Skip to content

Commit bef2ad1

Browse files
committed
Change internal ID to sequence
1 parent e518d39 commit bef2ad1

9 files changed

Lines changed: 230 additions & 198 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/migration.svg)
55
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord)
66

7-
Utopia Migration is a simple and lite library to migrate and transform resources inbetween services. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io).
7+
Utopia Migration is a simple and lite library to migrate and transform resources in-between services. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io).
88

99
Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework.
1010

bin/MigrationCLI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function (mixed $value) {
271271
},
272272
function (mixed $value, Document $document, Database $database) {
273273
$attributes = $database->find('attributes', [
274-
Query::equal('collectionInternalId', [$document->getInternalId()]),
274+
Query::equal('collectionInternalId', [$document->getSequence()]),
275275
Query::equal('databaseInternalId', [$document->getAttribute('databaseInternalId')]),
276276
Query::limit($database->getLimitForAttributes()),
277277
]);
@@ -298,7 +298,7 @@ function (mixed $value) {
298298
function (mixed $value, Document $document, Database $database) {
299299
return $database
300300
->find('indexes', [
301-
Query::equal('collectionInternalId', [$document->getInternalId()]),
301+
Query::equal('collectionInternalId', [$document->getSequence()]),
302302
Query::equal('databaseInternalId', [$document->getAttribute('databaseInternalId')]),
303303
Query::limit($database->getLimitForIndexes()),
304304
]);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"ext-curl": "*",
2727
"ext-openssl": "*",
2828
"appwrite/appwrite": "11.*",
29-
"utopia-php/database": "0.*.*",
29+
"utopia-php/database": "dev-feat-internal-to-sequence as 0.65.5",
3030
"utopia-php/storage": "0.18.*",
3131
"utopia-php/dsn": "0.2.*",
3232
"utopia-php/framework": "0.33.*"

composer.lock

Lines changed: 155 additions & 123 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Migration/Cache.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public function __construct()
3030
*/
3131
public function add(Resource $resource): void
3232
{
33-
if (! $resource->getInternalId()) {
33+
if (! $resource->getSequence()) {
3434
$resourceId = uniqid();
3535
if (isset($this->cache[$resource->getName()][$resourceId])) {
3636
$resourceId = uniqid();
3737
// todo: $resourceId is not used?
3838
}
39-
$resource->setInternalId(uniqid());
39+
$resource->setSequence(uniqid());
4040
}
4141

4242
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
4343
$status = $resource->getStatus();
44-
$documentId = $resource->getInternalId();
44+
$documentId = $resource->getSequence();
4545
$this->cache[$resource->getName()][$documentId] = $status;
4646
return;
4747
}
@@ -51,7 +51,7 @@ public function add(Resource $resource): void
5151
$resource->setData(''); // Prevent Memory Leak
5252
}
5353

54-
$this->cache[$resource->getName()][$resource->getInternalId()] = $resource;
54+
$this->cache[$resource->getName()][$resource->getSequence()] = $resource;
5555
}
5656

5757
/**
@@ -80,7 +80,7 @@ public function update(Resource $resource): void
8080
{
8181
// if documents then updating the status counter only
8282
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
83-
$documentId = $resource->getInternalId();
83+
$documentId = $resource->getSequence();
8484
if (!isset($this->cache[$resource->getName()][$documentId])) {
8585
$this->add($resource);
8686
} else {
@@ -94,7 +94,7 @@ public function update(Resource $resource): void
9494
$this->add($resource);
9595
}
9696

97-
$this->cache[$resource->getName()][$resource->getInternalId()] = $resource;
97+
$this->cache[$resource->getName()][$resource->getSequence()] = $resource;
9898
}
9999

100100
/**
@@ -120,15 +120,15 @@ public function updateAll(array $resources): void
120120
public function remove(Resource $resource): void
121121
{
122122
if ($resource->getName() === Resource::TYPE_DOCUMENT) {
123-
if (! isset($this->cache[$resource->getName()][$resource->getInternalId()])) {
123+
if (! isset($this->cache[$resource->getName()][$resource->getSequence()])) {
124124
throw new \Exception('Resource does not exist in cache');
125125
}
126126
}
127127
if (! in_array($resource, $this->cache[$resource->getName()])) {
128128
throw new \Exception('Resource does not exist in cache');
129129
}
130130

131-
unset($this->cache[$resource->getName()][$resource->getInternalId()]);
131+
unset($this->cache[$resource->getName()][$resource->getSequence()]);
132132
}
133133

134134
/**

src/Migration/Destinations/Appwrite.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ protected function createDatabase(Database $resource): bool
332332
'originalId' => empty($resource->getOriginalId()) ? null : $resource->getOriginalId(),
333333
]));
334334

335-
$resource->setInternalId($database->getInternalId());
335+
$resource->setSequence($database->getSequence());
336336

337337
$attributes = \array_map(
338338
fn ($attr) => new UtopiaDocument($attr),
@@ -344,7 +344,7 @@ protected function createDatabase(Database $resource): bool
344344
);
345345

346346
$this->database->createCollection(
347-
'database_' . $database->getInternalId(),
347+
'database_' . $database->getSequence(),
348348
$attributes,
349349
$indexes
350350
);
@@ -389,9 +389,9 @@ protected function createCollection(Collection $resource): bool
389389
);
390390
}
391391

392-
$collection = $this->database->createDocument('database_' . $database->getInternalId(), new UtopiaDocument([
392+
$collection = $this->database->createDocument('database_' . $database->getSequence(), new UtopiaDocument([
393393
'$id' => $resource->getId(),
394-
'databaseInternalId' => $database->getInternalId(),
394+
'databaseInternalId' => $database->getSequence(),
395395
'databaseId' => $resource->getDatabase()->getId(),
396396
'$permissions' => Permission::aggregate($resource->getPermissions()),
397397
'documentSecurity' => $resource->getDocumentSecurity(),
@@ -402,10 +402,10 @@ protected function createCollection(Collection $resource): bool
402402
'$updatedAt' => $resource->getUpdatedAt(),
403403
]));
404404

405-
$resource->setInternalId($collection->getInternalId());
405+
$resource->setSequence($collection->getSequence());
406406

407407
$this->database->createCollection(
408-
'database_' . $database->getInternalId() . '_collection_' . $resource->getInternalId(),
408+
'database_' . $database->getSequence() . '_collection_' . $resource->getSequence(),
409409
permissions: $resource->getPermissions(),
410410
documentSecurity: $resource->getDocumentSecurity()
411411
);
@@ -449,7 +449,7 @@ protected function createAttribute(Attribute $resource): bool
449449
}
450450

451451
$collection = $this->database->getDocument(
452-
'database_' . $database->getInternalId(),
452+
'database_' . $database->getSequence(),
453453
$resource->getCollection()->getId(),
454454
);
455455

@@ -491,7 +491,7 @@ protected function createAttribute(Attribute $resource): bool
491491
if ($type === UtopiaDatabase::VAR_RELATIONSHIP) {
492492
$resource->getOptions()['side'] = UtopiaDatabase::RELATION_SIDE_PARENT;
493493
$relatedCollection = $this->database->getDocument(
494-
'database_' . $database->getInternalId(),
494+
'database_' . $database->getSequence(),
495495
$resource->getOptions()['relatedCollection']
496496
);
497497
if ($relatedCollection->isEmpty()) {
@@ -506,11 +506,11 @@ protected function createAttribute(Attribute $resource): bool
506506

507507
try {
508508
$attribute = new UtopiaDocument([
509-
'$id' => ID::custom($database->getInternalId() . '_' . $collection->getInternalId() . '_' . $resource->getKey()),
509+
'$id' => ID::custom($database->getSequence() . '_' . $collection->getSequence() . '_' . $resource->getKey()),
510510
'key' => $resource->getKey(),
511-
'databaseInternalId' => $database->getInternalId(),
511+
'databaseInternalId' => $database->getSequence(),
512512
'databaseId' => $database->getId(),
513-
'collectionInternalId' => $collection->getInternalId(),
513+
'collectionInternalId' => $collection->getSequence(),
514514
'collectionId' => $collection->getId(),
515515
'type' => $type,
516516
'status' => 'available',
@@ -545,13 +545,13 @@ protected function createAttribute(Attribute $resource): bool
545545
message: 'Attribute limit exceeded',
546546
);
547547
} catch (\Throwable $e) {
548-
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $collection->getId());
549-
$this->database->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId());
548+
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $collection->getId());
549+
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $collection->getSequence());
550550
throw $e;
551551
}
552552

553-
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $collection->getId());
554-
$this->database->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId());
553+
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $collection->getId());
554+
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $collection->getSequence());
555555
$options = $resource->getOptions();
556556

557557
$twoWayKey = null;
@@ -564,11 +564,11 @@ protected function createAttribute(Attribute $resource): bool
564564

565565
try {
566566
$twoWayAttribute = new UtopiaDocument([
567-
'$id' => ID::custom($database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $twoWayKey),
567+
'$id' => ID::custom($database->getSequence() . '_' . $relatedCollection->getSequence() . '_' . $twoWayKey),
568568
'key' => $twoWayKey,
569-
'databaseInternalId' => $database->getInternalId(),
569+
'databaseInternalId' => $database->getSequence(),
570570
'databaseId' => $database->getId(),
571-
'collectionInternalId' => $relatedCollection->getInternalId(),
571+
'collectionInternalId' => $relatedCollection->getSequence(),
572572
'collectionId' => $relatedCollection->getId(),
573573
'type' => $type,
574574
'status' => 'available',
@@ -605,8 +605,8 @@ protected function createAttribute(Attribute $resource): bool
605605
message: 'Attribute limit exceeded',
606606
);
607607
} catch (\Throwable $e) {
608-
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $relatedCollection->getId());
609-
$this->database->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId());
608+
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $relatedCollection->getId());
609+
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedCollection->getSequence());
610610
throw $e;
611611
}
612612
}
@@ -615,8 +615,8 @@ protected function createAttribute(Attribute $resource): bool
615615
switch ($type) {
616616
case UtopiaDatabase::VAR_RELATIONSHIP:
617617
if (!$this->database->createRelationship(
618-
collection: 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
619-
relatedCollection: 'database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId(),
618+
collection: 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
619+
relatedCollection: 'database_' . $database->getSequence() . '_collection_' . $relatedCollection->getSequence(),
620620
type: $options['relationType'],
621621
twoWay: $options['twoWay'],
622622
id: $resource->getKey(),
@@ -633,7 +633,7 @@ protected function createAttribute(Attribute $resource): bool
633633
break;
634634
default:
635635
if (!$this->database->createAttribute(
636-
'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
636+
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
637637
$resource->getKey(),
638638
$type,
639639
$resource->getSize(),
@@ -664,11 +664,11 @@ protected function createAttribute(Attribute $resource): bool
664664
}
665665

666666
if ($type === UtopiaDatabase::VAR_RELATIONSHIP && $options['twoWay']) {
667-
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $relatedCollection->getId());
667+
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $relatedCollection->getId());
668668
}
669669

670-
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $collection->getId());
671-
$this->database->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId());
670+
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $collection->getId());
671+
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $collection->getSequence());
672672

673673
return true;
674674
}
@@ -693,7 +693,7 @@ protected function createIndex(Index $resource): bool
693693
}
694694

695695
$collection = $this->database->getDocument(
696-
'database_' . $database->getInternalId(),
696+
'database_' . $database->getSequence(),
697697
$resource->getCollection()->getId(),
698698
);
699699
if ($collection->isEmpty()) {
@@ -706,8 +706,8 @@ protected function createIndex(Index $resource): bool
706706
}
707707

708708
$count = $this->database->count('indexes', [
709-
Query::equal('collectionInternalId', [$collection->getInternalId()]),
710-
Query::equal('databaseInternalId', [$database->getInternalId()])
709+
Query::equal('collectionInternalId', [$collection->getSequence()]),
710+
Query::equal('databaseInternalId', [$database->getSequence()])
711711
], $this->database->getLimitForIndexes());
712712

713713
if ($count >= $this->database->getLimitForIndexes()) {
@@ -810,12 +810,12 @@ protected function createIndex(Index $resource): bool
810810
}
811811

812812
$index = new UtopiaDocument([
813-
'$id' => ID::custom($database->getInternalId() . '_' . $collection->getInternalId() . '_' . $resource->getKey()),
813+
'$id' => ID::custom($database->getSequence() . '_' . $collection->getSequence() . '_' . $resource->getKey()),
814814
'key' => $resource->getKey(),
815815
'status' => 'available', // processing, available, failed, deleting, stuck
816-
'databaseInternalId' => $database->getInternalId(),
816+
'databaseInternalId' => $database->getSequence(),
817817
'databaseId' => $database->getId(),
818-
'collectionInternalId' => $collection->getInternalId(),
818+
'collectionInternalId' => $collection->getSequence(),
819819
'collectionId' => $collection->getId(),
820820
'type' => $resource->getType(),
821821
'attributes' => $resource->getAttributes(),
@@ -844,7 +844,7 @@ protected function createIndex(Index $resource): bool
844844

845845
try {
846846
$result = $this->database->createIndex(
847-
'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
847+
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
848848
$resource->getKey(),
849849
$resource->getType(),
850850
$resource->getAttributes(),
@@ -872,7 +872,7 @@ protected function createIndex(Index $resource): bool
872872
}
873873

874874
$this->database->purgeCachedDocument(
875-
'database_' . $database->getInternalId(),
875+
'database_' . $database->getSequence(),
876876
$collection->getId()
877877
);
878878

@@ -928,12 +928,12 @@ protected function createDocument(Document $resource, bool $isLast): bool
928928
$resource->getCollection()->getDatabase()->getId(),
929929
);
930930
$collection = $this->database->getDocument(
931-
'database_' . $database->getInternalId(),
931+
'database_' . $database->getSequence(),
932932
$resource->getCollection()->getId(),
933933
);
934934

935-
$databaseInternalId = $database->getInternalId();
936-
$collectionInternalId = $collection->getInternalId();
935+
$databaseInternalId = $database->getSequence();
936+
$collectionInternalId = $collection->getSequence();
937937

938938
/**
939939
* This is in case an attribute was deleted from Appwrite attributes collection but was not deleted from the table

src/Migration/Resource.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ abstract class Resource implements \JsonSerializable
7575

7676
protected string $originalId = '';
7777

78-
protected string $internalId = '';
78+
protected string $sequence = '';
7979

8080
protected string $createdAt = '';
8181
protected string $updatedAt = '';
@@ -117,14 +117,14 @@ public function setOriginalId(string $originalId): self
117117
return $this;
118118
}
119119

120-
public function getInternalId(): string
120+
public function getSequence(): string
121121
{
122-
return $this->internalId;
122+
return $this->sequence;
123123
}
124124

125-
public function setInternalId(string $internalId): self
125+
public function setSequence(string $sequence): self
126126
{
127-
$this->internalId = $internalId;
127+
$this->sequence = $sequence;
128128

129129
return $this;
130130
}

src/Migration/Sources/Appwrite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ private function exportDeploymentData(Func $func, array $deployment): void
14071407
$file,
14081408
$deployment['activate']
14091409
);
1410-
$deployment->setInternalId($deployment->getId());
1410+
$deployment->setSequence($deployment->getId());
14111411

14121412
$this->callback([$deployment]);
14131413

@@ -1427,13 +1427,13 @@ private function exportDeploymentData(Func $func, array $deployment): void
14271427
$deployment['activate']
14281428
);
14291429

1430-
$deployment->setInternalId($deployment->getId());
1430+
$deployment->setSequence($deployment->getId());
14311431

14321432
// Loop until the entire file is downloaded
14331433
while ($start < $fileSize) {
14341434
$chunkData = $this->call(
14351435
'GET',
1436-
"/functions/{$func->getId()}/deployments/{$deployment->getInternalId()}/download",
1436+
"/functions/{$func->getId()}/deployments/{$deployment->getSequence()}/download",
14371437
['range' => "bytes=$start-$end"]
14381438
);
14391439

0 commit comments

Comments
 (0)