feat: add migration to populate search fields
This commit is contained in:
parent
4cc89ae597
commit
6f6a7eb6f8
|
@ -1,8 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
global $cli, $register, $projectDB, $console;
|
global $cli, $register;
|
||||||
|
|
||||||
use Utopia\Config\Config;
|
|
||||||
use Utopia\CLI\Console;
|
use Utopia\CLI\Console;
|
||||||
use Appwrite\Migration\Migration;
|
use Appwrite\Migration\Migration;
|
||||||
use Utopia\App;
|
use Utopia\App;
|
||||||
|
@ -24,6 +23,8 @@ $cli
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$app = new App('UTC');
|
||||||
|
|
||||||
Console::success('Starting Data Migration to version ' . $version);
|
Console::success('Starting Data Migration to version ' . $version);
|
||||||
|
|
||||||
$db = $register->get('db', true);
|
$db = $register->get('db', true);
|
||||||
|
@ -38,21 +39,19 @@ $cli
|
||||||
$consoleDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
$consoleDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
||||||
$consoleDB->setNamespace('_project_console');
|
$consoleDB->setNamespace('_project_console');
|
||||||
|
|
||||||
$console = $consoleDB->getDocument('projects', 'console');
|
$console = $app->getResource('console');
|
||||||
|
|
||||||
$limit = 30;
|
$limit = 30;
|
||||||
$sum = 30;
|
$sum = 30;
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
$projects = [$console];
|
$projects = [$console];
|
||||||
$count = 0;
|
$count = 0;
|
||||||
$totalProjects = $consoleDB->count('projects');
|
$totalProjects = $consoleDB->count('projects') + 1;
|
||||||
|
|
||||||
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
|
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
|
||||||
$migration = new $class();
|
$migration = new $class();
|
||||||
|
|
||||||
while ($sum > 0) {
|
while ($sum > 0) {
|
||||||
$projects = $consoleDB->find('projects', limit: $limit, offset: $offset);
|
|
||||||
|
|
||||||
foreach ($projects as $project) {
|
foreach ($projects as $project) {
|
||||||
try {
|
try {
|
||||||
$migration
|
$migration
|
||||||
|
@ -65,6 +64,8 @@ $cli
|
||||||
}
|
}
|
||||||
|
|
||||||
$sum = \count($projects);
|
$sum = \count($projects);
|
||||||
|
$projects = $consoleDB->find('projects', limit: $limit, offset: $offset);
|
||||||
|
|
||||||
$offset = $offset + $limit;
|
$offset = $offset + $limit;
|
||||||
$count = $count + $sum;
|
$count = $count + $sum;
|
||||||
|
|
||||||
|
|
|
@ -18,15 +18,96 @@ class V12 extends Migration
|
||||||
protected function fixDocument(Document $document)
|
protected function fixDocument(Document $document)
|
||||||
{
|
{
|
||||||
switch ($document->getCollection()) {
|
switch ($document->getCollection()) {
|
||||||
/**
|
|
||||||
* Bump Project version number.
|
|
||||||
*/
|
|
||||||
case 'projects':
|
case 'projects':
|
||||||
$document->setAttribute('version', '0.13.0');
|
/**
|
||||||
|
* Bump Project version number.
|
||||||
|
*/
|
||||||
|
$document->setAttribute('version', '0.13.0');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'users':
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'email', 'name'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'teams':
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'files':
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'functions':
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name', 'runtime'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'tags':
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'command'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'executions':
|
||||||
|
/**
|
||||||
|
* Populate search string from Migration to 0.12.
|
||||||
|
*/
|
||||||
|
if (empty($document->getAttribute('search'))) {
|
||||||
|
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'functionId'], $document));
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $document;
|
return $document;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a search string for a fulltext index.
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* @param Document $document
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function buildSearchAttribute(array $values, Document $document): string
|
||||||
|
{
|
||||||
|
$values = array_filter(array_map(fn (string $value) => $document->getAttribute($value) ?? '', $values));
|
||||||
|
|
||||||
|
return implode(' ', $values);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,85 @@ class MigrationV12Test extends MigrationTest
|
||||||
$this->method->setAccessible(true);
|
$this->method->setAccessible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMigration()
|
public function testMigrationProjects()
|
||||||
{
|
{
|
||||||
$document = $this->fixDocument(new Document([
|
$document = $this->fixDocument(new Document([
|
||||||
'$id' => 'project',
|
'$id' => 'project',
|
||||||
'$collection' => 'projects',
|
'$collection' => 'projects',
|
||||||
'version' => '0.12.0'
|
'name' => 'Appwrite',
|
||||||
|
'version' => '0.12.0',
|
||||||
|
'search' => ''
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->assertEquals($document->getAttribute('version'), '0.13.0');
|
$this->assertEquals($document->getAttribute('version'), '0.13.0');
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'project Appwrite');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMigrationUsers()
|
||||||
|
{
|
||||||
|
$document = $this->fixDocument(new Document([
|
||||||
|
'$id' => 'user',
|
||||||
|
'$collection' => 'users',
|
||||||
|
'email' => 'test@appwrite.io',
|
||||||
|
'name' => 'Torsten Dittmann'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'user test@appwrite.io Torsten Dittmann');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMigrationTeams()
|
||||||
|
{
|
||||||
|
$document = $this->fixDocument(new Document([
|
||||||
|
'$id' => 'team',
|
||||||
|
'$collection' => 'teams',
|
||||||
|
'name' => 'Appwrite'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'team Appwrite');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMigrationFiles()
|
||||||
|
{
|
||||||
|
$document = $this->fixDocument(new Document([
|
||||||
|
'$id' => 'file',
|
||||||
|
'$collection' => 'files',
|
||||||
|
'name' => 'Dog.jpeg'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'file Dog.jpeg');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMigrationFunctions()
|
||||||
|
{
|
||||||
|
$document = $this->fixDocument(new Document([
|
||||||
|
'$id' => 'function',
|
||||||
|
'$collection' => 'functions',
|
||||||
|
'name' => 'My Function',
|
||||||
|
'runtime' => 'php-8.0'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'function My Function php-8.0');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMigrationTags()
|
||||||
|
{
|
||||||
|
$document = $this->fixDocument(new Document([
|
||||||
|
'$id' => 'tag',
|
||||||
|
'$collection' => 'tags',
|
||||||
|
'command' => 'php main.php'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'tag php main.php');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMigrationExecutions()
|
||||||
|
{
|
||||||
|
$document = $this->fixDocument(new Document([
|
||||||
|
'$id' => 'execution',
|
||||||
|
'$collection' => 'executions',
|
||||||
|
'functionId' => 'function'
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($document->getAttribute('search'), 'execution function');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue