diff --git a/app/tasks/upgrade.php b/app/tasks/upgrade.php index 7962069854..e9e9363b84 100644 --- a/app/tasks/upgrade.php +++ b/app/tasks/upgrade.php @@ -3,8 +3,10 @@ require_once __DIR__.'/../init.php'; -global $register; +global $register, $projectDB, $console; +use Database\Database; +use Database\Validator\Authorization; use Utopia\CLI\CLI; use Utopia\CLI\Console; @@ -12,59 +14,102 @@ $cli = new CLI(); $db = $register->get('db'); $callbacks = [ - '1.0.1' => function() { + '0.4.0' => function() { Console::log('I got nothing to do.'); }, - '1.0.2' => function($tables) use ($db) { + '0.5.0' => function($project) use ($db, $projectDB) { - foreach($tables as $node) { - $table = $node['table']; - $project = $node['project']; - $namespace = $node['namespace']; - $name = $node['name']; + Console::info('Altering table for project: '.$project->getUid()); + + try { + $statement = $db->prepare(" + ALTER TABLE `appwrite`.`app_{$project->getUid()}.audit.audit` DROP COLUMN IF EXISTS `userType`; + ALTER TABLE `appwrite`.`app_{$project->getUid()}.audit.audit` DROP INDEX IF EXISTS `index_1`; + ALTER TABLE `appwrite`.`app_{$project->getUid()}.audit.audit` ADD INDEX IF NOT EXISTS `index_1` (`userId` ASC); + "); + + $statement->execute(); + } + catch (\Exception $e) { + Console::error($e->getMessage().'/'); + } + + $statement->closeCursor(); + + $limit = 30; + $sum = 30; + $offset = 0; + + while ($sum >= 30) { + $users = $projectDB->getCollection([ + 'limit' => $limit, + 'offset' => $offset, + 'orderField' => 'name', + 'orderType' => 'ASC', + 'orderCast' => 'string', + 'filters' => [ + '$collection='.Database::SYSTEM_COLLECTION_USERS, + ], + ]); + + $sum = count($users); - if (($namespace !== 'audit') || ($name !== 'audit')) { - continue; + Console::success('Fetched '.$sum.' users...'); + + foreach($users as $user) { + $user + ->setAttribute('emailVerification', $user->getAttribute('confirm', false)) + ->removeAttribute('confirm') + ; + + if(!$projectDB->updateDocument($user->getArrayCopy())) { + Console::error('Failed to update user'); + } + else { + Console::success('Updated user succefully'); + } } - Console::info('Altering table: '.$table); - - try { - $statement = $db->prepare(" - ALTER TABLE `appwrite`.`{$project}.audit.audit` DROP COLUMN IF EXISTS `userType`; - ALTER TABLE `appwrite`.`{$project}.audit.audit` DROP INDEX IF EXISTS `index_1`; - ALTER TABLE `appwrite`.`{$project}.audit.audit` ADD INDEX IF NOT EXISTS `index_1` (`userId` ASC); - "); - - $statement->execute(); - } - catch (\Exception $e) { - Console::error($e->getMessage().'/'); - } - + $offset = $offset + $limit; } }, ]; $cli ->task('run') - ->action(function () use ($db, $callbacks) { + ->action(function () use ($console, $db, $projectDB, $consoleDB, $callbacks) { Console::success('Starting Upgrade'); - - $statment = $db->query('SELECT table_name FROM information_schema.tables WHERE table_schema = "appwrite";'); - - $tables = array_map(function($node) { - $name = explode('.', $node['table_name']); - return [ - 'table' => implode('.', $name), - 'project' => array_shift($name), - 'namespace' => array_shift($name), - 'name' => array_shift($name), - ]; - }, $statment->fetchAll()); - - $callbacks['1.0.2']($tables); + Authorization::disable(); + + $limit = 30; + $sum = 30; + $offset = 0; + $projects = [$console]; + + while ($sum >= 30) { + foreach($projects as $project) { + $projectDB->setNamespace('app_'.$project->getUid()); + + $callbacks['0.5.0']($project); + } + + $projects = $consoleDB->getCollection([ + 'limit' => $limit, + 'offset' => $offset, + 'orderField' => 'name', + 'orderType' => 'ASC', + 'orderCast' => 'string', + 'filters' => [ + '$collection='.Database::SYSTEM_COLLECTION_PROJECTS, + ], + ]); + + $sum = count($projects); + $offset = $offset + $limit; + + Console::success('Fetched '.$sum.' projects...'); + } }); $cli->run();