fix: migration

This commit is contained in:
Torsten Dittmann 2021-12-27 18:38:51 +01:00
parent 447612b2d5
commit b16c4367d8

View file

@ -29,8 +29,7 @@ global $register;
class V11 extends Migration class V11 extends Migration
{ {
protected Database $dbInternal; protected Database $dbProject;
protected Database $dbExternal;
protected Database $dbConsole; protected Database $dbConsole;
protected array $oldCollections; protected array $oldCollections;
@ -43,10 +42,12 @@ class V11 extends Migration
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); // namespace is set on execution $this->dbProject = new Database(new MariaDB($this->db), $cacheAdapter); // namespace is set on execution
$this->dbExternal = new Database(new MariaDB($this->db), $cacheAdapter); // namespace is set on execution
$this->dbConsole = new Database(new MariaDB($this->db), $cacheAdapter); $this->dbConsole = new Database(new MariaDB($this->db), $cacheAdapter);
$this->dbConsole->setNamespace('project_console_internal');
$this->dbProject->setDefaultDatabase('appwrite');
$this->dbConsole->setDefaultDatabase('appwrite');
$this->dbConsole->setNamespace('_project_console');
} }
$this->newCollections = Config::getParam('collections', []); $this->newCollections = Config::getParam('collections', []);
@ -60,8 +61,7 @@ class V11 extends Migration
$oldProject = $this->project; $oldProject = $this->project;
$this->dbInternal->setNamespace('project_' . $oldProject->getId() . '_internal'); $this->dbProject->setNamespace('_project_' . $oldProject->getId());
$this->dbExternal->setNamespace('project_' . $oldProject->getId() . '_external');
Console::info(''); Console::info('');
Console::info('------------------------------------'); Console::info('------------------------------------');
@ -85,26 +85,18 @@ class V11 extends Migration
} }
/** /**
* Create internal DB tables * Create internal tables
*/ */
if (!$this->dbInternal->exists()) { if (!$this->dbProject->exists('appwrite')) {
$this->dbInternal->create(); $this->dbProject->create('appwrite');
Console::log('Created internal tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); Console::log('Created internal tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')');
} }
/**
* Create external DB tables
*/
if (!$this->dbExternal->exists()) {
$this->dbExternal->create();
Console::log('Created external tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')');
}
/** /**
* Create Audit tables * Create Audit tables
*/ */
if ($this->dbInternal->getCollection(Audit::COLLECTION)->isEmpty()) { if ($this->dbProject->getCollection(Audit::COLLECTION)->isEmpty()) {
$audit = new Audit($this->dbInternal); $audit = new Audit($this->dbProject);
$audit->setup(); $audit->setup();
Console::log('Created audit tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); Console::log('Created audit tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')');
} }
@ -112,8 +104,8 @@ class V11 extends Migration
/** /**
* Create Abuse tables * Create Abuse tables
*/ */
if ($this->dbInternal->getCollection(TimeLimit::COLLECTION)->isEmpty()) { if ($this->dbProject->getCollection(TimeLimit::COLLECTION)->isEmpty()) {
$adapter = new TimeLimit("", 0, 1, $this->dbInternal); $adapter = new TimeLimit("", 0, 1, $this->dbProject);
$adapter->setup(); $adapter->setup();
Console::log('Created abuse tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')'); Console::log('Created abuse tables for : ' . $project->getAttribute('name') . ' (' . $project->getId() . ')');
} }
@ -122,7 +114,7 @@ class V11 extends Migration
* Create internal collections for Project * Create internal collections for Project
*/ */
foreach ($this->newCollections as $key => $collection) { foreach ($this->newCollections as $key => $collection) {
if (!$this->dbInternal->getCollection($key)->isEmpty()) continue; // Skip if project collection already exists if (!$this->dbProject->getCollection($key)->isEmpty()) continue; // Skip if project collection already exists
$attributes = []; $attributes = [];
$indexes = []; $indexes = [];
@ -149,7 +141,7 @@ class V11 extends Migration
]); ]);
} }
$this->dbInternal->createCollection($key, $attributes, $indexes); $this->dbProject->createCollection($key, $attributes, $indexes);
} }
if ($this->options['migrateCollections']) { if ($this->options['migrateCollections']) {
$this->migrateExternalCollections(); $this->migrateExternalCollections();
@ -198,8 +190,8 @@ class V11 extends Migration
} }
try { try {
if ($this->dbInternal->getDocument($new->getCollection(), $new->getId())->isEmpty()) { if ($this->dbProject->getDocument($new->getCollection(), $new->getId())->isEmpty()) {
$this->dbInternal->createDocument($new->getCollection(), $new); $this->dbProject->createDocument($new->getCollection(), $new);
} }
} catch (\Throwable $th) { } catch (\Throwable $th) {
Console::error('Failed to update document: ' . $th->getMessage()); Console::error('Failed to update document: ' . $th->getMessage());
@ -249,10 +241,10 @@ class V11 extends Migration
$id = $oldCollection->getId(); $id = $oldCollection->getId();
$permissions = $oldCollection->getPermissions(); $permissions = $oldCollection->getPermissions();
$name = $oldCollection->getAttribute('name'); $name = $oldCollection->getAttribute('name');
$newCollection = $this->dbExternal->getCollection($id); $newCollection = $this->dbProject->getCollection('collection_' . $id);
if ($newCollection->isEmpty()) { if ($newCollection->isEmpty()) {
$this->dbExternal->createCollection($id); $this->dbProject->createCollection('collection_' . $id);
/** /**
* Migrate permissions * Migrate permissions
*/ */
@ -263,13 +255,13 @@ class V11 extends Migration
* Suffix collection name with a subsequent number to make it unique if possible. * Suffix collection name with a subsequent number to make it unique if possible.
*/ */
$suffix = 1; $suffix = 1;
while ($this->dbInternal->findOne('collections', [ while ($this->dbProject->findOne('collections', [
new Query('name', Query::TYPE_EQUAL, [$name]) new Query('name', Query::TYPE_EQUAL, [$name])
])) { ])) {
$name .= ' - ' . $suffix++; $name .= ' - ' . $suffix++;
} }
$this->dbInternal->createDocument('collections', new Document([ $this->dbProject->createDocument('collections', new Document([
'$id' => $id, '$id' => $id,
'$read' => [], '$read' => [],
'$write' => [], '$write' => [],
@ -290,7 +282,7 @@ class V11 extends Migration
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
try { try {
$this->dbExternal->createAttribute( $this->dbProject->createAttribute(
collection: $attribute['$collection'], collection: $attribute['$collection'],
id: $attribute['$id'], id: $attribute['$id'],
type: $attribute['type'], type: $attribute['type'],
@ -304,7 +296,7 @@ class V11 extends Migration
filters: $attribute['filters'] filters: $attribute['filters']
); );
$this->dbInternal->createDocument('attributes', new Document([ $this->dbProject->createDocument('attributes', new Document([
'$id' => $attribute['$collection'] . '_' . $attribute['$id'], '$id' => $attribute['$collection'] . '_' . $attribute['$id'],
'key' => $attribute['$id'], 'key' => $attribute['$id'],
'collectionId' => $attribute['$collection'], 'collectionId' => $attribute['$collection'],
@ -361,7 +353,7 @@ class V11 extends Migration
Console::log('Migrating External Documents for Collection ' . $collection . ': ' . $offset . ' / ' . $this->oldProjectDB->getSum()); 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->dbProject->getDocument('collection_' . $collection, $document->getId())->isEmpty()) {
continue; continue;
} }
go(function ($document) { go(function ($document) {
@ -398,7 +390,7 @@ class V11 extends Migration
}, $document); }, $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->dbProject->createDocument('collection_' . $collection, $document);
} }
$offset += $this->limit; $offset += $this->limit;
} }