1
0
Fork 0
mirror of synced 2024-09-28 23:41:23 +12:00

feat: Adapting Migrate to DI

This commit is contained in:
Binyamin Yawitz 2024-06-24 13:31:01 -04:00
parent 4ea291705a
commit aa4bc1c776
No known key found for this signature in database
2 changed files with 30 additions and 38 deletions

View file

@ -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;
}
}
}
}

View file

@ -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) {