appwrite/src/Appwrite/Task/Migrate.php

98 lines
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-07-13 07:02:55 +00:00
namespace Appwrite\Task;
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\Adapter\MariaDB;
use Utopia\Database\Database;
use Utopia\Database\Validator\Authorization;
use Utopia\Validator\Text;
use Swoole\Event;
2022-07-14 02:04:31 +00:00
class Migrate extends Action
{
2022-07-13 07:02:55 +00:00
public const NAME = 'migrate';
2022-07-14 02:04:31 +00:00
2022-07-13 07:02:55 +00:00
public function __construct()
{
$this
->param('version', APP_VERSION_STABLE, new Text(8), 'Version to migrate to.', true)
->callback(fn ($version) => $this->action($version));
}
2022-07-14 02:04:31 +00:00
public function action($version)
{
2022-07-13 07:02:55 +00:00
global $register;
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);
$db = $register->get('db', true);
$redis = $register->get('cache', true);
$redis->flushAll();
$cache = new Cache(new RedisCache($redis));
$projectDB = new Database(new MariaDB($db), $cache);
$projectDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$consoleDB = new Database(new MariaDB($db), $cache);
$consoleDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$consoleDB->setNamespace('_project_console');
$console = $app->getResource('console');
$limit = 30;
$sum = 30;
$offset = 0;
$projects = [$console];
$count = 0;
try {
$totalProjects = $consoleDB->count('projects') + 1;
} catch (\Throwable $th) {
$consoleDB->setNamespace('_console');
$totalProjects = $consoleDB->count('projects') + 1;
}
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
$migration = new $class();
while (!empty($projects)) {
foreach ($projects as $project) {
try {
$migration
->setProject($project, $projectDB, $consoleDB)
->execute();
} catch (\Throwable $th) {
throw $th;
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
}
}
$sum = \count($projects);
$projects = $consoleDB->find('projects', limit: $limit, offset: $offset);
$offset = $offset + $limit;
$count = $count + $sum;
Console::log('Migrated ' . $count . '/' . $totalProjects . ' projects...');
}
Event::wait(); // Wait for Coroutines to finish
$redis->flushAll();
Console::success('Data Migration Completed');
}
2022-07-14 02:04:31 +00:00
}