appwrite/src/Appwrite/CLI/Tasks/Migrate.php

108 lines
3.3 KiB
PHP
Raw Normal View History

2022-07-13 07:02:55 +00:00
<?php
2022-07-14 02:04:31 +00:00
2022-08-02 01:32:46 +00:00
namespace Appwrite\CLI\Tasks;
2022-07-13 07:02:55 +00:00
use Utopia\Platform\Action;
use Utopia\CLI\Console;
use Appwrite\Migration\Migration;
use Utopia\App;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\Redis as RedisCache;
use Utopia\Database\Validator\Authorization;
2022-10-28 07:36:02 +00:00
use Utopia\Registry\Registry;
2022-07-13 07:02:55 +00:00
use Utopia\Validator\Text;
2022-07-14 02:04:31 +00:00
class Migrate extends Action
{
2022-08-02 01:58:36 +00:00
public static function getName(): string
{
return 'migrate';
}
2022-07-14 02:04:31 +00:00
2022-07-13 07:02:55 +00:00
public function __construct()
{
$this
2022-08-02 01:58:36 +00:00
->desc('Migrate Appwrite to new version')
2022-07-13 07:02:55 +00:00
->param('version', APP_VERSION_STABLE, new Text(8), 'Version to migrate to.', true)
2022-10-28 07:36:02 +00:00
->inject('register')
->callback(fn ($version, $register) => $this->action($version, $register));
2022-07-13 07:02:55 +00:00
}
2022-10-28 07:36:02 +00:00
public function action(string $version, Registry $register)
2022-07-14 02:04:31 +00:00
{
2022-07-13 07:02:55 +00:00
Authorization::disable();
if (!array_key_exists($version, Migration::$versions)) {
Console::error("Version {$version} not found.");
Console::exit(1);
return;
}
$app = new App('UTC');
Console::success('Starting Data Migration to version ' . $version);
2022-07-15 20:58:15 +00:00
$dbPool = $register->get('dbPool', true);
2022-07-13 07:02:55 +00:00
$redis = $register->get('cache', true);
$redis->flushAll();
$cache = new Cache(new RedisCache($redis));
2022-07-15 20:58:15 +00:00
$dbForConsole = $dbPool->getDB('console', $cache);
$dbForConsole->setNamespace('_project_console');
2022-07-13 07:02:55 +00:00
$console = $app->getResource('console');
$limit = 30;
$sum = 30;
$offset = 0;
2022-09-09 17:50:05 +00:00
/**
* @var \Utopia\Database\Document[] $projects
*/
2022-07-13 07:02:55 +00:00
$projects = [$console];
$count = 0;
try {
2022-07-15 20:58:15 +00:00
$totalProjects = $dbForConsole->count('projects') + 1;
2022-07-13 07:02:55 +00:00
} catch (\Throwable $th) {
2022-07-15 20:58:15 +00:00
$dbForConsole->setNamespace('_console');
$totalProjects = $dbForConsole->count('projects') + 1;
2022-07-13 07:02:55 +00:00
}
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
$migration = new $class();
while (!empty($projects)) {
foreach ($projects as $project) {
2022-09-09 17:50:05 +00:00
/**
* Skip user projects with id 'console'
*/
if ($project->getId() === 'console' && $project->getInternalId() !== 'console') {
continue;
}
2022-07-13 07:02:55 +00:00
try {
2022-07-15 20:58:15 +00:00
// TODO: Iterate through all project DBs
$projectDB = $dbPool->getDB($project->getId(), $cache);
2022-07-13 07:02:55 +00:00
$migration
2022-07-15 20:58:15 +00:00
->setProject($project, $projectDB, $dbForConsole)
2022-07-13 07:02:55 +00:00
->execute();
} catch (\Throwable $th) {
throw $th;
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
}
}
$sum = \count($projects);
2022-07-15 20:58:15 +00:00
$projects = $dbForConsole->find('projects', limit: $limit, offset: $offset);
2022-07-13 07:02:55 +00:00
$offset = $offset + $limit;
$count = $count + $sum;
Console::log('Migrated ' . $count . '/' . $totalProjects . ' projects...');
}
2022-10-14 09:34:02 +00:00
Swoole\Event::wait(); // Wait for Coroutines to finish
2022-07-13 07:02:55 +00:00
$redis->flushAll();
Console::success('Data Migration Completed');
}
2022-07-14 02:04:31 +00:00
}