appwrite/app/tasks/migrate.php

83 lines
2.6 KiB
PHP
Raw Normal View History

2019-12-31 10:55:39 +00:00
<?php
2020-07-28 19:48:51 +00:00
global $cli, $register, $projectDB, $console;
2019-12-31 10:55:39 +00:00
2020-03-28 12:42:16 +00:00
use Utopia\Config\Config;
2019-12-31 10:55:39 +00:00
use Utopia\CLI\Console;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\Authorization;
2020-10-29 11:22:46 +00:00
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
2021-07-02 09:09:02 +00:00
use Appwrite\Migration\Migration;
2021-07-02 09:03:01 +00:00
use Utopia\Validator\Text;
2020-02-17 07:16:11 +00:00
2019-12-31 10:55:39 +00:00
$cli
2020-07-28 19:48:51 +00:00
->task('migrate')
2021-07-02 09:03:01 +00:00
->param('version', APP_VERSION_STABLE, new Text(8), 'Version to migrate to.', true)
->action(function ($version) use ($register) {
2021-07-02 09:09:02 +00:00
if (!array_key_exists($version, Migration::$versions)) {
2021-07-02 09:03:01 +00:00
Console::error("Version {$version} not found.");
Console::exit(1);
return;
}
Console::success('Starting Data Migration to version '.$version);
2021-07-01 13:35:54 +00:00
$db = $register->get('db', true);
$cache = $register->get('cache', true);
2020-02-10 16:16:02 +00:00
2020-10-29 11:22:46 +00:00
$consoleDB = new Database();
2021-01-13 16:51:02 +00:00
$consoleDB
2021-07-01 13:35:54 +00:00
->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache))
2021-01-13 16:51:02 +00:00
->setNamespace('app_console') // Main DB
->setMocks(Config::getParam('collections', []));
2020-10-29 11:22:46 +00:00
$projectDB = new Database();
2021-01-13 16:51:02 +00:00
$projectDB
2021-07-01 13:35:54 +00:00
->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache))
2021-01-13 16:51:02 +00:00
->setMocks(Config::getParam('collections', []));
2020-10-29 11:22:46 +00:00
$console = $consoleDB->getDocument('console');
2020-02-10 16:16:02 +00:00
Authorization::disable();
$limit = 30;
$sum = 30;
$offset = 0;
$projects = [$console];
2020-10-29 11:22:46 +00:00
$count = 0;
2020-02-10 16:16:02 +00:00
2021-07-02 09:09:02 +00:00
$class = 'Appwrite\\Migration\\Version\\'.Migration::$versions[$version];
2021-07-02 09:03:01 +00:00
$migration = new $class($register->get('db'));
2020-02-17 07:16:11 +00:00
while ($sum > 0) {
2021-01-13 16:51:02 +00:00
foreach ($projects as $project) {
2020-02-17 07:16:11 +00:00
try {
2021-01-13 16:51:02 +00:00
$migration
->setProject($project, $projectDB)
->execute();
2020-02-17 07:16:11 +00:00
} catch (\Throwable $th) {
2020-10-29 11:22:46 +00:00
throw $th;
Console::error('Failed to update project ("'.$project->getId().'") version with error: '.$th->getMessage());
2020-02-17 07:16:11 +00:00
}
2020-02-10 16:16:02 +00:00
}
$projects = $consoleDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_PROJECTS,
2020-02-10 16:16:02 +00:00
],
]);
2020-06-20 11:20:49 +00:00
$sum = \count($projects);
2020-02-10 16:16:02 +00:00
$offset = $offset + $limit;
2020-10-29 11:22:46 +00:00
$count = $count + $sum;
2021-08-30 12:25:23 +00:00
if ($sum > 0) {
Console::log('Fetched '.$count.'/'.$consoleDB->getSum().' projects...');
}
2020-02-10 16:16:02 +00:00
}
2020-05-21 06:25:29 +00:00
Console::success('Data Migration Completed');
2021-01-13 16:51:02 +00:00
});