From 787059e88d56a174e160617ef28a960d2baf99b3 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 1 Jul 2021 15:35:54 +0200 Subject: [PATCH] feat(migration): 0.9.x --- app/tasks/migrate.php | 8 ++-- src/Appwrite/Migration/Version/V07.php | 1 - src/Appwrite/Migration/Version/V08.php | 53 +++++++++++++++++++++++ tests/unit/Migration/MigrationV08Test.php | 33 ++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/Appwrite/Migration/Version/V08.php create mode 100644 tests/unit/Migration/MigrationV08Test.php diff --git a/app/tasks/migrate.php b/app/tasks/migrate.php index 7377de795..dad0847e9 100644 --- a/app/tasks/migrate.php +++ b/app/tasks/migrate.php @@ -14,16 +14,18 @@ $cli ->task('migrate') ->action(function () use ($register) { Console::success('Starting Data Migration'); + $db = $register->get('db', true); + $cache = $register->get('cache', true); $consoleDB = new Database(); $consoleDB - ->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)) + ->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache)) ->setNamespace('app_console') // Main DB ->setMocks(Config::getParam('collections', [])); $projectDB = new Database(); $projectDB - ->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)) + ->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache)) ->setMocks(Config::getParam('collections', [])); $console = $consoleDB->getDocument('console'); @@ -36,7 +38,7 @@ $cli $projects = [$console]; $count = 0; - $migration = new Version\V07($register->get('db')); //TODO: remove hardcoded version and move to dynamic migration + $migration = new Version\V08($register->get('db')); //TODO: remove hardcoded version and move to dynamic migration while ($sum > 0) { foreach ($projects as $project) { diff --git a/src/Appwrite/Migration/Version/V07.php b/src/Appwrite/Migration/Version/V07.php index 9c3054a28..3efd7d15c 100644 --- a/src/Appwrite/Migration/Version/V07.php +++ b/src/Appwrite/Migration/Version/V07.php @@ -13,7 +13,6 @@ class V07 extends Migration { public function execute(): void { - $db = $this->db; $project = $this->project; Console::log('Migrating project: ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); diff --git a/src/Appwrite/Migration/Version/V08.php b/src/Appwrite/Migration/Version/V08.php new file mode 100644 index 000000000..59294c326 --- /dev/null +++ b/src/Appwrite/Migration/Version/V08.php @@ -0,0 +1,53 @@ +project; + Console::log('Migrating project: ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); + + $this->forEachDocument([$this, 'fixDocument']); + } + + protected function fixDocument(Document $document) + { + switch ($document->getAttribute('$collection')) { + /** + * Rename env attribute to runtime. + */ + case Database::SYSTEM_COLLECTION_FUNCTIONS: + if ($document->isSet('env')) { + $document + ->setAttribute('runtime', $document->getAttribute('env', $document->getAttribute('env', ''))) + ->removeAttribute('env'); + } + + break; + } + + foreach ($document as &$attr) { + if ($attr instanceof Document) { + $attr = $this->fixDocument($attr); + } + + if (\is_array($attr)) { + foreach ($attr as &$child) { + if ($child instanceof Document) { + $child = $this->fixDocument($child); + } + } + } + } + + return $document; + } +} diff --git a/tests/unit/Migration/MigrationV08Test.php b/tests/unit/Migration/MigrationV08Test.php new file mode 100644 index 000000000..cb759cecb --- /dev/null +++ b/tests/unit/Migration/MigrationV08Test.php @@ -0,0 +1,33 @@ +pdo = new \PDO('sqlite::memory:'); + $this->migration = new V08($this->pdo); + $reflector = new ReflectionClass('Appwrite\Migration\Version\V08'); + $this->method = $reflector->getMethod('fixDocument'); + $this->method->setAccessible(true); + } + + public function testMigration() + { + $document = $this->fixDocument(new Document([ + '$id' => 'unique', + '$collection' => Database::SYSTEM_COLLECTION_FUNCTIONS, + 'env' => 'node-16' + ])); + + $this->assertEquals($document->getAttribute('env', null), null); + $this->assertEquals($document->getAttribute('runtime', null), 'node-16'); + } +}