From 1bc788c07b937b53436dfc9d157a734af53989c6 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Sat, 26 Jul 2025 01:58:00 +0530 Subject: [PATCH 1/4] added new key for the resources to have a parent child relationship --- src/Migration/Cache.php | 74 +++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/src/Migration/Cache.php b/src/Migration/Cache.php index c0c4af97..6a6f1374 100644 --- a/src/Migration/Cache.php +++ b/src/Migration/Cache.php @@ -2,6 +2,10 @@ namespace Utopia\Migration; +use Utopia\Migration\Resources\Database\Column; +use Utopia\Migration\Resources\Database\Index; +use Utopia\Migration\Resources\Database\Row; +use Utopia\Migration\Resources\Database\Table; use Utopia\Migration\Resources\Functions\Deployment; use Utopia\Migration\Resources\Storage\File; @@ -22,6 +26,56 @@ public function __construct() $this->cache = []; } + /** + * Get cache key from resource + * + * @param Resource $resource + * @return string + */ + public function getResourceCacheKey(Resource $resource): string + { + $resourceName = $resource->getName(); + $keys = []; + + switch ($resourceName) { + case Resource::TYPE_TABLE: + case Resource::TYPE_COLLECTION: + /** @var Table $resource */ + $keys[] = $resource->getDatabase()->getSequence(); + break; + + case Resource::TYPE_ROW: + case Resource::TYPE_DOCUMENT: + case Resource::TYPE_COLUMN: + case Resource::TYPE_ATTRIBUTE: + case Resource::TYPE_INDEX: + /** @var Row|Column|Index $resource */ + $table = $resource->getTable(); + $keys[] = $table->getDatabase()->getSequence(); + $keys[] = $table->getSequence(); + break; + + case Resource::TYPE_FILE: + /** @var File $resource */ + $keys[] = $resource->getBucket()->getSequence(); + break; + + case Resource::TYPE_DEPLOYMENT: + /** @var Deployment $resource */ + $keys[] = $resource->getFunction()->getSequence(); + break; + + default: + break; + } + + $keys[] = $resource->getSequence(); + + $joinedKey = implode('_', $keys); + return $joinedKey; + } + + /** * Add Resource * @@ -38,11 +92,10 @@ public function add(Resource $resource): void } $resource->setSequence(uniqid()); } - + $key = $this->getResourceCacheKey($resource); if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) { $status = $resource->getStatus(); - $rowId = $resource->getSequence(); - $this->cache[$resource->getName()][$rowId] = $status; + $this->cache[$resource->getName()][$key] = $status; return; } @@ -51,7 +104,7 @@ public function add(Resource $resource): void $resource->setData(''); // Prevent Memory Leak } - $this->cache[$resource->getName()][$resource->getSequence()] = $resource; + $this->cache[$resource->getName()][$key] = $resource; } /** @@ -78,14 +131,14 @@ public function addAll(array $resources): void */ public function update(Resource $resource): void { + $key = $this->getResourceCacheKey($resource); // if rows then updating the status counter only if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) { - $rowId = $resource->getSequence(); - if (!isset($this->cache[$resource->getName()][$rowId])) { + if (!isset($this->cache[$resource->getName()][$key])) { $this->add($resource); } else { $status = $resource->getStatus(); - $this->cache[$resource->getName()][$rowId] = $status; + $this->cache[$resource->getName()][$key] = $status; } return; } @@ -94,7 +147,7 @@ public function update(Resource $resource): void $this->add($resource); } - $this->cache[$resource->getName()][$resource->getSequence()] = $resource; + $this->cache[$resource->getName()][$key] = $resource; } /** @@ -119,8 +172,9 @@ public function updateAll(array $resources): void */ public function remove(Resource $resource): void { + $key = $this->getResourceCacheKey($resource); if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) { - if (! isset($this->cache[$resource->getName()][$resource->getSequence()])) { + if (! isset($this->cache[$resource->getName()][$key])) { throw new \Exception('Resource does not exist in cache'); } } @@ -128,7 +182,7 @@ public function remove(Resource $resource): void throw new \Exception('Resource does not exist in cache'); } - unset($this->cache[$resource->getName()][$resource->getSequence()]); + unset($this->cache[$resource->getName()][$key]); } /** From 0238ae930876ff153ef40c4a55e046487928e9cb Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Sat, 26 Jul 2025 02:07:19 +0530 Subject: [PATCH 2/4] added key --- src/Migration/Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Migration/Cache.php b/src/Migration/Cache.php index 6a6f1374..97a01547 100644 --- a/src/Migration/Cache.php +++ b/src/Migration/Cache.php @@ -178,7 +178,7 @@ public function remove(Resource $resource): void throw new \Exception('Resource does not exist in cache'); } } - if (! in_array($resource, $this->cache[$resource->getName()])) { + if (! in_array($resource, $this->cache[$key])) { throw new \Exception('Resource does not exist in cache'); } From 935f96571a6abbe0fb3f4770f943e6fcbe68853a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 28 Jul 2025 14:42:13 +1200 Subject: [PATCH 3/4] Apply suggestion from @abnegate --- src/Migration/Cache.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Migration/Cache.php b/src/Migration/Cache.php index 97a01547..e5690114 100644 --- a/src/Migration/Cache.php +++ b/src/Migration/Cache.php @@ -27,11 +27,11 @@ public function __construct() } /** - * Get cache key from resource - * - * @param Resource $resource - * @return string - */ + * Get cache key for a resource + * + * @param Resource $resource + * @return string + */ public function getResourceCacheKey(Resource $resource): string { $resourceName = $resource->getName(); From a2615cee82698c7e2ffae06df1fb4a01ab83f807 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 28 Jul 2025 14:42:19 +1200 Subject: [PATCH 4/4] Apply suggestion from @abnegate --- src/Migration/Cache.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Migration/Cache.php b/src/Migration/Cache.php index e5690114..3e836947 100644 --- a/src/Migration/Cache.php +++ b/src/Migration/Cache.php @@ -75,7 +75,6 @@ public function getResourceCacheKey(Resource $resource): string return $joinedKey; } - /** * Add Resource *