Skip to content

Commit 8bc1eb8

Browse files
saving before leaving
1 parent 8ddf9a2 commit 8bc1eb8

4 files changed

Lines changed: 41 additions & 60 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,13 +979,13 @@ protected function createRecord(Row $resource, bool $isLast): bool
979979
* This is in case an attribute was deleted from Appwrite attributes collection but was not deleted from the table
980980
* When creating an archive we select * which will include orphan attribute from the schema
981981
*/
982-
if($dbForDatabase->getAdapter()->getSupportForAttributes()){
982+
if ($dbForDatabase->getAdapter()->getSupportForAttributes()) {
983983
foreach ($this->rowBuffer as $row) {
984984
foreach ($row as $key => $value) {
985985
if (\str_starts_with($key, '$')) {
986986
continue;
987987
}
988-
988+
989989
/** @var \Utopia\Database\Document $attribute */
990990
$found = false;
991991
foreach ($table->getAttribute('attributes', []) as $attribute) {
@@ -994,7 +994,7 @@ protected function createRecord(Row $resource, bool $isLast): bool
994994
break;
995995
}
996996
}
997-
997+
998998
if (! $found) {
999999
$row->removeAttribute($key);
10001000
}

src/Migration/Resource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class Resource implements \JsonSerializable
3131
public const TYPE_DATABASE = 'database';
3232

3333
public const TYPE_DATABASE_LEGACY = 'legacy';
34-
34+
3535
public const TYPE_DATABASE_TABLESDB = 'tablesdb';
3636

3737
public const TYPE_DOCUMENTSDB_DATABASE = 'documentsdb';

src/Migration/Sources/Appwrite.php

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,16 @@ public static function getName(): string
114114
}
115115

116116
/**
117-
* Create a reader instance for a specific database
117+
* Create a reader instance for general operations (not database-specific)
118118
*
119-
* @param Database|null $database
120119
* @return Reader
121120
* @throws \Exception
122121
*/
123-
private function createReaderForDatabase(Database|null $database): Reader
122+
private function createReader(): Reader
124123
{
125124
return match ($this->source) {
126125
static::SOURCE_API => new APIReader(new Databases($this->client)),
127-
static::SOURCE_DATABASE => new DatabaseReader(
128-
call_user_func($this->getDatabaseDB, new UtopiaDocument(['database' => $database]))
129-
),
126+
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabaseDB),
130127
default => throw new \Exception('Unknown source'),
131128
};
132129
}
@@ -296,14 +293,7 @@ private function reportAuth(array $resources, array &$report): void
296293
*/
297294
private function reportDatabases(array $resources, array &$report): void
298295
{
299-
// For reporting, we use a temporary reader with dbForProject for SOURCE_DATABASE
300-
// or the API reader for SOURCE_API
301-
$reader = match ($this->source) {
302-
static::SOURCE_API => new APIReader(new Databases($this->client)),
303-
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabaseDB),
304-
default => throw new \Exception('Unknown source'),
305-
};
306-
296+
$reader = $this->createReader();
307297
$reader->report($resources, $report);
308298
}
309299

@@ -660,11 +650,11 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
660650
return;
661651
}
662652

663-
if(count($this->cache->get(Database::getName()))>0){
653+
if (count($this->cache->get(Database::getName())) > 0) {
664654
$this->exportTablesDB($batchSize, $resources);
665655
}
666656

667-
if(count($this->cache->get(DocumentsDB::getName()))>0){
657+
if (count($this->cache->get(DocumentsDB::getName())) > 0) {
668658
$this->exportDocumentsDB($batchSize, $resources);
669659
}
670660
}
@@ -679,11 +669,7 @@ private function exportDatabases(int $batchSize, array $resources = []): void
679669
$lastDatabase = null;
680670

681671
// Create a reader for listing databases (not database-specific)
682-
$reader = match ($this->source) {
683-
static::SOURCE_API => new APIReader(new Databases($this->client)),
684-
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabaseDB),
685-
default => throw new \Exception('Unknown source'),
686-
};
672+
$reader = $this->createReader();
687673

688674
while (true) {
689675
$queries = [$reader->queryLimit($batchSize)];
@@ -712,17 +698,23 @@ private function exportDatabases(int $batchSize, array $resources = []): void
712698
$response = $reader->listDatabases($queries);
713699

714700
foreach ($response as $database) {
715-
$databaseType = $database['type'] ?? Resource::TYPE_DATABASE;
716-
717-
$newDatabase = self::getDatabase($databaseType, [
718-
'id' => $database['$id'],
719-
'name' => $database['name'],
720-
'createdAt' => $database['$createdAt'],
721-
'updatedAt' => $database['$updatedAt'],
722-
'type' => $databaseType,
723-
'database' => $database['database']
724-
]);
725-
$databases[] = $newDatabase;
701+
$databaseType = $database['type'];
702+
if ($databaseType === Resource::TYPE_DATABASE_LEGACY || $databaseType === Resource::TYPE_DATABASE_TABLESDB) {
703+
$databaseType = Resource::TYPE_DATABASE;
704+
}
705+
706+
if (Resource::isSupported($databaseType, $resources)) {
707+
$newDatabase = self::getDatabase($databaseType, [
708+
'id' => $database['$id'],
709+
'name' => $database['name'],
710+
'createdAt' => $database['$createdAt'],
711+
'updatedAt' => $database['$updatedAt'],
712+
'type' => $databaseType,
713+
'database' => $database['database']
714+
]);
715+
$databases[] = $newDatabase;
716+
717+
}
726718
}
727719

728720
if (empty($databases)) {
@@ -752,12 +744,8 @@ private function exportEntities(string $databaseName, int $batchSize): void
752744
/** @var Database $database */
753745
$lastTable = null;
754746

755-
// colections in the metadata
756-
$reader = match ($this->source) {
757-
static::SOURCE_API => new APIReader(new Databases($this->client)),
758-
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabaseDB),
759-
default => throw new \Exception('Unknown source'),
760-
};
747+
// collections in the metadata
748+
$reader = $this->createReader();
761749

762750
while (true) {
763751
$queries = [$reader->queryLimit($batchSize)];
@@ -833,11 +821,7 @@ private function exportFields(int $batchSize): void
833821
foreach ($tables as $table) {
834822
$lastColumn = null;
835823

836-
$reader = match ($this->source) {
837-
static::SOURCE_API => new APIReader(new Databases($this->client)),
838-
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabaseDB),
839-
default => throw new \Exception('Unknown source'),
840-
};
824+
$reader = $this->createReader();
841825

842826
while (true) {
843827
$queries = [$reader->queryLimit($batchSize)];
@@ -1049,11 +1033,7 @@ private function exportIndexes(string $entityType, int $batchSize): void
10491033
$lastIndex = null;
10501034

10511035
// Create reader for this specific database
1052-
$reader = match ($this->source) {
1053-
static::SOURCE_API => new APIReader(new Databases($this->client)),
1054-
static::SOURCE_DATABASE => new DatabaseReader($this->dbForProject, $this->getDatabaseDB),
1055-
default => throw new \Exception('Unknown source'),
1056-
};
1036+
$reader = $this->createReader();
10571037

10581038
while (true) {
10591039
$queries = [$reader->queryLimit($batchSize)];
@@ -1110,7 +1090,8 @@ private function exportRecords(string $entityName, int $batchSize): void
11101090
$reader = match ($this->source) {
11111091
static::SOURCE_API => new APIReader(new Databases($this->client)),
11121092
static::SOURCE_DATABASE => new DatabaseReader(
1113-
$this->dbForProject,$this->getDatabaseDB
1093+
$this->dbForProject,
1094+
$this->getDatabaseDB
11141095
),
11151096
default => throw new \Exception('Unknown source'),
11161097
};
@@ -1710,7 +1691,7 @@ private function exportTablesDB(int $batchSize, array $resources)
17101691

17111692
/**
17121693
* Export DocumentsDB databases (collections and documents)
1713-
*
1694+
*
17141695
* @param int $batchSize
17151696
* @param array $resources
17161697
* @param array $documentsDBDatabases
@@ -1737,7 +1718,7 @@ private function exportDocumentsDB(int $batchSize, array $resources): void
17371718

17381719
try {
17391720
if (\in_array(Resource::TYPE_INDEX, $resources)) {
1740-
$this->exportIndexes(Collection::getName(),$batchSize);
1721+
$this->exportIndexes(Collection::getName(), $batchSize);
17411722
}
17421723
} catch (\Throwable $e) {
17431724
$this->addError(

src/Migration/Sources/Appwrite/Reader/Database.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public function report(array $resources, array &$report): mixed
7575
$dbResources = [];
7676
foreach ($databases as $database) {
7777
$databaseType = $database->getAttribute('type');
78-
if($databaseType === Resource::TYPE_DATABASE_LEGACY || $databaseType === Resource::TYPE_DATABASE_TABLESDB){
78+
if ($databaseType === Resource::TYPE_DATABASE_LEGACY || $databaseType === Resource::TYPE_DATABASE_TABLESDB) {
7979
$databaseType = Resource::TYPE_DATABASE;
8080
}
81-
if(Resource::isSupported($databaseType,$resources)){
82-
$report[$databaseType]+=1;
81+
if (Resource::isSupported($databaseType, $resources)) {
82+
$report[$databaseType] += 1;
8383
}
8484
$databaseSpecificResources = Resource::DATABASE_TYPE_RESOURCE_MAP[$databaseType];
85-
85+
8686
$databaseSequence = $database->getSequence();
8787

8888
$tableId = "database_{$databaseSequence}";
@@ -127,7 +127,7 @@ public function report(array $resources, array &$report): mixed
127127
];
128128

129129
// todo: apply check get support for attributes
130-
if(Resource::isSupported($databaseSpecificResources['fields'], $resources)){
130+
if (Resource::isSupported($databaseSpecificResources['fields'], $resources)) {
131131
$count = $this->countResources('attributes', $commonQueries);
132132
$report[$databaseSpecificResources['fields']] += $count;
133133
}

0 commit comments

Comments
 (0)