Skip to content

Commit d063f46

Browse files
* Multi-type database support for appwrite destination
separated database object into dbForProject to interact with project metadata and dbForDatabase to work with the targetted database
1 parent bfdf3f3 commit d063f46

1 file changed

Lines changed: 47 additions & 45 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ class Appwrite extends Destination
6666
* @param string $project
6767
* @param string $endpoint
6868
* @param string $key
69-
* @param UtopiaDatabase $database
69+
* @param UtopiaDatabase $dbForProject
70+
* @param UtopiaDatabase $dbForDatabase
7071
* @param array<array<string, mixed>> $collectionStructure
7172
*/
7273
public function __construct(
7374
string $project,
7475
string $endpoint,
7576
string $key,
76-
protected UtopiaDatabase $database,
77+
protected UtopiaDatabase $dbForProject,
78+
protected UtopiaDatabase $dbForDatabase,
7779
protected array $collectionStructure
7880
) {
7981
$this->project = $project;
@@ -228,7 +230,7 @@ protected function import(array $resources, callable $callback): void
228230
$isLast = $index === $total - 1;
229231

230232
try {
231-
$this->database->setPreserveDates(true);
233+
$this->dbForDatabase->setPreserveDates(true);
232234

233235
$responseResource = match ($resource->getGroup()) {
234236
Transfer::GROUP_DATABASES => $this->importDatabaseResource($resource, $isLast),
@@ -255,7 +257,7 @@ protected function import(array $resources, callable $callback): void
255257

256258
$responseResource = $resource;
257259
} finally {
258-
$this->database->setPreserveDates(false);
260+
$this->dbForDatabase->setPreserveDates(false);
259261
}
260262

261263
$this->cache->update($responseResource);
@@ -329,7 +331,7 @@ protected function createDatabase(Database $resource): bool
329331
);
330332
}
331333

332-
$database = $this->database->createDocument('databases', new UtopiaDocument([
334+
$database = $this->dbForProject->createDocument('databases', new UtopiaDocument([
333335
'$id' => $resource->getId(),
334336
'name' => $resource->getDatabaseName(),
335337
'enabled' => $resource->getEnabled(),
@@ -353,7 +355,7 @@ protected function createDatabase(Database $resource): bool
353355
$this->collectionStructure['indexes']
354356
);
355357

356-
$this->database->createCollection(
358+
$this->dbForProject->createCollection(
357359
'database_' . $database->getSequence(),
358360
$columns,
359361
$indexes
@@ -385,7 +387,7 @@ protected function createTable(Table $resource): bool
385387
);
386388
}
387389

388-
$database = $this->database->getDocument(
390+
$database = $this->dbForProject->getDocument(
389391
'databases',
390392
$resource->getDatabase()->getId()
391393
);
@@ -399,7 +401,7 @@ protected function createTable(Table $resource): bool
399401
);
400402
}
401403

402-
$table = $this->database->createDocument('database_' . $database->getSequence(), new UtopiaDocument([
404+
$table = $this->dbForProject->createDocument('database_' . $database->getSequence(), new UtopiaDocument([
403405
'$id' => $resource->getId(),
404406
'databaseInternalId' => $database->getSequence(),
405407
'databaseId' => $resource->getDatabase()->getId(),
@@ -414,7 +416,7 @@ protected function createTable(Table $resource): bool
414416

415417
$resource->setSequence($table->getSequence());
416418

417-
$this->database->createCollection(
419+
$this->dbForDatabase->createCollection(
418420
'database_' . $database->getSequence() . '_collection_' . $resource->getSequence(),
419421
permissions: $resource->getPermissions(),
420422
documentSecurity: $resource->getRowSecurity()
@@ -447,7 +449,7 @@ protected function createColumn(Column $resource): bool
447449
default => throw new \Exception('Invalid resource type '.$resource->getType()),
448450
};
449451

450-
$database = $this->database->getDocument(
452+
$database = $this->dbForProject->getDocument(
451453
'databases',
452454
$resource->getTable()->getDatabase()->getId(),
453455
);
@@ -461,7 +463,7 @@ protected function createColumn(Column $resource): bool
461463
);
462464
}
463465

464-
$table = $this->database->getDocument(
466+
$table = $this->dbForProject->getDocument(
465467
'database_' . $database->getSequence(),
466468
$resource->getTable()->getId(),
467469
);
@@ -506,7 +508,7 @@ protected function createColumn(Column $resource): bool
506508

507509
if ($type === UtopiaDatabase::VAR_RELATIONSHIP) {
508510
$resource->getOptions()['side'] = UtopiaDatabase::RELATION_SIDE_PARENT;
509-
$relatedTable = $this->database->getDocument(
511+
$relatedTable = $this->dbForProject->getDocument(
510512
'database_' . $database->getSequence(),
511513
$resource->getOptions()['relatedCollection']
512514
);
@@ -543,9 +545,9 @@ protected function createColumn(Column $resource): bool
543545
'$updatedAt' => $resource->getUpdatedAt(),
544546
]);
545547

546-
$this->database->checkAttribute($table, $column);
548+
$this->dbForProject->checkAttribute($table, $column);
547549

548-
$column = $this->database->createDocument('attributes', $column);
550+
$column = $this->dbForProject->createDocument('attributes', $column);
549551
} catch (DuplicateException) {
550552
throw new Exception(
551553
resourceName: $resource->getName(),
@@ -561,13 +563,13 @@ protected function createColumn(Column $resource): bool
561563
message: 'Attribute limit exceeded',
562564
);
563565
} catch (\Throwable $e) {
564-
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
565-
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
566+
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
567+
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
566568
throw $e;
567569
}
568570

569-
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
570-
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
571+
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
572+
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
571573
$options = $resource->getOptions();
572574

573575
$twoWayKey = null;
@@ -601,9 +603,9 @@ protected function createColumn(Column $resource): bool
601603
'$updatedAt' => $resource->getUpdatedAt(),
602604
]);
603605

604-
$this->database->createDocument('attributes', $twoWayAttribute);
606+
$this->dbForProject->createDocument('attributes', $twoWayAttribute);
605607
} catch (DuplicateException) {
606-
$this->database->deleteDocument('attributes', $column->getId());
608+
$this->dbForProject->deleteDocument('attributes', $column->getId());
607609

608610
throw new Exception(
609611
resourceName: $resource->getName(),
@@ -612,7 +614,7 @@ protected function createColumn(Column $resource): bool
612614
message: 'Attribute already exists',
613615
);
614616
} catch (LimitException) {
615-
$this->database->deleteDocument('attributes', $column->getId());
617+
$this->dbForProject->deleteDocument('attributes', $column->getId());
616618

617619
throw new Exception(
618620
resourceName: $resource->getName(),
@@ -621,16 +623,16 @@ protected function createColumn(Column $resource): bool
621623
message: 'Column limit exceeded',
622624
);
623625
} catch (\Throwable $e) {
624-
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId());
625-
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence());
626+
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId());
627+
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence());
626628
throw $e;
627629
}
628630
}
629631

630632
try {
631633
switch ($type) {
632634
case UtopiaDatabase::VAR_RELATIONSHIP:
633-
if (!$this->database->createRelationship(
635+
if (!$this->dbForDatabase->createRelationship(
634636
collection: 'database_' . $database->getSequence() . '_collection_' . $table->getSequence(),
635637
relatedCollection: 'database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence(),
636638
type: $options['relationType'],
@@ -648,7 +650,7 @@ protected function createColumn(Column $resource): bool
648650
}
649651
break;
650652
default:
651-
if (!$this->database->createAttribute(
653+
if (!$this->dbForDatabase->createAttribute(
652654
'database_' . $database->getSequence() . '_collection_' . $table->getSequence(),
653655
$resource->getKey(),
654656
$type,
@@ -665,10 +667,10 @@ protected function createColumn(Column $resource): bool
665667
}
666668
}
667669
} catch (\Throwable) {
668-
$this->database->deleteDocument('attributes', $column->getId());
670+
$this->dbForProject->deleteDocument('attributes', $column->getId());
669671

670672
if (isset($twoWayAttribute)) {
671-
$this->database->deleteDocument('attributes', $twoWayAttribute->getId());
673+
$this->dbForProject->deleteDocument('attributes', $twoWayAttribute->getId());
672674
}
673675

674676
throw new Exception(
@@ -680,11 +682,11 @@ protected function createColumn(Column $resource): bool
680682
}
681683

682684
if ($type === UtopiaDatabase::VAR_RELATIONSHIP && $options['twoWay']) {
683-
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId());
685+
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId());
684686
}
685687

686-
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
687-
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
688+
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
689+
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
688690

689691
return true;
690692
}
@@ -695,7 +697,7 @@ protected function createColumn(Column $resource): bool
695697
*/
696698
protected function createIndex(Index $resource): bool
697699
{
698-
$database = $this->database->getDocument(
700+
$database = $this->dbForProject->getDocument(
699701
'databases',
700702
$resource->getTable()->getDatabase()->getId(),
701703
);
@@ -708,7 +710,7 @@ protected function createIndex(Index $resource): bool
708710
);
709711
}
710712

711-
$table = $this->database->getDocument(
713+
$table = $this->dbForProject->getDocument(
712714
'database_' . $database->getSequence(),
713715
$resource->getTable()->getId(),
714716
);
@@ -721,12 +723,12 @@ protected function createIndex(Index $resource): bool
721723
);
722724
}
723725

724-
$count = $this->database->count('indexes', [
726+
$count = $this->dbForProject->count('indexes', [
725727
Query::equal('collectionInternalId', [$table->getSequence()]),
726728
Query::equal('databaseInternalId', [$database->getSequence()])
727-
], $this->database->getLimitForIndexes());
729+
], $this->dbForDatabase->getLimitForIndexes());
728730

729-
if ($count >= $this->database->getLimitForIndexes()) {
731+
if ($count >= $this->dbForDatabase->getLimitForIndexes()) {
730732
throw new Exception(
731733
resourceName: $resource->getName(),
732734
resourceGroup: $resource->getGroup(),
@@ -845,9 +847,9 @@ protected function createIndex(Index $resource): bool
845847

846848
$validator = new IndexValidator(
847849
$tableColumns,
848-
$this->database->getAdapter()->getMaxIndexLength(),
849-
$this->database->getAdapter()->getInternalIndexesKeys(),
850-
$this->database->getAdapter()->getSupportForIndexArray(),
850+
$this->dbForDatabase->getAdapter()->getMaxIndexLength(),
851+
$this->dbForDatabase->getAdapter()->getInternalIndexesKeys(),
852+
$this->dbForDatabase->getAdapter()->getSupportForIndexArray(),
851853
);
852854

853855
if (!$validator->isValid($index)) {
@@ -859,10 +861,10 @@ protected function createIndex(Index $resource): bool
859861
);
860862
}
861863

862-
$index = $this->database->createDocument('indexes', $index);
864+
$index = $this->dbForProject->createDocument('indexes', $index);
863865

864866
try {
865-
$result = $this->database->createIndex(
867+
$result = $this->dbForDatabase->createIndex(
866868
'database_' . $database->getSequence() . '_collection_' . $table->getSequence(),
867869
$resource->getKey(),
868870
$resource->getType(),
@@ -880,7 +882,7 @@ protected function createIndex(Index $resource): bool
880882
);
881883
}
882884
} catch (\Throwable $th) {
883-
$this->database->deleteDocument('indexes', $index->getId());
885+
$this->dbForProject->deleteDocument('indexes', $index->getId());
884886

885887
throw new Exception(
886888
resourceName: $resource->getName(),
@@ -890,7 +892,7 @@ protected function createIndex(Index $resource): bool
890892
);
891893
}
892894

893-
$this->database->purgeCachedDocument(
895+
$this->dbForProject->purgeCachedDocument(
894896
'database_' . $database->getSequence(),
895897
$table->getId()
896898
);
@@ -942,12 +944,12 @@ protected function createRow(Row $resource, bool $isLast): bool
942944

943945
if ($isLast) {
944946
try {
945-
$database = $this->database->getDocument(
947+
$database = $this->dbForProject->getDocument(
946948
'databases',
947949
$resource->getTable()->getDatabase()->getId(),
948950
);
949951

950-
$table = $this->database->getDocument(
952+
$table = $this->dbForProject->getDocument(
951953
'database_' . $database->getSequence(),
952954
$resource->getTable()->getId(),
953955
);
@@ -980,7 +982,7 @@ protected function createRow(Row $resource, bool $isLast): bool
980982
}
981983
}
982984

983-
$this->database->skipRelationshipsExistCheck(fn () => $this->database->createDocuments(
985+
$this->dbForDatabase->skipRelationshipsExistCheck(fn () => $this->dbForDatabase->createDocuments(
984986
'database_' . $databaseInternalId . '_collection_' . $tableInternalId,
985987
$this->rowBuffer
986988
));

0 commit comments

Comments
 (0)