From 39b994c958fd818805177a3d0c5670c614f359bc Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 13 May 2022 14:49:31 +0200 Subject: [PATCH] fix: migration --- src/Appwrite/Migration/Migration.php | 61 ++++++++++++++++++++++++-- src/Appwrite/Migration/Version/V12.php | 47 -------------------- src/Appwrite/Migration/Version/V13.php | 7 +++ 3 files changed, 65 insertions(+), 50 deletions(-) diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index f669360614..b5c32733ea 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -8,6 +8,8 @@ use Utopia\Database\Database; use Utopia\CLI\Console; use Utopia\Config\Config; use Exception; +use Utopia\App; +use Utopia\Database\Validator\Authorization; abstract class Migration { @@ -50,15 +52,20 @@ abstract class Migration public function __construct() { + Authorization::disable(); + Authorization::setDefaultStatus(false); $this->collections = array_merge([ '_metadata' => [ - '$id' => '_metadata' + '$id' => '_metadata', + '$collection' => Database::METADATA ], 'audit' => [ - '$id' => 'audit' + '$id' => 'audit', + '$collection' => Database::METADATA ], 'abuse' => [ - '$id' => 'abuse' + '$id' => 'abuse', + '$collection' => Database::METADATA ] ], Config::getParam('collections', [])); } @@ -93,6 +100,7 @@ abstract class Migration Runtime::enableCoroutine(SWOOLE_HOOK_ALL); foreach ($this->collections as $collection) { + if ($collection['$collection'] !== Database::METADATA) return; $sum = 0; $nextDocument = null; $collectionCount = $this->projectDB->count($collection['$id']); @@ -189,6 +197,53 @@ abstract class Migration return $result; } + /** + * Creates colletion from the config collection. + * + * @param string $id + * @param string|null $name + * @return void + * @throws \Throwable + */ + protected function createCollection(string $id, string $name = null): void + { + $name ??= $id; + + if (!$this->projectDB->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), $name)) { + $attributes = []; + $indexes = []; + $collection = $this->collections[$id]; + + foreach ($collection['attributes'] as $attribute) { + $attributes[] = new Document([ + '$id' => $attribute['$id'], + 'type' => $attribute['type'], + 'size' => $attribute['size'], + 'required' => $attribute['required'], + 'signed' => $attribute['signed'], + 'array' => $attribute['array'], + 'filters' => $attribute['filters'], + ]); + } + + foreach ($collection['indexes'] as $index) { + $indexes[] = new Document([ + '$id' => $index['$id'], + 'type' => $index['type'], + 'attributes' => $index['attributes'], + 'lengths' => $index['lengths'], + 'orders' => $index['orders'], + ]); + } + + try { + $this->projectDB->createCollection($name, $attributes, $indexes); + } catch (\Throwable $th) { + throw $th; + } + } + } + /** * Executes migration for set project. */ diff --git a/src/Appwrite/Migration/Version/V12.php b/src/Appwrite/Migration/Version/V12.php index 0b8f194d20..6b8e88c7d6 100644 --- a/src/Appwrite/Migration/Version/V12.php +++ b/src/Appwrite/Migration/Version/V12.php @@ -297,53 +297,6 @@ class V12 extends Migration } } - /** - * Creates colletion from the config collection. - * - * @param string $id - * @param string|null $name - * @return void - * @throws \Throwable - */ - protected function createCollection(string $id, string $name = null): void - { - $name ??= $id; - - if (!$this->projectDB->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), $name)) { - $attributes = []; - $indexes = []; - $collection = $this->collections[$id]; - - foreach ($collection['attributes'] as $attribute) { - $attributes[] = new Document([ - '$id' => $attribute['$id'], - 'type' => $attribute['type'], - 'size' => $attribute['size'], - 'required' => $attribute['required'], - 'signed' => $attribute['signed'], - 'array' => $attribute['array'], - 'filters' => $attribute['filters'], - ]); - } - - foreach ($collection['indexes'] as $index) { - $indexes[] = new Document([ - '$id' => $index['$id'], - 'type' => $index['type'], - 'attributes' => $index['attributes'], - 'lengths' => $index['lengths'], - 'orders' => $index['orders'], - ]); - } - - try { - $this->projectDB->createCollection($name, $attributes, $indexes); - } catch (\Throwable $th) { - throw $th; - } - } - } - /** * Migrates permissions to dedicated table. * diff --git a/src/Appwrite/Migration/Version/V13.php b/src/Appwrite/Migration/Version/V13.php index d799854a71..cbe4cf2bed 100644 --- a/src/Appwrite/Migration/Version/V13.php +++ b/src/Appwrite/Migration/Version/V13.php @@ -177,6 +177,13 @@ class V13 extends Migration case 'stats': //TODO: migrate value (size => 8) break; + case 'tokens': + try { + $this->createCollection('tokens'); + } catch (\Throwable $th) { + Console::warning("'tokens': {$th->getMessage()}"); + } + break; } usleep(100000); }