1
0
Fork 0
mirror of synced 2024-07-06 07:00:56 +12:00
appwrite/app/tasks/migrate.php

138 lines
5.5 KiB
PHP
Raw Normal View History

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;
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-07-02 21:09:02 +12:00
use Appwrite\Migration\Migration;
2021-07-02 21:03:01 +12:00
use Utopia\Validator\Text;
2020-02-17 20:16:11 +13:00
2021-12-09 23:42:49 +13:00
Config::load('collections.old', __DIR__ . '/../config/collections.old.php');
2021-12-02 00:48:23 +13:00
2019-12-31 23:55:39 +13:00
$cli
2020-07-29 07:48:51 +12:00
->task('migrate')
2021-07-02 21:03:01 +12:00
->param('version', APP_VERSION_STABLE, new Text(8), 'Version to migrate to.', true)
->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 23:42:49 +13:00
$options = [];
2021-12-09 06:49:44 +13:00
if (str_starts_with($version, '0.12.')) {
2021-12-13 23:59:13 +13:00
Console::error('--------------------');
2021-12-09 06:49:44 +13:00
Console::error('WARNING');
2021-12-13 23:59:13 +13:00
Console::error('--------------------');
2021-12-09 06:49:44 +13:00
Console::warning('Migrating to Version 0.12.x introduces a major breaking change within the Database Service!');
Console::warning('Before migrating, please read about the breaking changes here:');
Console::info('https://appwrite.io/guide-to-db-migration');
$confirm = Console::confirm("If you want to proceed, type 'yes':");
2021-12-09 23:42:49 +13:00
if ($confirm != 'yes') {
Console::exit(1);
return;
}
Console::log('');
Console::log('Collections');
Console::log('--------------------');
Console::warning('Be aware that following actions will happen during the migration:');
Console::warning('- Nested Document rules will be migrated to String attributes');
Console::warning('- Numeric rules will be migrated to float attributes');
2021-12-13 23:59:13 +13:00
Console::warning('- Wildcard and Markdown rules will be converted to string attributes');
2021-12-09 23:42:49 +13:00
Console::info("Do you want to migrate your Database Collections?");
$options['migrateCollections'] = Console::confirm("Type 'yes' or 'no':");
if ($options['migrateCollections'] === 'yes') {
Console::log('');
Console::log('Documents');
Console::log('------------------');
Console::warning('Be aware that following actions will happen during the migration:');
Console::warning('- Nested Documents will be stored as JSON values');
Console::warning('- All Numeric values will be converted to float');
2021-12-13 23:59:13 +13:00
Console::warning('- All Wildcard and Markdown values will be converted to string');
2021-12-09 23:42:49 +13:00
Console::info("Do you want to migrate your Database Documents?");
$options['migrateDocuments'] = Console::confirm("Type 'yes' or 'no':");
} else {
$options['migrateDocuments'] = 'no';
}
if (
!in_array($options['migrateDocuments'], ['yes', 'no'])
|| !in_array($options['migrateCollections'], ['yes', 'no'])
) {
Console::error("You must reply with 'yes' or 'no'!");
2021-12-09 06:49:44 +13:00
Console::exit(1);
return;
}
}
2021-12-09 23:42:49 +13:00
Config::load('collectionsold', __DIR__ . '/../config/collections.old.php');
2021-12-09 06:49:44 +13:00
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);
$cache = $register->get('cache', true);
2021-12-09 06:49:44 +13:00
$cache->flushAll();
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
2021-12-09 06:49:44 +13:00
->setMocks(Config::getParam('collectionsold', []));
2021-01-14 05:51:02 +13:00
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-12-09 06:49:44 +13:00
->setMocks(Config::getParam('collectionsold', []));
2020-10-30 00:22:46 +13:00
$console = $consoleDB->getDocument('console');
2020-02-11 05:16:02 +13:00
$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-12-09 23:42:49 +13:00
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
$migration = new $class($register->get('db'), $register->get('cache'), $options);
2020-02-17 20:16:11 +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
2021-12-09 06:49:44 +13:00
->setProject($project, $projectDB, $consoleDB)
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
}
2020-02-11 05:16:02 +13:00
}
$projects = $consoleDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'filters' => [
2021-12-09 23:42:49 +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-08-31 00:25:23 +12:00
if ($sum > 0) {
2021-12-09 23:42:49 +13:00
Console::log('Fetched ' . $count . '/' . $consoleDB->getSum() . ' projects...');
}
2020-02-11 05:16:02 +13:00
}
2021-12-09 06:49:44 +13:00
$cache->flushAll();
2020-05-21 18:25:29 +12: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
});