From 07369cbfd256110b3ee3f107fbecbca82f338ceb Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Sun, 8 May 2022 16:31:50 +0200 Subject: [PATCH] feat: migration for 0.14 --- src/Appwrite/Migration/Version/V13.php | 216 ++++++++++++++++++++++ tests/unit/Migration/MigrationV13Test.php | 51 +++++ 2 files changed, 267 insertions(+) create mode 100644 src/Appwrite/Migration/Version/V13.php create mode 100644 tests/unit/Migration/MigrationV13Test.php diff --git a/src/Appwrite/Migration/Version/V13.php b/src/Appwrite/Migration/Version/V13.php new file mode 100644 index 0000000000..8255fa9d27 --- /dev/null +++ b/src/Appwrite/Migration/Version/V13.php @@ -0,0 +1,216 @@ +project->getAttribute('name') . ' (' . $this->project->getId() . ')'); + Console::info('Migrating Collections'); + $this->migrateCollections(); + Console::info('Migrating Documents'); + $this->forEachDocument([$this, 'fixDocument']); + } + + /** + * Migrate all Collections. + * + * @return void + */ + protected function migrateCollections(): void + { + foreach ($this->collections as $collection) { + $id = $collection['$id']; + + Console::log("- {$id}"); + switch ($id) { + case 'executions': + try { + /** + * Rename stdout to response + */ + $this->projectDB->renameAttribute($id, 'stdout', 'response'); + } catch (\Throwable $th) { + Console::warning("'stdout' from {$id}: {$th->getMessage()}"); + } + break; + case 'projects': + try { + /** + * Rename providers to authProviders + */ + $this->projectDB->renameAttribute($id, 'providers', 'authProviders'); + } catch (\Throwable $th) { + Console::warning("'providers' from {$id}: {$th->getMessage()}"); + } + break; + } + usleep(100000); + } + } + + /** + * Fix run on each document + * + * @param \Utopia\Database\Document $document + * @return \Utopia\Database\Document + */ + protected function fixDocument(Document $document) + { + switch ($document->getCollection()) { + case 'projects': + /** + * Bump Project version number. + */ + $document->setAttribute('version', '0.14.0'); + + break; + + case 'functions': + /** + * Migrate events. + */ + if (!empty($document->getAttribute('events'))) { + $document->setAttribute('events', $this->migrateEvents($document->getAttribute('events'))); + } + + break; + + case 'webhooks': + /** + * Migrate events. + */ + if (!empty($document->getAttribute('events'))) { + $document->setAttribute('events', $this->migrateEvents($document->getAttribute('events'))); + } + + break; + } + + return $document; + } + + public function migrateEvents(array $events): array + { + return array_filter(array_unique(array_map(function ($event) { + if (!in_array($event, $this->events)) return $event; + $parts = \explode('.', $event); + $first = array_shift($parts); + switch ($first) { + case 'account': + case 'users': + $first = 'users'; + + switch ($parts[0]) { + case 'recovery': + case 'sessions': + case 'verification': + $second = array_shift($parts); + return 'users.*.' . $second . '.*.' . implode('.', $parts); + + default: + return 'users.*.' . implode('.', $parts); + } + case 'functions': + switch ($parts[0]) { + case 'deployments': + case 'executions': + $second = array_shift($parts); + return 'functions.*.' . $second . '.*.' . implode('.', $parts); + + default: + return 'functions.*.' . implode('.', $parts); + } + case 'teams': + switch ($parts[0]) { + case 'memberships': + $second = array_shift($parts); + return 'teams.*.' . $second . '.*.' . implode('.', $parts); + + default: + return 'teams.*.' . implode('.', $parts); + } + case 'storage': + $second = array_shift($parts); + switch ($second) { + case 'buckets': + return 'buckets.*.' . implode('.', $parts); + case 'files': + return 'buckets.*.' . $second . '.*.' . implode('.', $parts); + } + case 'database': + $second = array_shift($parts); + switch ($second) { + case 'collections': + return 'collections.*.' . implode('.', $parts); + case 'documents': + case 'indexes': + case 'attributes': + return 'collections.*.' . $second . '.*.' . implode('.', $parts); + } + } + return ''; + }, $events))); + } +} diff --git a/tests/unit/Migration/MigrationV13Test.php b/tests/unit/Migration/MigrationV13Test.php new file mode 100644 index 0000000000..2315a4ff63 --- /dev/null +++ b/tests/unit/Migration/MigrationV13Test.php @@ -0,0 +1,51 @@ +migration = new V13(); + $reflector = new ReflectionClass('Appwrite\Migration\Version\V13'); + $this->method = $reflector->getMethod('fixDocument'); + $this->method->setAccessible(true); + } + + public function testMigrateFunctions() + { + $document = $this->fixDocument(new Document([ + '$id' => 'func', + '$collection' => 'functions', + 'events' => ['account.create', 'users.create'] + ])); + + $this->assertEquals($document->getAttribute('events'), ['users.*.create']); + } + + public function testMigrationWebhooks() + { + $document = $this->fixDocument(new Document([ + '$id' => 'webh', + '$collection' => 'webhooks', + 'events' => ['account.create', 'users.create'] + ])); + + $this->assertEquals($document->getAttribute('events'), ['users.*.create']); + } + + public function testEventsConversion() + { + $migration = new V13(); + $events = $migration->migrateEvents($migration->events); + foreach ($events as $event) { + $this->assertTrue((new Event())->isValid($event), $event); + } + $this->assertCount(45, $events); + } +}