Skip to content

Commit 262c1c2

Browse files
replaced static database db with a callback to create with dsn
1 parent d063f46 commit 262c1c2

1 file changed

Lines changed: 30 additions & 20 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class Appwrite extends Destination
5757
private Teams $teams;
5858
private Users $users;
5959

60+
/**
61+
* @var callable(string $databaseDSN): UtopiaDatabase
62+
*/
63+
protected mixed $getDatabaseDB;
64+
6065
/**
6166
* @var array<UtopiaDocument>
6267
*/
@@ -67,15 +72,15 @@ class Appwrite extends Destination
6772
* @param string $endpoint
6873
* @param string $key
6974
* @param UtopiaDatabase $dbForProject
70-
* @param UtopiaDatabase $dbForDatabase
75+
* @param callable(string $databaseDSN):UtopiaDatabase $getDatabaseDB
7176
* @param array<array<string, mixed>> $collectionStructure
7277
*/
7378
public function __construct(
7479
string $project,
7580
string $endpoint,
7681
string $key,
7782
protected UtopiaDatabase $dbForProject,
78-
protected UtopiaDatabase $dbForDatabase,
83+
callable $getDatabaseDB,
7984
protected array $collectionStructure
8085
) {
8186
$this->project = $project;
@@ -91,6 +96,8 @@ public function __construct(
9196
$this->storage = new Storage($this->client);
9297
$this->teams = new Teams($this->client);
9398
$this->users = new Users($this->client);
99+
100+
$this->getDatabaseDB = $getDatabaseDB;
94101
}
95102

96103
public static function getName(): string
@@ -230,7 +237,7 @@ protected function import(array $resources, callable $callback): void
230237
$isLast = $index === $total - 1;
231238

232239
try {
233-
$this->dbForDatabase->setPreserveDates(true);
240+
$this->dbForProject->setPreserveDates(true);
234241

235242
$responseResource = match ($resource->getGroup()) {
236243
Transfer::GROUP_DATABASES => $this->importDatabaseResource($resource, $isLast),
@@ -257,7 +264,7 @@ protected function import(array $resources, callable $callback): void
257264

258265
$responseResource = $resource;
259266
} finally {
260-
$this->dbForDatabase->setPreserveDates(false);
267+
$this->dbForProject->setPreserveDates(false);
261268
}
262269

263270
$this->cache->update($responseResource);
@@ -401,6 +408,8 @@ protected function createTable(Table $resource): bool
401408
);
402409
}
403410

411+
$dbForDatabase = call_user_func($this->getDatabaseDB, $database->getAttribute('database'));
412+
404413
$table = $this->dbForProject->createDocument('database_' . $database->getSequence(), new UtopiaDocument([
405414
'$id' => $resource->getId(),
406415
'databaseInternalId' => $database->getSequence(),
@@ -416,7 +425,7 @@ protected function createTable(Table $resource): bool
416425

417426
$resource->setSequence($table->getSequence());
418427

419-
$this->dbForDatabase->createCollection(
428+
$dbForDatabase->createCollection(
420429
'database_' . $database->getSequence() . '_collection_' . $resource->getSequence(),
421430
permissions: $resource->getPermissions(),
422431
documentSecurity: $resource->getRowSecurity()
@@ -521,7 +530,7 @@ protected function createColumn(Column $resource): bool
521530
);
522531
}
523532
}
524-
533+
$dbForDatabase = call_user_func($this->getDatabaseDB, $database->getAttribute('database'));
525534
try {
526535
$column = new UtopiaDocument([
527536
'$id' => ID::custom($database->getSequence() . '_' . $table->getSequence() . '_' . $resource->getKey()),
@@ -564,12 +573,12 @@ protected function createColumn(Column $resource): bool
564573
);
565574
} catch (\Throwable $e) {
566575
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
567-
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
576+
$dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
568577
throw $e;
569578
}
570579

571580
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
572-
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
581+
$dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
573582
$options = $resource->getOptions();
574583

575584
$twoWayKey = null;
@@ -624,15 +633,15 @@ protected function createColumn(Column $resource): bool
624633
);
625634
} catch (\Throwable $e) {
626635
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId());
627-
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence());
636+
$dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence());
628637
throw $e;
629638
}
630639
}
631640

632641
try {
633642
switch ($type) {
634643
case UtopiaDatabase::VAR_RELATIONSHIP:
635-
if (!$this->dbForDatabase->createRelationship(
644+
if (!$dbForDatabase->createRelationship(
636645
collection: 'database_' . $database->getSequence() . '_collection_' . $table->getSequence(),
637646
relatedCollection: 'database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence(),
638647
type: $options['relationType'],
@@ -650,7 +659,7 @@ protected function createColumn(Column $resource): bool
650659
}
651660
break;
652661
default:
653-
if (!$this->dbForDatabase->createAttribute(
662+
if (!$dbForDatabase->createAttribute(
654663
'database_' . $database->getSequence() . '_collection_' . $table->getSequence(),
655664
$resource->getKey(),
656665
$type,
@@ -686,7 +695,7 @@ protected function createColumn(Column $resource): bool
686695
}
687696

688697
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
689-
$this->dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
698+
$dbForDatabase->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
690699

691700
return true;
692701
}
@@ -722,13 +731,14 @@ protected function createIndex(Index $resource): bool
722731
message: 'Table not found',
723732
);
724733
}
734+
$dbForDatabase = call_user_func($this->getDatabaseDB, $database->getAttribute('database'));
725735

726736
$count = $this->dbForProject->count('indexes', [
727737
Query::equal('collectionInternalId', [$table->getSequence()]),
728738
Query::equal('databaseInternalId', [$database->getSequence()])
729-
], $this->dbForDatabase->getLimitForIndexes());
739+
], $dbForDatabase->getLimitForIndexes());
730740

731-
if ($count >= $this->dbForDatabase->getLimitForIndexes()) {
741+
if ($count >= $dbForDatabase->getLimitForIndexes()) {
732742
throw new Exception(
733743
resourceName: $resource->getName(),
734744
resourceGroup: $resource->getGroup(),
@@ -847,9 +857,9 @@ protected function createIndex(Index $resource): bool
847857

848858
$validator = new IndexValidator(
849859
$tableColumns,
850-
$this->dbForDatabase->getAdapter()->getMaxIndexLength(),
851-
$this->dbForDatabase->getAdapter()->getInternalIndexesKeys(),
852-
$this->dbForDatabase->getAdapter()->getSupportForIndexArray(),
860+
$dbForDatabase->getAdapter()->getMaxIndexLength(),
861+
$dbForDatabase->getAdapter()->getInternalIndexesKeys(),
862+
$dbForDatabase->getAdapter()->getSupportForIndexArray(),
853863
);
854864

855865
if (!$validator->isValid($index)) {
@@ -864,7 +874,7 @@ protected function createIndex(Index $resource): bool
864874
$index = $this->dbForProject->createDocument('indexes', $index);
865875

866876
try {
867-
$result = $this->dbForDatabase->createIndex(
877+
$result = $dbForDatabase->createIndex(
868878
'database_' . $database->getSequence() . '_collection_' . $table->getSequence(),
869879
$resource->getKey(),
870880
$resource->getType(),
@@ -981,8 +991,8 @@ protected function createRow(Row $resource, bool $isLast): bool
981991
}
982992
}
983993
}
984-
985-
$this->dbForDatabase->skipRelationshipsExistCheck(fn () => $this->dbForDatabase->createDocuments(
994+
$dbForDatabase = call_user_func($this->getDatabaseDB, $database->getAttribute('database'));
995+
$dbForDatabase->skipRelationshipsExistCheck(fn () => $dbForDatabase->createDocuments(
986996
'database_' . $databaseInternalId . '_collection_' . $tableInternalId,
987997
$this->rowBuffer
988998
));

0 commit comments

Comments
 (0)