2019-12-31 23:55:39 +13:00
|
|
|
<?php
|
|
|
|
|
2022-01-20 00:59:58 +13:00
|
|
|
global $cli, $register;
|
2019-12-31 23:55:39 +13:00
|
|
|
|
|
|
|
use Utopia\CLI\Console;
|
2021-07-02 21:09:02 +12:00
|
|
|
use Appwrite\Migration\Migration;
|
2022-01-19 00:05:04 +13:00
|
|
|
use Utopia\App;
|
|
|
|
use Utopia\Cache\Cache;
|
|
|
|
use Utopia\Cache\Adapter\Redis as RedisCache;
|
|
|
|
use Utopia\Database\Adapter\MariaDB;
|
|
|
|
use Utopia\Database\Database;
|
2022-11-18 00:26:14 +13:00
|
|
|
use Utopia\Database\Document;
|
2022-08-12 11:53:52 +12:00
|
|
|
use Utopia\Database\Query;
|
2022-01-19 00:05:04 +13:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2021-07-02 21:03:01 +12:00
|
|
|
use Utopia\Validator\Text;
|
2020-02-17 20:16:11 +13:00
|
|
|
|
2022-11-18 00:26:14 +13:00
|
|
|
function clearProjectsCache(Redis $redis, Document $project)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$redis->del($redis->keys("cache-_{$project->getInternalId()}:*"));
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
Console::error('Failed to clear project ("' . $project->getId() . '") cache with error: ' . $th->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 23:55:39 +13:00
|
|
|
$cli
|
2020-07-29 07:48:51 +12:00
|
|
|
->task('migrate')
|
2022-09-07 20:43:05 +12:00
|
|
|
->param('version', APP_VERSION_STABLE, new Text(32), 'Version to migrate to.', true)
|
2021-07-02 21:03:01 +12:00
|
|
|
->action(function ($version) use ($register) {
|
2021-12-09 06:49:44 +13:00
|
|
|
Authorization::disable();
|
2021-07-02 21:09:02 +12:00
|
|
|
if (!array_key_exists($version, Migration::$versions)) {
|
2021-07-02 21:03:01 +12:00
|
|
|
Console::error("Version {$version} not found.");
|
|
|
|
Console::exit(1);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-09 06:49:44 +13:00
|
|
|
|
2022-01-20 00:59:58 +13:00
|
|
|
$app = new App('UTC');
|
|
|
|
|
2021-12-09 23:42:49 +13:00
|
|
|
Console::success('Starting Data Migration to version ' . $version);
|
2021-12-09 06:49:44 +13:00
|
|
|
|
2021-07-02 01:35:54 +12:00
|
|
|
$db = $register->get('db', true);
|
2022-06-23 01:52:21 +12:00
|
|
|
$redis = $register->get('cache', true);
|
2022-11-18 00:26:14 +13:00
|
|
|
|
2022-06-23 01:52:21 +12:00
|
|
|
$cache = new Cache(new RedisCache($redis));
|
2021-01-14 05:51:02 +13:00
|
|
|
|
2022-01-19 00:05:04 +13:00
|
|
|
$projectDB = new Database(new MariaDB($db), $cache);
|
|
|
|
$projectDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
2020-10-30 00:22:46 +13:00
|
|
|
|
2022-01-19 00:05:04 +13:00
|
|
|
$consoleDB = new Database(new MariaDB($db), $cache);
|
|
|
|
$consoleDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
|
|
|
$consoleDB->setNamespace('_project_console');
|
|
|
|
|
2022-01-20 00:59:58 +13:00
|
|
|
$console = $app->getResource('console');
|
2020-10-30 00:22:46 +13:00
|
|
|
|
2020-02-11 05:16:02 +13:00
|
|
|
$limit = 30;
|
|
|
|
$sum = 30;
|
|
|
|
$offset = 0;
|
2022-09-10 05:50:05 +12:00
|
|
|
/**
|
|
|
|
* @var \Utopia\Database\Document[] $projects
|
|
|
|
*/
|
2020-02-11 05:16:02 +13:00
|
|
|
$projects = [$console];
|
2020-10-30 00:22:46 +13:00
|
|
|
$count = 0;
|
2022-02-18 07:24:50 +13:00
|
|
|
|
|
|
|
try {
|
|
|
|
$totalProjects = $consoleDB->count('projects') + 1;
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$consoleDB->setNamespace('_console');
|
|
|
|
$totalProjects = $consoleDB->count('projects') + 1;
|
|
|
|
}
|
2020-02-11 05:16:02 +13:00
|
|
|
|
2021-12-09 23:42:49 +13:00
|
|
|
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
|
2022-01-19 00:05:04 +13:00
|
|
|
$migration = new $class();
|
2020-02-17 20:16:11 +13:00
|
|
|
|
2022-01-20 01:03:11 +13:00
|
|
|
while (!empty($projects)) {
|
2021-01-14 05:51:02 +13:00
|
|
|
foreach ($projects as $project) {
|
2022-09-10 05:50:05 +12:00
|
|
|
/**
|
|
|
|
* Skip user projects with id 'console'
|
|
|
|
*/
|
|
|
|
if ($project->getId() === 'console' && $project->getInternalId() !== 'console') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:26:14 +13:00
|
|
|
clearProjectsCache($redis, $project);
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
try {
|
2021-01-14 05:51:02 +13:00
|
|
|
$migration
|
2021-12-09 06:49:44 +13:00
|
|
|
->setProject($project, $projectDB, $consoleDB)
|
2023-02-15 20:35:24 +13:00
|
|
|
->setPDO($register->get('db'))
|
2021-01-14 05:51:02 +13:00
|
|
|
->execute();
|
2020-02-17 20:16:11 +13:00
|
|
|
} catch (\Throwable $th) {
|
2020-10-30 00:22:46 +13:00
|
|
|
throw $th;
|
2021-12-09 23:42:49 +13:00
|
|
|
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
|
2020-02-17 20:16:11 +13:00
|
|
|
}
|
2022-11-18 00:26:14 +13:00
|
|
|
|
|
|
|
clearProjectsCache($redis, $project);
|
2020-02-11 05:16:02 +13:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$sum = \count($projects);
|
2022-08-12 11:53:52 +12:00
|
|
|
$projects = $consoleDB->find('projects', [Query::limit($limit), Query::offset($offset)]);
|
2022-01-20 00:59:58 +13:00
|
|
|
|
2020-02-11 05:16:02 +13:00
|
|
|
$offset = $offset + $limit;
|
2020-10-30 00:22:46 +13:00
|
|
|
$count = $count + $sum;
|
2021-08-31 00:25:23 +12:00
|
|
|
|
2022-01-20 01:03:11 +13:00
|
|
|
Console::log('Migrated ' . $count . '/' . $totalProjects . ' projects...');
|
2020-02-11 05:16:02 +13:00
|
|
|
}
|
2020-05-21 18:25:29 +12:00
|
|
|
|
2021-12-10 03:46:07 +13:00
|
|
|
Swoole\Event::wait(); // Wait for Coroutines to finish
|
2020-05-21 18:25:29 +12:00
|
|
|
Console::success('Data Migration Completed');
|
2021-01-14 05:51:02 +13:00
|
|
|
});
|