From 787a41a52de0decbf73e401fef96c543286364f0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 17 Mar 2023 01:21:46 +0000 Subject: [PATCH] added migration --- src/Appwrite/Migration/Version/V19.php | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/Appwrite/Migration/Version/V19.php diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php new file mode 100644 index 0000000000..2ee1a2edc2 --- /dev/null +++ b/src/Appwrite/Migration/Version/V19.php @@ -0,0 +1,94 @@ + null, + fn () => [] + ); + } + + Console::log('Migrating Project: ' . $this->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("Migrating Collection \"{$id}\""); + + $this->projectDB->setNamespace("_{$this->project->getInternalId()}"); + + switch ($id) { + case 'projects': + try { + /** + * Create 'passwordHistory' attribute + */ + $this->createAttributeFromCollection($this->projectDB, $id, 'smtp'); + $this->createAttributeFromCollection($this->projectDB, $id, 'templates'); + $this->projectDB->deleteCachedCollection($id); + } catch (\Throwable $th) { + Console::warning("'SMTP and Templates' from {$id}: {$th->getMessage()}"); + } + break; + default: + break; + } + + usleep(50000); + } + } + + /** + * 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 version number. + */ + $document->setAttribute('version', '1.4.0'); + + $document->setAttribute('smtp', []); + $document->setAttribute('templates', []); + + break; + } + + return $document; + } +}