From ff82a24a4b23595c800801aa617e5d1c00cbe8b1 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 24 Apr 2023 13:00:23 +0200 Subject: [PATCH] feat: migration --- app/http.php | 7 +----- composer.lock | 20 ++++++++--------- src/Appwrite/Migration/Migration.php | 9 ++++++++ src/Appwrite/Migration/Version/V18.php | 31 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/app/http.php b/app/http.php index 4a0f1046dd..249e8dc2d9 100644 --- a/app/http.php +++ b/app/http.php @@ -115,12 +115,6 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { if (!$dbForConsole->getCollection($key)->isEmpty()) { continue; } - /** - * Skip to prevent 0.16 migration issues. - */ - if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) { - continue; - } Console::success('[Setup] - Creating collection: ' . $collection['$id'] . '...'); @@ -215,6 +209,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { } Console::success('[Setup] - Server database init completed...'); + var_dump($dbForConsole->find(Database::METADATA)); }); Console::success('Server started successfully (max payload is ' . number_format($payloadSize) . ' bytes)'); diff --git a/composer.lock b/composer.lock index 254c2a4186..cbe17fdc2e 100644 --- a/composer.lock +++ b/composer.lock @@ -2113,12 +2113,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "6ce621ac9d97e3ff18e1595c328526b3050ab68b" + "reference": "c3dfecc21e3b8ce8f233425150b4a330739806b5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/6ce621ac9d97e3ff18e1595c328526b3050ab68b", - "reference": "6ce621ac9d97e3ff18e1595c328526b3050ab68b", + "url": "https://api.github.com/repos/utopia-php/database/zipball/c3dfecc21e3b8ce8f233425150b4a330739806b5", + "reference": "c3dfecc21e3b8ce8f233425150b4a330739806b5", "shasum": "" }, "require": { @@ -2163,7 +2163,7 @@ "issues": "https://github.com/utopia-php/database/issues", "source": "https://github.com/utopia-php/database/tree/feat-collection-level-permissions" }, - "time": "2023-04-21T10:56:21+00:00" + "time": "2023-04-24T09:13:37+00:00" }, { "name": "utopia-php/domains", @@ -3784,16 +3784,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.20.0", + "version": "1.20.2", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "10553ab3f0337ff1a71433c3417d7eb2a3eec1fd" + "reference": "90490bd8fd8530a272043c4950c180b6d0cf5f81" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/10553ab3f0337ff1a71433c3417d7eb2a3eec1fd", - "reference": "10553ab3f0337ff1a71433c3417d7eb2a3eec1fd", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/90490bd8fd8530a272043c4950c180b6d0cf5f81", + "reference": "90490bd8fd8530a272043c4950c180b6d0cf5f81", "shasum": "" }, "require": { @@ -3823,9 +3823,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.20.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.20.2" }, - "time": "2023-04-20T11:18:07+00:00" + "time": "2023-04-22T12:59:35+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index 4a79ffafbf..39740a1f1c 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -37,6 +37,9 @@ abstract class Migration */ protected Database $consoleDB; + /** + * @var \PDO + */ protected \PDO $pdo; /** @@ -102,6 +105,12 @@ abstract class Migration return $this; } + /** + * Set PDO for Migration. + * + * @param \PDO $pdo + * @return \Appwrite\Migration\Migration + */ public function setPDO(\PDO $pdo): self { $this->pdo = $pdo; diff --git a/src/Appwrite/Migration/Version/V18.php b/src/Appwrite/Migration/Version/V18.php index eba19fa861..3e57686e75 100644 --- a/src/Appwrite/Migration/Version/V18.php +++ b/src/Appwrite/Migration/Version/V18.php @@ -25,6 +25,7 @@ class V18 extends Migration Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')'); $this->projectDB->setNamespace("_{$this->project->getInternalId()}"); + $this->addDocumentSecurityToProject(); Console::info('Migrating Databases'); $this->migrateDatabases(); @@ -58,6 +59,15 @@ class V18 extends Migration } $this->changeAttributeInternalType($collectionTable, $attribute['key'], 'DOUBLE'); } + + try { + $documentSecurity = $collection->getAttribute('documentSecurity', false); + $permissions = $collection->getPermissions(); + + $this->projectDB->updateCollection($collectionTable, $permissions, $documentSecurity); + } catch (\Throwable $th) { + Console::warning($th->getMessage()); + } } } } @@ -168,4 +178,25 @@ class V18 extends Migration return $document; } + + protected function addDocumentSecurityToProject(): void + { + try { + /** + * Create 'documentSecurity' column + */ + $this->pdo->prepare("ALTER TABLE `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getInternalId()}__metadata` ADD COLUMN IF NOT EXISTS documentSecurity TINYINT(1);")->execute(); + } catch (\Throwable $th) { + Console::warning($th->getMessage()); + } + + try { + /** + * Set 'documentSecurity' column to 1 if NULL + */ + $this->pdo->prepare("UPDATE `{$this->projectDB->getDefaultDatabase()}`.`_{$this->project->getInternalId()}__metadata` SET documentSecurity = 1 WHERE documentSecurity IS NULL")->execute(); + } catch (\Throwable $th) { + Console::warning($th->getMessage()); + } + } }