From aa4bc1c7769f632a1d0fec8887ba187517c253b3 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:31:01 -0400 Subject: [PATCH] feat: Adapting Migrate to DI --- src/Appwrite/Migration/Migration.php | 56 ++++++++++++------------- src/Appwrite/Platform/Tasks/Migrate.php | 12 ++---- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index 86fffb4b3e..695c42f05f 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -78,14 +78,14 @@ abstract class Migration '1.4.11' => 'V19', '1.4.12' => 'V19', '1.4.13' => 'V19', - '1.5.0' => 'V20', - '1.5.1' => 'V20', - '1.5.2' => 'V20', - '1.5.3' => 'V20', - '1.5.4' => 'V20', - '1.5.5' => 'V20', - '1.5.6' => 'V20', - '1.5.7' => 'V20', + '1.5.0' => 'V20', + '1.5.1' => 'V20', + '1.5.2' => 'V20', + '1.5.3' => 'V20', + '1.5.4' => 'V20', + '1.5.5' => 'V20', + '1.5.6' => 'V20', + '1.5.7' => 'V20', ]; /** @@ -170,29 +170,25 @@ abstract class Migration Console::log('Migrating Collection ' . $collection['$id'] . ':'); - \Co\run(function (array $collection, callable $callback) { - foreach ($this->documentsIterator($collection['$id']) as $document) { - go(function (Document $document, callable $callback) { - if (empty($document->getId()) || empty($document->getCollection())) { - return; - } - - $old = $document->getArrayCopy(); - $new = call_user_func($callback, $document); - - if (is_null($new) || $new->getArrayCopy() == $old) { - return; - } - - try { - $this->projectDB->updateDocument($document->getCollection(), $document->getId(), $document); - } catch (\Throwable $th) { - Console::error('Failed to update document: ' . $th->getMessage()); - return; - } - }, $document, $callback); + foreach ($this->documentsIterator($collection['$id']) as $document) { + if (empty($document->getId()) || empty($document->getCollection())) { + return; } - }, $collection, $callback); + + $old = $document->getArrayCopy(); + $new = call_user_func($callback, $document); + + if (is_null($new) || $new->getArrayCopy() == $old) { + return; + } + + try { + $this->projectDB->updateDocument($document->getCollection(), $document->getId(), $document); + } catch (\Throwable $th) { + Console::error('Failed to update document: ' . $th->getMessage()); + return; + } + } } } diff --git a/src/Appwrite/Platform/Tasks/Migrate.php b/src/Appwrite/Platform/Tasks/Migrate.php index e08985e1ae..5d000acdc5 100644 --- a/src/Appwrite/Platform/Tasks/Migrate.php +++ b/src/Appwrite/Platform/Tasks/Migrate.php @@ -9,8 +9,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; -use Utopia\Http\Adapter\FPM\Server; -use Utopia\Http\Http; use Utopia\Http\Validator\Text; use Utopia\Platform\Action; use Utopia\Registry\Registry; @@ -33,7 +31,8 @@ class Migrate extends Action ->inject('getProjectDB') ->inject('register') ->inject('authorization') - ->callback(fn ($version, $cache, $dbForConsole, $getProjectDB, Registry $register, Authorization $authorization) => $this->action($version, $cache, $dbForConsole, $getProjectDB, $register, $authorization)); + ->inject('console') + ->callback(fn ($version, $cache, $dbForConsole, $getProjectDB, Registry $register, Authorization $authorization, Document $console) => $this->action($version, $cache, $dbForConsole, $getProjectDB, $register, $authorization, $console)); } private function clearProjectsCache(Cache $cache, Document $project) @@ -45,7 +44,7 @@ class Migrate extends Action } } - public function action(string $version, Cache $cache, Database $dbForConsole, callable $getProjectDB, Registry $register, Authorization $auth) + public function action(string $version, Cache $cache, Database $dbForConsole, callable $getProjectDB, Registry $register, Authorization $auth, Document $console) { $auth->disable(); if (!array_key_exists($version, Migration::$versions)) { @@ -54,12 +53,9 @@ class Migrate extends Action return; } - $http = new Http(new Server(), 'UTC'); Console::success('Starting Data Migration to version ' . $version); - $console = $http->getResource('console'); - $limit = 30; $sum = 30; $offset = 0; @@ -78,7 +74,7 @@ class Migrate extends Action $class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version]; /** @var Migration $migration */ - $migration = new $class(); + $migration = new $class($auth, ); while (!empty($projects)) { foreach ($projects as $project) {