1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

feat: add migration to populate search fields

This commit is contained in:
Torsten Dittmann 2022-01-19 12:59:58 +01:00
parent 4cc89ae597
commit 6f6a7eb6f8
3 changed files with 165 additions and 12 deletions

View file

@ -1,8 +1,7 @@
<?php
global $cli, $register, $projectDB, $console;
global $cli, $register;
use Utopia\Config\Config;
use Utopia\CLI\Console;
use Appwrite\Migration\Migration;
use Utopia\App;
@ -24,6 +23,8 @@ $cli
return;
}
$app = new App('UTC');
Console::success('Starting Data Migration to version ' . $version);
$db = $register->get('db', true);
@ -38,21 +39,19 @@ $cli
$consoleDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$consoleDB->setNamespace('_project_console');
$console = $consoleDB->getDocument('projects', 'console');
$console = $app->getResource('console');
$limit = 30;
$sum = 30;
$offset = 0;
$projects = [$console];
$count = 0;
$totalProjects = $consoleDB->count('projects');
$totalProjects = $consoleDB->count('projects') + 1;
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
$migration = new $class();
while ($sum > 0) {
$projects = $consoleDB->find('projects', limit: $limit, offset: $offset);
foreach ($projects as $project) {
try {
$migration
@ -65,6 +64,8 @@ $cli
}
$sum = \count($projects);
$projects = $consoleDB->find('projects', limit: $limit, offset: $offset);
$offset = $offset + $limit;
$count = $count + $sum;

View file

@ -18,15 +18,96 @@ class V12 extends Migration
protected function fixDocument(Document $document)
{
switch ($document->getCollection()) {
/**
* Bump Project version number.
*/
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;
}
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);
}
}

View file

@ -16,14 +16,85 @@ class MigrationV12Test extends MigrationTest
$this->method->setAccessible(true);
}
public function testMigration()
public function testMigrationProjects()
{
$document = $this->fixDocument(new Document([
'$id' => 'project',
'$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('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');
}
}