2019-12-31 23:55:39 +13:00
|
|
|
#!/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../init.php';
|
|
|
|
|
2020-06-29 08:45:36 +12:00
|
|
|
global $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\CLI;
|
|
|
|
use Utopia\CLI\Console;
|
2020-03-25 06:56:32 +13:00
|
|
|
use Appwrite\Database\Database;
|
|
|
|
use Appwrite\Database\Document;
|
|
|
|
use Appwrite\Database\Validator\Authorization;
|
2019-12-31 23:55:39 +13:00
|
|
|
|
|
|
|
$cli = new CLI();
|
|
|
|
$db = $register->get('db');
|
|
|
|
|
|
|
|
$callbacks = [
|
2020-02-11 05:16:02 +13:00
|
|
|
'0.4.0' => function() {
|
2019-12-31 23:55:39 +13:00
|
|
|
Console::log('I got nothing to do.');
|
|
|
|
},
|
2020-03-19 19:42:09 +13:00
|
|
|
'0.5.0' => function($project) use ($db, $projectDB, $requset) {
|
2019-12-31 23:55:39 +13:00
|
|
|
|
2020-05-21 19:00:06 +12:00
|
|
|
Console::log('Migrating project: '.$project->getId());
|
2019-12-31 23:55:39 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
// Update all documents $uid -> $id
|
2020-02-11 05:16:02 +13:00
|
|
|
|
|
|
|
$limit = 30;
|
|
|
|
$sum = 30;
|
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
while ($sum >= 30) {
|
2020-02-17 20:16:11 +13:00
|
|
|
$all = $projectDB->getCollection([
|
2020-02-11 05:16:02 +13:00
|
|
|
'limit' => $limit,
|
|
|
|
'offset' => $offset,
|
2020-02-17 20:16:11 +13:00
|
|
|
'orderField' => '$uid',
|
|
|
|
'orderType' => 'DESC',
|
2020-02-11 05:16:02 +13:00
|
|
|
'orderCast' => 'string',
|
|
|
|
]);
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$sum = \count($all);
|
2020-02-17 20:16:11 +13:00
|
|
|
|
2020-05-21 19:00:06 +12:00
|
|
|
Console::log('Migrating: '.$offset.' / '.$projectDB->getSum());
|
2019-12-31 23:55:39 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
foreach($all as $document) {
|
|
|
|
$document = fixDocument($document);
|
2020-02-11 05:16:02 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
if(empty($document->getId())) {
|
|
|
|
throw new Exception('Missing ID');
|
|
|
|
}
|
2020-02-11 05:16:02 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
try {
|
|
|
|
$new = $projectDB->overwriteDocument($document->getArrayCopy());
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
Console::error('Failed to update document: '.$th->getMessage());
|
|
|
|
continue;
|
2020-02-11 05:16:02 +13:00
|
|
|
}
|
2020-02-17 20:16:11 +13:00
|
|
|
|
|
|
|
if($new->getId() !== $document->getId()) {
|
|
|
|
throw new Exception('Duplication Error');
|
2020-02-11 05:16:02 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $offset + $limit;
|
2019-12-31 23:55:39 +13:00
|
|
|
}
|
2020-02-17 20:16:11 +13:00
|
|
|
|
2020-03-19 20:09:21 +13:00
|
|
|
$schema = (isset($_SERVER['_APP_DB_SCHEMA'])) ? $_SERVER['_APP_DB_SCHEMA'] : '';
|
2020-03-19 19:54:29 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
try {
|
|
|
|
$statement = $db->prepare("
|
2020-03-22 10:10:06 +13:00
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS `template.database.unique` (
|
|
|
|
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
|
`key` varchar(128) DEFAULT NULL,
|
|
|
|
PRIMARY KEY (`id`),
|
|
|
|
UNIQUE KEY `index1` (`key`)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS `{$schema}`.`app_{$project->getId()}.database.unique` LIKE `template.database.unique`;
|
2020-03-19 19:42:09 +13:00
|
|
|
ALTER TABLE `{$schema}`.`app_{$project->getId()}.audit.audit` DROP COLUMN IF EXISTS `userType`;
|
|
|
|
ALTER TABLE `{$schema}`.`app_{$project->getId()}.audit.audit` DROP INDEX IF EXISTS `index_1`;
|
|
|
|
ALTER TABLE `{$schema}`.`app_{$project->getId()}.audit.audit` ADD INDEX IF NOT EXISTS `index_1` (`userId` ASC);
|
2020-02-17 20:16:11 +13:00
|
|
|
");
|
|
|
|
|
|
|
|
$statement->closeCursor();
|
|
|
|
|
|
|
|
$statement->execute();
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
Console::error('Failed to alter table for project: '.$project->getId().' with message: '.$e->getMessage().'/');
|
|
|
|
}
|
2019-12-31 23:55:39 +13:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
function fixDocument(Document $document) {
|
2020-03-29 01:42:16 +13:00
|
|
|
$providers = Config::getParam('providers');
|
2020-02-17 20:16:11 +13:00
|
|
|
|
|
|
|
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_PROJECTS){
|
|
|
|
foreach($providers as $key => $provider) {
|
2020-06-20 23:20:49 +12:00
|
|
|
if(!empty($document->getAttribute('usersOauth'.\ucfirst($key).'Appid'))) {
|
2020-02-17 20:16:11 +13:00
|
|
|
$document
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('usersOauth2'.\ucfirst($key).'Appid', $document->getAttribute('usersOauth'.\ucfirst($key).'Appid', ''))
|
|
|
|
->removeAttribute('usersOauth'.\ucfirst($key).'Appid')
|
2020-02-17 20:16:11 +13:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
if(!empty($document->getAttribute('usersOauth'.\ucfirst($key).'Secret'))) {
|
2020-02-17 20:16:11 +13:00
|
|
|
$document
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('usersOauth2'.\ucfirst($key).'Secret', $document->getAttribute('usersOauth'.\ucfirst($key).'Secret', ''))
|
|
|
|
->removeAttribute('usersOauth'.\ucfirst($key).'Secret')
|
2020-02-17 20:16:11 +13:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_USERS) {
|
|
|
|
foreach($providers as $key => $provider) {
|
2020-06-20 23:20:49 +12:00
|
|
|
if(!empty($document->getAttribute('oauth'.\ucfirst($key)))) {
|
2020-02-17 20:16:11 +13:00
|
|
|
$document
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('oauth2'.\ucfirst($key), $document->getAttribute('oauth'.\ucfirst($key), ''))
|
|
|
|
->removeAttribute('oauth'.\ucfirst($key))
|
2020-02-17 20:16:11 +13:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
if(!empty($document->getAttribute('oauth'.\ucfirst($key).'AccessToken'))) {
|
2020-02-17 20:16:11 +13:00
|
|
|
$document
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('oauth2'.\ucfirst($key).'AccessToken', $document->getAttribute('oauth'.\ucfirst($key).'AccessToken', ''))
|
|
|
|
->removeAttribute('oauth'.\ucfirst($key).'AccessToken')
|
2020-02-17 20:16:11 +13:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($document->getAttribute('confirm', null) !== null) {
|
|
|
|
$document
|
|
|
|
->setAttribute('emailVerification', $document->getAttribute('confirm', $document->getAttribute('emailVerification', false)))
|
|
|
|
->removeAttribute('confirm')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 11:54:49 +13:00
|
|
|
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_PLATFORMS) {
|
|
|
|
if($document->getAttribute('url', null) !== null) {
|
|
|
|
$document
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('hostname', \parse_url($document->getAttribute('url', $document->getAttribute('hostname', '')), PHP_URL_HOST))
|
2020-03-05 11:54:49 +13:00
|
|
|
->removeAttribute('url')
|
|
|
|
;
|
|
|
|
}
|
2020-02-17 20:16:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
$document
|
2020-03-05 11:54:49 +13:00
|
|
|
->setAttribute('$id', $document->getAttribute('$uid', $document->getAttribute('$id')))
|
2020-02-17 20:16:11 +13:00
|
|
|
->removeAttribute('$uid')
|
|
|
|
;
|
|
|
|
|
2020-05-21 19:00:06 +12:00
|
|
|
//Console::log('Switched from $uid to $id: '.$document->getCollection().'/'.$document->getId());
|
2020-03-19 19:26:59 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
foreach($document as &$attr) {
|
|
|
|
if($attr instanceof Document) {
|
|
|
|
$attr = fixDocument($attr);
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
if(\is_array($attr)) {
|
2020-02-17 20:16:11 +13:00
|
|
|
foreach($attr as &$child) {
|
|
|
|
if($child instanceof Document) {
|
|
|
|
$child = fixDocument($child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $document;
|
|
|
|
}
|
|
|
|
|
2019-12-31 23:55:39 +13:00
|
|
|
$cli
|
|
|
|
->task('run')
|
2020-02-17 20:16:11 +13:00
|
|
|
->action(function () use ($console, $projectDB, $consoleDB, $callbacks) {
|
2020-05-21 18:25:29 +12:00
|
|
|
Console::success('Starting Data Migration');
|
2020-02-11 05:16:02 +13:00
|
|
|
|
|
|
|
Authorization::disable();
|
|
|
|
|
|
|
|
$limit = 30;
|
|
|
|
$sum = 30;
|
|
|
|
$offset = 0;
|
|
|
|
$projects = [$console];
|
|
|
|
|
|
|
|
while ($sum >= 30) {
|
|
|
|
foreach($projects as $project) {
|
2020-02-17 20:16:11 +13:00
|
|
|
$projectDB->setNamespace('app_'.$project->getId());
|
|
|
|
|
|
|
|
try {
|
|
|
|
$callbacks['0.5.0']($project);
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
Console::error('Failed to update project ("'.$project->getId().'") version with error: '.$th->getMessage());
|
|
|
|
$projectDB->setNamespace('app_console');
|
|
|
|
$projectDB->deleteDocument($project->getId());
|
|
|
|
}
|
2020-02-11 05:16:02 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
$projects = $consoleDB->getCollection([
|
|
|
|
'limit' => $limit,
|
|
|
|
'offset' => $offset,
|
|
|
|
'orderField' => 'name',
|
|
|
|
'orderType' => 'ASC',
|
|
|
|
'orderCast' => 'string',
|
|
|
|
'filters' => [
|
|
|
|
'$collection='.Database::SYSTEM_COLLECTION_PROJECTS,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$sum = \count($projects);
|
2020-02-11 05:16:02 +13:00
|
|
|
$offset = $offset + $limit;
|
|
|
|
|
2020-05-21 19:00:06 +12:00
|
|
|
Console::log('Fetched '.$sum.' projects...');
|
2020-02-11 05:16:02 +13:00
|
|
|
}
|
2020-05-21 18:25:29 +12:00
|
|
|
|
|
|
|
Console::success('Data Migration Completed');
|
2019-12-31 23:55:39 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
$cli->run();
|