From 6f6a7eb6f8ca33302d2d5844e94a3e627ea11bad Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 19 Jan 2022 12:59:58 +0100 Subject: [PATCH] feat: add migration to populate search fields --- app/tasks/migrate.php | 13 ++-- src/Appwrite/Migration/Version/V12.php | 89 ++++++++++++++++++++++- tests/unit/Migration/MigrationV12Test.php | 75 ++++++++++++++++++- 3 files changed, 165 insertions(+), 12 deletions(-) diff --git a/app/tasks/migrate.php b/app/tasks/migrate.php index c5736a038..bf49ac0ac 100644 --- a/app/tasks/migrate.php +++ b/app/tasks/migrate.php @@ -1,8 +1,7 @@ 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; diff --git a/src/Appwrite/Migration/Version/V12.php b/src/Appwrite/Migration/Version/V12.php index 42d01e008..9b9376aba 100644 --- a/src/Appwrite/Migration/Version/V12.php +++ b/src/Appwrite/Migration/Version/V12.php @@ -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); + } } diff --git a/tests/unit/Migration/MigrationV12Test.php b/tests/unit/Migration/MigrationV12Test.php index cdab56904..0c268ecfc 100644 --- a/tests/unit/Migration/MigrationV12Test.php +++ b/tests/unit/Migration/MigrationV12Test.php @@ -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'); } }