From 86390701159c533e045d0015a16ee2bf287063d0 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 30 Aug 2021 14:25:23 +0200 Subject: [PATCH] feat(migration): add 0.10.x migration --- app/tasks/migrate.php | 2 +- src/Appwrite/Migration/Migration.php | 2 +- src/Appwrite/Migration/Version/V09.php | 48 +++++++++++++++++++++++ tests/unit/Migration/MigrationV09Test.php | 31 +++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Appwrite/Migration/Version/V09.php create mode 100644 tests/unit/Migration/MigrationV09Test.php diff --git a/app/tasks/migrate.php b/app/tasks/migrate.php index 60aad941d..be80c6e22 100644 --- a/app/tasks/migrate.php +++ b/app/tasks/migrate.php @@ -72,7 +72,7 @@ $cli $sum = \count($projects); $offset = $offset + $limit; $count = $count + $sum; - + if ($sum > 0) { Console::log('Fetched '.$count.'/'.$consoleDB->getSum().' projects...'); } diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index 2ed66ba06..85ddf14ac 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -43,7 +43,7 @@ abstract class Migration '0.9.2' => 'V08', '0.9.3' => 'V08', '0.9.4' => 'V08', - '0.10.0' => 'V08', + '0.10.0' => 'V09', ]; /** diff --git a/src/Appwrite/Migration/Version/V09.php b/src/Appwrite/Migration/Version/V09.php new file mode 100644 index 000000000..2d1411c4c --- /dev/null +++ b/src/Appwrite/Migration/Version/V09.php @@ -0,0 +1,48 @@ +project; + Console::log('Migrating project: ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); + + $this->forEachDocument([$this, 'fixDocument']); + } + + protected function fixDocument(Document $document) + { + switch ($document->getAttribute('$collection')) { + /** + * Add version reference to database. + */ + case Database::SYSTEM_COLLECTION_PROJECTS: + $document->setAttribute('version', '0.10.0'); + + 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/MigrationV09Test.php b/tests/unit/Migration/MigrationV09Test.php new file mode 100644 index 000000000..d5bee3808 --- /dev/null +++ b/tests/unit/Migration/MigrationV09Test.php @@ -0,0 +1,31 @@ +pdo = new \PDO('sqlite::memory:'); + $this->migration = new V09($this->pdo); + $reflector = new ReflectionClass('Appwrite\Migration\Version\V09'); + $this->method = $reflector->getMethod('fixDocument'); + $this->method->setAccessible(true); + } + + public function testMigration() + { + $document = $this->fixDocument(new Document([ + '$id' => 'project', + '$collection' => Database::SYSTEM_COLLECTION_PROJECTS, + 'version' => '0.9.0' + ])); + + $this->assertEquals($document->getAttribute('version', '0.9.0'), '0.10.0'); + } +}