diff --git a/src/Migration/Cache.php b/src/Migration/Cache.php index c0c4af97..3e836947 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,55 @@ public function __construct() $this->cache = []; } + /** + * Get cache key for a 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 +91,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 +103,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 +130,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 +146,7 @@ public function update(Resource $resource): void $this->add($resource); } - $this->cache[$resource->getName()][$resource->getSequence()] = $resource; + $this->cache[$resource->getName()][$key] = $resource; } /** @@ -119,16 +171,17 @@ 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'); } } - if (! in_array($resource, $this->cache[$resource->getName()])) { + if (! in_array($resource, $this->cache[$key])) { throw new \Exception('Resource does not exist in cache'); } - unset($this->cache[$resource->getName()][$resource->getSequence()]); + unset($this->cache[$resource->getName()][$key]); } /**