1
0
Fork 0
mirror of synced 2024-05-20 12:42:39 +12:00

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