2022-07-13 07:02:55 +00:00
|
|
|
<?php
|
2022-07-14 02:04:31 +00:00
|
|
|
|
2022-11-14 10:01:41 +00:00
|
|
|
namespace Appwrite\Platform\Tasks;
|
2022-07-13 07:02:55 +00:00
|
|
|
|
|
|
|
|
use Appwrite\Migration\Migration;
|
2024-07-21 06:27:12 +00:00
|
|
|
use Redis;
|
2022-07-13 07:02:55 +00:00
|
|
|
use Utopia\App;
|
2024-03-06 17:34:21 +00:00
|
|
|
use Utopia\CLI\Console;
|
2023-08-16 00:37:16 +00:00
|
|
|
use Utopia\Database\Database;
|
2022-11-17 11:26:14 +00:00
|
|
|
use Utopia\Database\Document;
|
2023-08-16 00:37:16 +00:00
|
|
|
use Utopia\Database\Query;
|
2022-07-13 07:02:55 +00:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2024-10-01 14:30:47 +00:00
|
|
|
use Utopia\Http\Validator\Text;
|
2024-03-06 17:34:21 +00:00
|
|
|
use Utopia\Platform\Action;
|
2023-11-20 16:31:20 +00:00
|
|
|
use Utopia\Registry\Registry;
|
2024-07-15 09:17:10 +00:00
|
|
|
use Utopia\System\System;
|
2022-07-13 07:02:55 +00:00
|
|
|
|
2022-07-14 02:04:31 +00:00
|
|
|
class Migrate extends Action
|
|
|
|
|
{
|
2024-07-15 09:17:10 +00:00
|
|
|
protected Redis $redis;
|
|
|
|
|
|
2022-08-02 01:58:36 +00:00
|
|
|
public static function getName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'migrate';
|
|
|
|
|
}
|
2022-07-14 02:04:31 +00:00
|
|
|
|
2022-07-13 07:02:55 +00:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this
|
2022-08-02 01:58:36 +00:00
|
|
|
->desc('Migrate Appwrite to new version')
|
2022-11-17 14:56:10 +00:00
|
|
|
/** @TODO APP_VERSION_STABLE needs to be defined */
|
2022-07-13 07:02:55 +00:00
|
|
|
->param('version', APP_VERSION_STABLE, new Text(8), 'Version to migrate to.', true)
|
2024-10-01 14:30:47 +00:00
|
|
|
->inject('cache')
|
2023-08-16 00:37:16 +00:00
|
|
|
->inject('dbForConsole')
|
|
|
|
|
->inject('getProjectDB')
|
2023-11-20 16:31:20 +00:00
|
|
|
->inject('register')
|
2024-10-01 14:30:47 +00:00
|
|
|
->inject('authorization')
|
|
|
|
|
->inject('console')
|
|
|
|
|
->callback(function ($version, $dbForConsole, $getProjectDB, Registry $register, Authorization $authorization, Document $console) {
|
|
|
|
|
\Co\run(function () use ($version, $dbForConsole, $getProjectDB, $register, $authorization, $console) {
|
|
|
|
|
$this->action($version, $dbForConsole, $getProjectDB, $register, $authorization, $console);
|
2024-08-08 13:25:56 +00:00
|
|
|
});
|
|
|
|
|
});
|
2022-07-13 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 09:17:10 +00:00
|
|
|
private function clearProjectsCache(Document $project)
|
2022-11-17 14:56:10 +00:00
|
|
|
{
|
|
|
|
|
try {
|
2024-09-03 23:32:30 +00:00
|
|
|
$iterator = null;
|
|
|
|
|
do {
|
|
|
|
|
$pattern = "default-cache-_{$project->getInternalId()}:*";
|
|
|
|
|
$keys = $this->redis->scan($iterator, $pattern, 1000);
|
|
|
|
|
if ($keys !== false) {
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
|
$this->redis->del($key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while ($iterator > 0);
|
2022-11-17 14:56:10 +00:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
Console::error('Failed to clear project ("' . $project->getId() . '") cache with error: ' . $th->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 14:30:47 +00:00
|
|
|
public function action(string $version, Database $dbForConsole, callable $getProjectDB, Registry $register, Authorization $auth, Document $console)
|
2022-07-14 02:04:31 +00:00
|
|
|
{
|
2024-10-01 14:30:47 +00:00
|
|
|
$auth->disable();
|
2022-07-13 07:02:55 +00:00
|
|
|
if (!array_key_exists($version, Migration::$versions)) {
|
|
|
|
|
Console::error("Version {$version} not found.");
|
|
|
|
|
Console::exit(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 10:23:51 +00:00
|
|
|
$this->redis = new Redis();
|
|
|
|
|
$this->redis->connect(
|
|
|
|
|
System::getEnv('_APP_REDIS_HOST', null),
|
|
|
|
|
System::getEnv('_APP_REDIS_PORT', 6379),
|
|
|
|
|
3,
|
|
|
|
|
null,
|
|
|
|
|
10
|
|
|
|
|
);
|
2022-07-13 07:02:55 +00:00
|
|
|
|
|
|
|
|
Console::success('Starting Data Migration to version ' . $version);
|
|
|
|
|
|
|
|
|
|
$limit = 30;
|
|
|
|
|
$sum = 30;
|
|
|
|
|
$offset = 0;
|
2022-09-09 17:50:05 +00:00
|
|
|
/**
|
|
|
|
|
* @var \Utopia\Database\Document[] $projects
|
|
|
|
|
*/
|
2022-07-13 07:02:55 +00:00
|
|
|
$projects = [$console];
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
2022-07-15 20:58:15 +00:00
|
|
|
$totalProjects = $dbForConsole->count('projects') + 1;
|
2022-07-13 07:02:55 +00:00
|
|
|
} catch (\Throwable $th) {
|
2022-07-15 20:58:15 +00:00
|
|
|
$dbForConsole->setNamespace('_console');
|
|
|
|
|
$totalProjects = $dbForConsole->count('projects') + 1;
|
2022-07-13 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version];
|
2023-08-16 00:37:16 +00:00
|
|
|
/** @var Migration $migration */
|
2024-10-01 14:30:47 +00:00
|
|
|
$migration = new $class($auth, );
|
2022-07-13 07:02:55 +00:00
|
|
|
|
|
|
|
|
while (!empty($projects)) {
|
|
|
|
|
foreach ($projects as $project) {
|
2022-09-09 17:50:05 +00:00
|
|
|
/**
|
|
|
|
|
* Skip user projects with id 'console'
|
|
|
|
|
*/
|
|
|
|
|
if ($project->getId() === 'console' && $project->getInternalId() !== 'console') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 09:17:10 +00:00
|
|
|
$this->clearProjectsCache($project);
|
2022-11-17 11:26:14 +00:00
|
|
|
|
2022-07-13 07:02:55 +00:00
|
|
|
try {
|
2022-07-15 20:58:15 +00:00
|
|
|
// TODO: Iterate through all project DBs
|
2023-11-20 16:31:20 +00:00
|
|
|
/** @var Database $projectDB */
|
2023-08-16 00:37:16 +00:00
|
|
|
$projectDB = $getProjectDB($project);
|
2024-06-20 23:00:49 +00:00
|
|
|
$projectDB->disableValidation();
|
2022-07-13 07:02:55 +00:00
|
|
|
$migration
|
2022-07-15 20:58:15 +00:00
|
|
|
->setProject($project, $projectDB, $dbForConsole)
|
2023-11-20 16:31:20 +00:00
|
|
|
->setPDO($register->get('db', true))
|
2022-07-13 07:02:55 +00:00
|
|
|
->execute();
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
|
2023-04-11 11:42:06 +00:00
|
|
|
throw $th;
|
2022-07-13 07:02:55 +00:00
|
|
|
}
|
2022-11-17 11:26:14 +00:00
|
|
|
|
2024-07-15 09:17:10 +00:00
|
|
|
$this->clearProjectsCache($project);
|
2022-07-13 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sum = \count($projects);
|
2023-08-16 00:37:16 +00:00
|
|
|
$projects = $dbForConsole->find('projects', [Query::limit($limit), Query::offset($offset)]);
|
2022-07-13 07:02:55 +00:00
|
|
|
|
|
|
|
|
$offset = $offset + $limit;
|
|
|
|
|
$count = $count + $sum;
|
|
|
|
|
|
|
|
|
|
Console::log('Migrated ' . $count . '/' . $totalProjects . ' projects...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console::success('Data Migration Completed');
|
|
|
|
|
}
|
2022-07-14 02:04:31 +00:00
|
|
|
}
|