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