From 0c5e9eed332f2a8c74a2631a1fe7eaa457d3d260 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 29 Dec 2021 17:33:52 +0100 Subject: [PATCH] fix: migration --- composer.lock | 24 ++++++------- src/Appwrite/Migration/Version/V11.php | 48 ++++++++++++++++++++------ 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/composer.lock b/composer.lock index 2e8f6a495f..347db5efbc 100644 --- a/composer.lock +++ b/composer.lock @@ -5655,16 +5655,16 @@ }, { "name": "symfony/console", - "version": "v6.0.1", + "version": "v6.0.2", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "fafd9802d386bf1c267e0249ddb7ceb14dcfdad4" + "reference": "dd434fa8d69325e5d210f63070014d889511fcb3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/fafd9802d386bf1c267e0249ddb7ceb14dcfdad4", - "reference": "fafd9802d386bf1c267e0249ddb7ceb14dcfdad4", + "url": "https://api.github.com/repos/symfony/console/zipball/dd434fa8d69325e5d210f63070014d889511fcb3", + "reference": "dd434fa8d69325e5d210f63070014d889511fcb3", "shasum": "" }, "require": { @@ -5730,7 +5730,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v6.0.1" + "source": "https://github.com/symfony/console/tree/v6.0.2" }, "funding": [ { @@ -5746,7 +5746,7 @@ "type": "tidelift" } ], - "time": "2021-12-09T12:47:37+00:00" + "time": "2021-12-27T21:05:08+00:00" }, { "name": "symfony/polyfill-intl-grapheme", @@ -6077,16 +6077,16 @@ }, { "name": "symfony/string", - "version": "v6.0.1", + "version": "v6.0.2", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "0cfed595758ec6e0a25591bdc8ca733c1896af32" + "reference": "bae261d0c3ac38a1f802b4dfed42094296100631" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/0cfed595758ec6e0a25591bdc8ca733c1896af32", - "reference": "0cfed595758ec6e0a25591bdc8ca733c1896af32", + "url": "https://api.github.com/repos/symfony/string/zipball/bae261d0c3ac38a1f802b4dfed42094296100631", + "reference": "bae261d0c3ac38a1f802b4dfed42094296100631", "shasum": "" }, "require": { @@ -6142,7 +6142,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v6.0.1" + "source": "https://github.com/symfony/string/tree/v6.0.2" }, "funding": [ { @@ -6158,7 +6158,7 @@ "type": "tidelift" } ], - "time": "2021-12-08T15:13:44+00:00" + "time": "2021-12-16T22:13:01+00:00" }, { "name": "textalk/websocket", diff --git a/src/Appwrite/Migration/Version/V11.php b/src/Appwrite/Migration/Version/V11.php index 36a13383e3..0d2b08fc41 100644 --- a/src/Appwrite/Migration/Version/V11.php +++ b/src/Appwrite/Migration/Version/V11.php @@ -42,6 +42,7 @@ class V11 extends Migration $this->options = array_map(fn ($option) => $option === 'yes' ? true : false, $this->options); if (!is_null($cache)) { + $this->cache->flushAll(); $cacheAdapter = new Cache(new RedisCache($this->cache)); $this->dbProject = new Database(new MariaDB($this->db), $cacheAdapter); // namespace is set on execution $this->dbConsole = new Database(new MariaDB($this->db), $cacheAdapter); @@ -63,6 +64,7 @@ class V11 extends Migration $oldProject = $this->project; $this->dbProject->setNamespace('_project_' . $oldProject->getId()); + $this->dbConsole->setNamespace('_project_console'); Console::info(''); Console::info('------------------------------------'); @@ -73,7 +75,12 @@ class V11 extends Migration * Create internal/external structure for projects and skip the console project. */ if ($oldProject->getId() !== 'console') { - $project = $this->dbConsole->getDocument('projects', $oldProject->getId()); + try { + $project = $this->dbConsole->getDocument(collection: 'projects', id: $oldProject->getId()); + } catch (\Throwable $th) { + var_dump($th->getTraceAsString()); + var_dump($th); + } /** * Migrate Project Document. @@ -88,10 +95,10 @@ class V11 extends Migration /** * Create internal tables */ - if (!$this->dbProject->exists('appwrite')) { - $this->dbProject->create('appwrite'); + try { Console::log('Created internal tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); - } + $this->dbProject->createMetadata(); + } catch (\Throwable $th) { } /** * Create Audit tables @@ -197,10 +204,6 @@ class V11 extends Migration } catch (\Throwable $th) { Console::error('Failed to update document: ' . $th->getMessage()); continue; - - if ($document && $new->getId() !== $document->getId()) { - throw new Exception('Duplication Error'); - } } } @@ -270,6 +273,7 @@ class V11 extends Migration 'dateCreated' => time(), 'dateUpdated' => time(), 'name' => $name, + 'enabled' => true, 'search' => implode(' ', [$id, $name]), ])); } else { @@ -284,7 +288,7 @@ class V11 extends Migration foreach ($attributes as $attribute) { try { $this->dbProject->createAttribute( - collection: $attribute['$collection'], + collection: 'collection_' . $attribute['$collection'], id: $attribute['$id'], type: $attribute['type'], size: $attribute['size'], @@ -296,7 +300,6 @@ class V11 extends Migration formatOptions: $attribute['formatOptions'] ?? [], filters: $attribute['filters'] ); - $this->dbProject->createDocument('attributes', new Document([ '$id' => $attribute['$collection'] . '_' . $attribute['$id'], 'key' => $attribute['$id'], @@ -426,7 +429,30 @@ class V11 extends Migration } switch ($document->getAttribute('$collection')) { - case OldDatabase::SYSTEM_COLLECTION_PLATFORMS: + case OldDatabase::SYSTEM_COLLECTION_PROJECTS: + $newProviders = []; + $providers = Config::getParam('providers', []); + + /* + * Add enabled OAuth2 providers to default data rules + */ + foreach ($providers as $index => $provider) { + $appId = $document->getAttribute('usersOauth2'.\ucfirst($index).'Appid'); + $appSecret = $document->getAttribute('usersOauth2'.\ucfirst($index).'Secret'); + + if (!is_null($appId) || !is_null($appId)) { + $newProviders[$appId] = $appSecret; + } + + $document + ->removeAttribute('usersOauth2'.\ucfirst($index).'Appid') + ->removeAttribute('usersOauth2'.\ucfirst($index).'Secret'); + } + + $document->setAttribute('providers', $newProviders); + + break; + case OldDatabase::SYSTEM_COLLECTION_PLATFORMS: $projectId = $this->getProjectIdFromReadPermissions($document); /**