Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/migration.svg)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord)

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).
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).

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.

Expand Down
4 changes: 2 additions & 2 deletions bin/MigrationCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function (mixed $value) {
},
function (mixed $value, Document $document, Database $database) {
$attributes = $database->find('attributes', [
Query::equal('collectionInternalId', [$document->getInternalId()]),
Query::equal('collectionInternalId', [$document->getSequence()]),
Query::equal('databaseInternalId', [$document->getAttribute('databaseInternalId')]),
Query::limit($database->getLimitForAttributes()),
]);
Expand All @@ -298,7 +298,7 @@ function (mixed $value) {
function (mixed $value, Document $document, Database $database) {
return $database
->find('indexes', [
Query::equal('collectionInternalId', [$document->getInternalId()]),
Query::equal('collectionInternalId', [$document->getSequence()]),
Query::equal('databaseInternalId', [$document->getAttribute('databaseInternalId')]),
Query::limit($database->getLimitForIndexes()),
]);
Expand Down
263 changes: 143 additions & 120 deletions composer.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/Migration/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ public function __construct()
*/
public function add(Resource $resource): void
{
if (! $resource->getInternalId()) {
if (! $resource->getSequence()) {
$resourceId = uniqid();
if (isset($this->cache[$resource->getName()][$resourceId])) {
$resourceId = uniqid();
// todo: $resourceId is not used?
}
$resource->setInternalId(uniqid());
$resource->setSequence(uniqid());
}

if ($resource->getName() == Resource::TYPE_DOCUMENT) {
$status = $resource->getStatus();
$documentId = $resource->getInternalId();
$documentId = $resource->getSequence();
$this->cache[$resource->getName()][$documentId] = $status;
return;
}
Expand All @@ -51,7 +51,7 @@ public function add(Resource $resource): void
$resource->setData(''); // Prevent Memory Leak
}

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

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ public function update(Resource $resource): void
{
// if documents then updating the status counter only
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
$documentId = $resource->getInternalId();
$documentId = $resource->getSequence();
if (!isset($this->cache[$resource->getName()][$documentId])) {
$this->add($resource);
} else {
Expand All @@ -94,7 +94,7 @@ public function update(Resource $resource): void
$this->add($resource);
}

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

/**
Expand All @@ -120,15 +120,15 @@ public function updateAll(array $resources): void
public function remove(Resource $resource): void
{
if ($resource->getName() === Resource::TYPE_DOCUMENT) {
if (! isset($this->cache[$resource->getName()][$resource->getInternalId()])) {
if (! isset($this->cache[$resource->getName()][$resource->getSequence()])) {
throw new \Exception('Resource does not exist in cache');
}
}
if (! in_array($resource, $this->cache[$resource->getName()])) {
throw new \Exception('Resource does not exist in cache');
}

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

/**
Expand Down
74 changes: 37 additions & 37 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected function createDatabase(Database $resource): bool
'originalId' => empty($resource->getOriginalId()) ? null : $resource->getOriginalId(),
]));

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

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

$this->database->createCollection(
'database_' . $database->getInternalId(),
'database_' . $database->getSequence(),
$attributes,
$indexes
);
Expand Down Expand Up @@ -389,9 +389,9 @@ protected function createCollection(Collection $resource): bool
);
}

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

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

$this->database->createCollection(
'database_' . $database->getInternalId() . '_collection_' . $resource->getInternalId(),
'database_' . $database->getSequence() . '_collection_' . $resource->getSequence(),
permissions: $resource->getPermissions(),
documentSecurity: $resource->getDocumentSecurity()
);
Expand Down Expand Up @@ -449,7 +449,7 @@ protected function createAttribute(Attribute $resource): bool
}

$collection = $this->database->getDocument(
'database_' . $database->getInternalId(),
'database_' . $database->getSequence(),
$resource->getCollection()->getId(),
);

Expand Down Expand Up @@ -491,7 +491,7 @@ protected function createAttribute(Attribute $resource): bool
if ($type === UtopiaDatabase::VAR_RELATIONSHIP) {
$resource->getOptions()['side'] = UtopiaDatabase::RELATION_SIDE_PARENT;
$relatedCollection = $this->database->getDocument(
'database_' . $database->getInternalId(),
'database_' . $database->getSequence(),
$resource->getOptions()['relatedCollection']
);
if ($relatedCollection->isEmpty()) {
Expand All @@ -506,11 +506,11 @@ protected function createAttribute(Attribute $resource): bool

try {
$attribute = new UtopiaDocument([
'$id' => ID::custom($database->getInternalId() . '_' . $collection->getInternalId() . '_' . $resource->getKey()),
'$id' => ID::custom($database->getSequence() . '_' . $collection->getSequence() . '_' . $resource->getKey()),
'key' => $resource->getKey(),
'databaseInternalId' => $database->getInternalId(),
'databaseInternalId' => $database->getSequence(),
'databaseId' => $database->getId(),
'collectionInternalId' => $collection->getInternalId(),
'collectionInternalId' => $collection->getSequence(),
'collectionId' => $collection->getId(),
'type' => $type,
'status' => 'available',
Expand Down Expand Up @@ -545,13 +545,13 @@ protected function createAttribute(Attribute $resource): bool
message: 'Attribute limit exceeded',
);
} catch (\Throwable $e) {
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $collection->getId());
$this->database->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId());
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $collection->getId());
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $collection->getSequence());
throw $e;
}

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

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

try {
$twoWayAttribute = new UtopiaDocument([
'$id' => ID::custom($database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $twoWayKey),
'$id' => ID::custom($database->getSequence() . '_' . $relatedCollection->getSequence() . '_' . $twoWayKey),
'key' => $twoWayKey,
'databaseInternalId' => $database->getInternalId(),
'databaseInternalId' => $database->getSequence(),
'databaseId' => $database->getId(),
'collectionInternalId' => $relatedCollection->getInternalId(),
'collectionInternalId' => $relatedCollection->getSequence(),
'collectionId' => $relatedCollection->getId(),
'type' => $type,
'status' => 'available',
Expand Down Expand Up @@ -605,8 +605,8 @@ protected function createAttribute(Attribute $resource): bool
message: 'Attribute limit exceeded',
);
} catch (\Throwable $e) {
$this->database->purgeCachedDocument('database_' . $database->getInternalId(), $relatedCollection->getId());
$this->database->purgeCachedCollection('database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId());
$this->database->purgeCachedDocument('database_' . $database->getSequence(), $relatedCollection->getId());
$this->database->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedCollection->getSequence());
throw $e;
}
}
Expand All @@ -615,8 +615,8 @@ protected function createAttribute(Attribute $resource): bool
switch ($type) {
case UtopiaDatabase::VAR_RELATIONSHIP:
if (!$this->database->createRelationship(
collection: 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
relatedCollection: 'database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId(),
collection: 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
relatedCollection: 'database_' . $database->getSequence() . '_collection_' . $relatedCollection->getSequence(),
type: $options['relationType'],
twoWay: $options['twoWay'],
id: $resource->getKey(),
Expand All @@ -633,7 +633,7 @@ protected function createAttribute(Attribute $resource): bool
break;
default:
if (!$this->database->createAttribute(
'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
$resource->getKey(),
$type,
$resource->getSize(),
Expand Down Expand Up @@ -664,11 +664,11 @@ protected function createAttribute(Attribute $resource): bool
}

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

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

return true;
}
Expand All @@ -693,7 +693,7 @@ protected function createIndex(Index $resource): bool
}

$collection = $this->database->getDocument(
'database_' . $database->getInternalId(),
'database_' . $database->getSequence(),
$resource->getCollection()->getId(),
);
if ($collection->isEmpty()) {
Expand All @@ -706,8 +706,8 @@ protected function createIndex(Index $resource): bool
}

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

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

$index = new UtopiaDocument([
'$id' => ID::custom($database->getInternalId() . '_' . $collection->getInternalId() . '_' . $resource->getKey()),
'$id' => ID::custom($database->getSequence() . '_' . $collection->getSequence() . '_' . $resource->getKey()),
'key' => $resource->getKey(),
'status' => 'available', // processing, available, failed, deleting, stuck
'databaseInternalId' => $database->getInternalId(),
'databaseInternalId' => $database->getSequence(),
'databaseId' => $database->getId(),
'collectionInternalId' => $collection->getInternalId(),
'collectionInternalId' => $collection->getSequence(),
'collectionId' => $collection->getId(),
'type' => $resource->getType(),
'attributes' => $resource->getAttributes(),
Expand Down Expand Up @@ -844,7 +844,7 @@ protected function createIndex(Index $resource): bool

try {
$result = $this->database->createIndex(
'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(),
'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(),
$resource->getKey(),
$resource->getType(),
$resource->getAttributes(),
Expand Down Expand Up @@ -872,7 +872,7 @@ protected function createIndex(Index $resource): bool
}

$this->database->purgeCachedDocument(
'database_' . $database->getInternalId(),
'database_' . $database->getSequence(),
$collection->getId()
);

Expand Down Expand Up @@ -928,12 +928,12 @@ protected function createDocument(Document $resource, bool $isLast): bool
$resource->getCollection()->getDatabase()->getId(),
);
$collection = $this->database->getDocument(
'database_' . $database->getInternalId(),
'database_' . $database->getSequence(),
$resource->getCollection()->getId(),
);

$databaseInternalId = $database->getInternalId();
$collectionInternalId = $collection->getInternalId();
$databaseInternalId = $database->getSequence();
$collectionInternalId = $collection->getSequence();

/**
* This is in case an attribute was deleted from Appwrite attributes collection but was not deleted from the table
Expand Down
10 changes: 5 additions & 5 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ abstract class Resource implements \JsonSerializable

protected string $originalId = '';

protected string $internalId = '';
protected string $sequence = '';

protected string $createdAt = '';
protected string $updatedAt = '';
Expand Down Expand Up @@ -117,14 +117,14 @@ public function setOriginalId(string $originalId): self
return $this;
}

public function getInternalId(): string
public function getSequence(): string
{
return $this->internalId;
return $this->sequence;
}

public function setInternalId(string $internalId): self
public function setSequence(string $sequence): self
{
$this->internalId = $internalId;
$this->sequence = $sequence;

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ private function exportDeploymentData(Func $func, array $deployment): void
$file,
$deployment['activate']
);
$deployment->setInternalId($deployment->getId());
$deployment->setSequence($deployment->getId());

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

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

$deployment->setInternalId($deployment->getId());
$deployment->setSequence($deployment->getId());

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

Expand Down
Loading