1
0
Fork 0
mirror of synced 2024-07-03 05:31:38 +12:00

add coroutine

This commit is contained in:
Torsten Dittmann 2021-12-09 13:51:29 +01:00
parent 9d47280d78
commit 04a3e0633d
2 changed files with 79 additions and 72 deletions

View file

@ -30,7 +30,7 @@ abstract class Migration
/** /**
* @var int * @var int
*/ */
protected int $limit = 50; protected int $limit = 500;
/** /**
* @var OldDocument * @var OldDocument

View file

@ -39,8 +39,8 @@ class V11 extends Migration
public function __construct(PDO $db, Redis $cache = null, array $options = []) public function __construct(PDO $db, Redis $cache = null, array $options = [])
{ {
parent::__construct($db, $cache, $options); parent::__construct($db, $cache, $options);
$this->options = array_map(fn($option) => $option === 'yes' ? true : false, $this->options); $this->options = array_map(fn ($option) => $option === 'yes' ? true : false, $this->options);
var_dump($this->options);
if (!is_null($cache)) { if (!is_null($cache)) {
$cacheAdapter = new Cache(new RedisCache($this->cache)); $cacheAdapter = new Cache(new RedisCache($this->cache));
$this->dbInternal = new Database(new MariaDB($this->db), $cacheAdapter); $this->dbInternal = new Database(new MariaDB($this->db), $cacheAdapter);
@ -58,16 +58,6 @@ class V11 extends Migration
Authorization::disable(); Authorization::disable();
Runtime::enableCoroutine(SWOOLE_HOOK_ALL); Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
/**
* Get a project to check if the version works with this migration.
*/
if (!str_starts_with($this->oldConsoleDB->getCollectionFirst([
'filters' => [
'$collection=' . OldDatabase::SYSTEM_COLLECTION_PROJECTS
]
])->getAttribute('version'), '0.11.')) {
throw new Exception("Can only migrate from version 0.11.x to 0.12.x");
}
$oldProject = $this->project; $oldProject = $this->project;
$this->dbInternal->setNamespace('project_' . $oldProject->getId() . '_internal'); $this->dbInternal->setNamespace('project_' . $oldProject->getId() . '_internal');
@ -175,11 +165,21 @@ class V11 extends Migration
'offset' => $offset, 'offset' => $offset,
'orderType' => 'DESC', 'orderType' => 'DESC',
'filters' => [ 'filters' => [
'$collection!=' . OldDatabase::SYSTEM_COLLECTION_COLLECTIONS, '$collection=' . OldDatabase::SYSTEM_COLLECTION_DOMAINS,
'$collection!=' . OldDatabase::SYSTEM_COLLECTION_RULES, '$collection=' . OldDatabase::SYSTEM_COLLECTION_EXECUTIONS,
'$collection!=' . OldDatabase::SYSTEM_COLLECTION_TASKS, '$collection=' . OldDatabase::SYSTEM_COLLECTION_FILES,
'$collection!=' . OldDatabase::SYSTEM_COLLECTION_PROJECTS, '$collection=' . OldDatabase::SYSTEM_COLLECTION_FUNCTIONS,
'$collection!=' . OldDatabase::SYSTEM_COLLECTION_CONNECTIONS, '$collection=' . OldDatabase::SYSTEM_COLLECTION_KEYS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_MEMBERSHIPS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_PLATFORMS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_SESSIONS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_TAGS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_TEAMS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_TOKENS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_USAGES,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_USERS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_WEBHOOKS,
'$collection=' . OldDatabase::SYSTEM_COLLECTION_CERTIFICATES,
] ]
]); ]);
@ -187,19 +187,19 @@ class V11 extends Migration
Console::log('Migrating Documents: ' . $offset . ' / ' . $this->oldProjectDB->getSum()); Console::log('Migrating Documents: ' . $offset . ' / ' . $this->oldProjectDB->getSum());
go(function ($all) {
foreach ($all as $document) { foreach ($all as $document) {
if ( if (
!array_key_exists($document->getCollection(), $this->oldCollections) !array_key_exists($document->getCollection(), $this->oldCollections)
) { ) {
continue; return;
} }
$old = $document->getArrayCopy();
$new = $this->fixDocument($document); $new = $this->fixDocument($document);
if (empty($new->getId())) { if (empty($new->getId())) {
Console::warning('Skipped Document due to missing ID.'); Console::warning('Skipped Document due to missing ID.');
continue; return;
} }
try { try {
@ -210,13 +210,14 @@ class V11 extends Migration
} }
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::error('Failed to update document: ' . $th->getMessage()); Console::error('Failed to update document: ' . $th->getMessage());
continue; return;
if ($document && $new->getId() !== $document->getId()) { if ($document && $new->getId() !== $document->getId()) {
throw new Exception('Duplication Error'); throw new Exception('Duplication Error');
} }
} }
} }
}, $all);
$offset += $this->limit; $offset += $this->limit;
} }
@ -326,7 +327,7 @@ class V11 extends Migration
Console::log('Created "' . $attribute['$id'] . '" attribute in collection: ' . $name); Console::log('Created "' . $attribute['$id'] . '" attribute in collection: ' . $name);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::log($th->getMessage() . ' - (' . $attribute['$id'] . '" attribute in collection ' . $name . ')'); Console::log($th->getMessage() . ' - ("' . $attribute['$id'] . '" attribute in collection ' . $name . ')');
} }
} }
if ($this->options['migrateDocuments']) { if ($this->options['migrateDocuments']) {
@ -349,6 +350,7 @@ class V11 extends Migration
{ {
$sum = $this->limit; $sum = $this->limit;
$offset = 0; $offset = 0;
while ($sum >= $this->limit) { while ($sum >= $this->limit) {
$allDocs = $this->oldProjectDB->getCollection([ $allDocs = $this->oldProjectDB->getCollection([
'limit' => $this->limit, 'limit' => $this->limit,
@ -360,10 +362,14 @@ class V11 extends Migration
]); ]);
$sum = \count($allDocs); $sum = \count($allDocs);
Console::log('Migrating External Documents for Collection ' . $collection . ': ' . $offset . ' / ' . $this->oldProjectDB->getSum());
foreach ($allDocs as $document) { foreach ($allDocs as $document) {
if (!$this->dbExternal->getDocument($collection, $document->getId())->isEmpty()) { if (!$this->dbExternal->getDocument($collection, $document->getId())->isEmpty()) {
continue; continue;
} }
go(function ($document) {
foreach ($document as $key => $attr) { foreach ($document as $key => $attr) {
/** /**
* Convert nested Document to JSON strings. * Convert nested Document to JSON strings.
@ -394,6 +400,7 @@ class V11 extends Migration
} }
} }
} }
}, $document);
$document = new Document($document->getArrayCopy()); $document = new Document($document->getArrayCopy());
$document = $this->migratePermissions($document); $document = $this->migratePermissions($document);
$this->dbExternal->createDocument($collection, $document); $this->dbExternal->createDocument($collection, $document);