1
0
Fork 0
mirror of synced 2024-06-28 11:10:46 +12:00

Sycned migration script with 0.7 updates

This commit is contained in:
Eldad Fux 2020-08-09 18:03:09 +03:00
parent a7a55821a1
commit 78b49cd045

View file

@ -7,6 +7,8 @@ use Utopia\CLI\Console;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\Authorization;
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
$db = $register->get('db');
@ -14,9 +16,9 @@ $callbacks = [
'0.4.0' => function() {
Console::log('I got nothing to do.');
},
'0.5.0' => function($project) use ($db, $projectDB, $requset) {
'0.5.0' => function(Document $project, $projectDB) use ($db) {
Console::log('Migrating project: '.$project->getId());
Console::log('Migrating project: '.$project->getAttribute('name').' ('.$project->getId().')');
// Update all documents $uid -> $id
@ -47,6 +49,7 @@ $callbacks = [
try {
$new = $projectDB->overwriteDocument($document->getArrayCopy());
} catch (\Throwable $th) {
var_dump($document);
Console::error('Failed to update document: '.$th->getMessage());
continue;
}
@ -108,6 +111,14 @@ function fixDocument(Document $document) {
}
}
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_WEBHOOKS){
$document->setAttribute('security', ($document->getAttribute('security')) ? true : false);
}
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_TASKS){
$document->setAttribute('security', ($document->getAttribute('security')) ? true : false);
}
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_USERS) {
foreach($providers as $key => $provider) {
if(!empty($document->getAttribute('oauth'.\ucfirst($key)))) {
@ -166,26 +177,38 @@ function fixDocument(Document $document) {
$cli
->task('migrate')
->action(function () use ($console, $projectDB, $consoleDB, $callbacks) {
->action(function () use ($register, $callbacks) {
Console::success('Starting Data Migration');
$consoleDB = new Database();
$consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
$consoleDB->setNamespace('app_console'); // Main DB
$consoleDB->setMocks(Config::getParam('collections', []));
$projectDB = new Database();
$projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
$projectDB->setMocks(Config::getParam('collections', []));
$console = $consoleDB->getDocument('console');
Authorization::disable();
$limit = 30;
$sum = 30;
$offset = 0;
$projects = [$console];
$count = 0;
while ($sum >= 30) {
foreach($projects as $project) {
$projectDB->setNamespace('app_'.$project->getId());
try {
$callbacks['0.5.0']($project);
$callbacks['0.5.0']($project, $projectDB);
} catch (\Throwable $th) {
throw $th;
Console::error('Failed to update project ("'.$project->getId().'") version with error: '.$th->getMessage());
$projectDB->setNamespace('app_console');
$projectDB->deleteDocument($project->getId());
}
}
@ -202,8 +225,9 @@ $cli
$sum = \count($projects);
$offset = $offset + $limit;
$count = $count + $sum;
Console::log('Fetched '.$sum.' projects...');
Console::log('Fetched '.$count.'/'.$consoleDB->getSum().' projects...');
}
Console::success('Data Migration Completed');