Sycned migration script with 0.7 updates
This commit is contained in:
parent
a7a55821a1
commit
78b49cd045
1 changed files with 31 additions and 7 deletions
|
@ -7,6 +7,8 @@ use Utopia\CLI\Console;
|
||||||
use Appwrite\Database\Database;
|
use Appwrite\Database\Database;
|
||||||
use Appwrite\Database\Document;
|
use Appwrite\Database\Document;
|
||||||
use Appwrite\Database\Validator\Authorization;
|
use Appwrite\Database\Validator\Authorization;
|
||||||
|
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
|
||||||
|
use Appwrite\Database\Adapter\Redis as RedisAdapter;
|
||||||
|
|
||||||
$db = $register->get('db');
|
$db = $register->get('db');
|
||||||
|
|
||||||
|
@ -14,9 +16,9 @@ $callbacks = [
|
||||||
'0.4.0' => function() {
|
'0.4.0' => function() {
|
||||||
Console::log('I got nothing to do.');
|
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
|
// Update all documents $uid -> $id
|
||||||
|
|
||||||
|
@ -47,6 +49,7 @@ $callbacks = [
|
||||||
try {
|
try {
|
||||||
$new = $projectDB->overwriteDocument($document->getArrayCopy());
|
$new = $projectDB->overwriteDocument($document->getArrayCopy());
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
|
var_dump($document);
|
||||||
Console::error('Failed to update document: '.$th->getMessage());
|
Console::error('Failed to update document: '.$th->getMessage());
|
||||||
continue;
|
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) {
|
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_USERS) {
|
||||||
foreach($providers as $key => $provider) {
|
foreach($providers as $key => $provider) {
|
||||||
if(!empty($document->getAttribute('oauth'.\ucfirst($key)))) {
|
if(!empty($document->getAttribute('oauth'.\ucfirst($key)))) {
|
||||||
|
@ -166,26 +177,38 @@ function fixDocument(Document $document) {
|
||||||
|
|
||||||
$cli
|
$cli
|
||||||
->task('migrate')
|
->task('migrate')
|
||||||
->action(function () use ($console, $projectDB, $consoleDB, $callbacks) {
|
->action(function () use ($register, $callbacks) {
|
||||||
Console::success('Starting Data Migration');
|
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();
|
Authorization::disable();
|
||||||
|
|
||||||
$limit = 30;
|
$limit = 30;
|
||||||
$sum = 30;
|
$sum = 30;
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
$projects = [$console];
|
$projects = [$console];
|
||||||
|
$count = 0;
|
||||||
|
|
||||||
while ($sum >= 30) {
|
while ($sum >= 30) {
|
||||||
foreach($projects as $project) {
|
foreach($projects as $project) {
|
||||||
|
|
||||||
$projectDB->setNamespace('app_'.$project->getId());
|
$projectDB->setNamespace('app_'.$project->getId());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$callbacks['0.5.0']($project);
|
$callbacks['0.5.0']($project, $projectDB);
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
|
throw $th;
|
||||||
Console::error('Failed to update project ("'.$project->getId().'") version with error: '.$th->getMessage());
|
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);
|
$sum = \count($projects);
|
||||||
$offset = $offset + $limit;
|
$offset = $offset + $limit;
|
||||||
|
$count = $count + $sum;
|
||||||
|
|
||||||
Console::log('Fetched '.$sum.' projects...');
|
Console::log('Fetched '.$count.'/'.$consoleDB->getSum().' projects...');
|
||||||
}
|
}
|
||||||
|
|
||||||
Console::success('Data Migration Completed');
|
Console::success('Data Migration Completed');
|
||||||
|
|
Loading…
Reference in a new issue