Skip to content

Commit 8ddf9a2

Browse files
working version migration multi type
1 parent b79066f commit 8ddf9a2

8 files changed

Lines changed: 242 additions & 88 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static function getSupportedResources(): array
118118

119119
// Database
120120
Resource::TYPE_DATABASE,
121+
Resource::TYPE_DOCUMENTSDB_DATABASE,
121122
Resource::TYPE_TABLE,
122123
Resource::TYPE_COLUMN,
123124
Resource::TYPE_INDEX,
@@ -282,18 +283,19 @@ public function importDatabaseResource(Resource $resource, bool $isLast): Resour
282283
{
283284
switch ($resource->getName()) {
284285
case Resource::TYPE_DATABASE:
286+
case Resource::TYPE_DOCUMENTSDB_DATABASE:
285287
/** @var Database $resource */
286288
$success = $this->createDatabase($resource);
287289
break;
288290
case Resource::TYPE_TABLE:
289291
case Resource::TYPE_COLLECTION:
290292
/** @var Table $resource */
291-
$success = $this->createTable($resource);
293+
$success = $this->createEntity($resource);
292294
break;
293295
case Resource::TYPE_COLUMN:
294296
case Resource::TYPE_ATTRIBUTE:
295297
/** @var Column $resource */
296-
$success = $this->createColumn($resource);
298+
$success = $this->createField($resource);
297299
break;
298300
case Resource::TYPE_INDEX:
299301
/** @var Index $resource */
@@ -302,7 +304,7 @@ public function importDatabaseResource(Resource $resource, bool $isLast): Resour
302304
case Resource::TYPE_ROW:
303305
case Resource::TYPE_DOCUMENT:
304306
/** @var Row $resource */
305-
$success = $this->createRow($resource, $isLast);
307+
$success = $this->createRecord($resource, $isLast);
306308
break;
307309
default:
308310
$success = false;
@@ -377,7 +379,7 @@ protected function createDatabase(Database $resource): bool
377379
* @throws StructureException
378380
* @throws Exception
379381
*/
380-
protected function createTable(Table $resource): bool
382+
protected function createEntity(Table $resource): bool
381383
{
382384
if ($resource->getId() == 'unique()') {
383385
$resource->setId(ID::unique());
@@ -439,7 +441,7 @@ protected function createTable(Table $resource): bool
439441
* @throws \Exception
440442
* @throws \Throwable
441443
*/
442-
protected function createColumn(Column $resource): bool
444+
protected function createField(Column $resource): bool
443445
{
444446
// Skip columns for documents DB (schemaless)
445447
if ($resource->getTable()->getDatabase()->getType() === 'documents') {
@@ -922,7 +924,7 @@ protected function createIndex(Index $resource): bool
922924
* @throws StructureException
923925
* @throws Exception
924926
*/
925-
protected function createRow(Row $resource, bool $isLast): bool
927+
protected function createRecord(Row $resource, bool $isLast): bool
926928
{
927929
if ($resource->getId() == 'unique()') {
928930
$resource->setId(ID::unique());
@@ -942,7 +944,7 @@ protected function createRow(Row $resource, bool $isLast): bool
942944
// Check if document has already been created
943945
$exists = \array_key_exists(
944946
$resource->getId(),
945-
$this->cache->get(Resource::TYPE_ROW)
947+
$this->cache->get($resource->getName())
946948
);
947949

948950
if ($exists) {
@@ -972,32 +974,33 @@ protected function createRow(Row $resource, bool $isLast): bool
972974

973975
$databaseInternalId = $database->getSequence();
974976
$tableInternalId = $table->getSequence();
975-
977+
$dbForDatabase = call_user_func($this->getDatabaseDB, $database);
976978
/**
977979
* This is in case an attribute was deleted from Appwrite attributes collection but was not deleted from the table
978980
* When creating an archive we select * which will include orphan attribute from the schema
979981
*/
980-
foreach ($this->rowBuffer as $row) {
981-
foreach ($row as $key => $value) {
982-
if (\str_starts_with($key, '$')) {
983-
continue;
984-
}
985-
986-
/** @var \Utopia\Database\Document $attribute */
987-
$found = false;
988-
foreach ($table->getAttribute('attributes', []) as $attribute) {
989-
if ($attribute->getAttribute('key') == $key) {
990-
$found = true;
991-
break;
982+
if($dbForDatabase->getAdapter()->getSupportForAttributes()){
983+
foreach ($this->rowBuffer as $row) {
984+
foreach ($row as $key => $value) {
985+
if (\str_starts_with($key, '$')) {
986+
continue;
987+
}
988+
989+
/** @var \Utopia\Database\Document $attribute */
990+
$found = false;
991+
foreach ($table->getAttribute('attributes', []) as $attribute) {
992+
if ($attribute->getAttribute('key') == $key) {
993+
$found = true;
994+
break;
995+
}
996+
}
997+
998+
if (! $found) {
999+
$row->removeAttribute($key);
9921000
}
993-
}
994-
995-
if (! $found) {
996-
$row->removeAttribute($key);
9971001
}
9981002
}
9991003
}
1000-
$dbForDatabase = call_user_func($this->getDatabaseDB, $database);
10011004
$dbForDatabase->skipRelationshipsExistCheck(fn () => $dbForDatabase->createDocuments(
10021005
'database_' . $databaseInternalId . '_collection_' . $tableInternalId,
10031006
$this->rowBuffer

src/Migration/Resource.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ abstract class Resource implements \JsonSerializable
3030

3131
public const TYPE_DATABASE = 'database';
3232

33+
public const TYPE_DATABASE_LEGACY = 'legacy';
34+
35+
public const TYPE_DATABASE_TABLESDB = 'tablesdb';
36+
3337
public const TYPE_DOCUMENTSDB_DATABASE = 'documentsdb';
3438

3539
public const TYPE_ROW = 'row';
@@ -90,6 +94,20 @@ abstract class Resource implements \JsonSerializable
9094
self::TYPE_COLLECTION,
9195
];
9296

97+
// index terminology is same for all
98+
public const DATABASE_TYPE_RESOURCE_MAP = [
99+
self::TYPE_DATABASE => [
100+
'entity' => self::TYPE_TABLE,
101+
'fields' => self::TYPE_COLUMN,
102+
'records' => self::TYPE_ROW,
103+
],
104+
self::TYPE_DOCUMENTSDB_DATABASE => [
105+
'entity' => self::TYPE_COLLECTION,
106+
'records' => self::TYPE_DOCUMENT,
107+
'fields' => self::TYPE_ATTRIBUTE,
108+
],
109+
];
110+
93111
protected string $id = '';
94112

95113
protected string $originalId = '';

src/Migration/Resources/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
*/
4545
public static function fromArray(array $array): self
4646
{
47-
return new self(
47+
return new static(
4848
$array['id'],
4949
$array['name'],
5050
createdAt: $array['createdAt'] ?? '',

src/Migration/Resources/Database/Row.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
*/
5353
public static function fromArray(array $array): self
5454
{
55-
return new self(
55+
return new static(
5656
$array['id'],
5757
Table::fromArray($array['table'] ?? $array['collection']),
5858
$array['data'],

src/Migration/Resources/Database/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949
*/
5050
public static function fromArray(array $array): self
5151
{
52-
return new self(
52+
return new static(
5353
Database::fromArray($array['database']),
5454
name: $array['name'],
5555
id: $array['id'],

0 commit comments

Comments
 (0)