Skip to content

Commit e0e351b

Browse files
authored
Merge pull request #96 from utopia-php/dat-597
added new key for the resources to have a parent child relationship
2 parents cf6b192 + a2615ce commit e0e351b

1 file changed

Lines changed: 64 additions & 11 deletions

File tree

src/Migration/Cache.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Utopia\Migration;
44

5+
use Utopia\Migration\Resources\Database\Column;
6+
use Utopia\Migration\Resources\Database\Index;
7+
use Utopia\Migration\Resources\Database\Row;
8+
use Utopia\Migration\Resources\Database\Table;
59
use Utopia\Migration\Resources\Functions\Deployment;
610
use Utopia\Migration\Resources\Storage\File;
711

@@ -22,6 +26,55 @@ public function __construct()
2226
$this->cache = [];
2327
}
2428

29+
/**
30+
* Get cache key for a resource
31+
*
32+
* @param Resource $resource
33+
* @return string
34+
*/
35+
public function getResourceCacheKey(Resource $resource): string
36+
{
37+
$resourceName = $resource->getName();
38+
$keys = [];
39+
40+
switch ($resourceName) {
41+
case Resource::TYPE_TABLE:
42+
case Resource::TYPE_COLLECTION:
43+
/** @var Table $resource */
44+
$keys[] = $resource->getDatabase()->getSequence();
45+
break;
46+
47+
case Resource::TYPE_ROW:
48+
case Resource::TYPE_DOCUMENT:
49+
case Resource::TYPE_COLUMN:
50+
case Resource::TYPE_ATTRIBUTE:
51+
case Resource::TYPE_INDEX:
52+
/** @var Row|Column|Index $resource */
53+
$table = $resource->getTable();
54+
$keys[] = $table->getDatabase()->getSequence();
55+
$keys[] = $table->getSequence();
56+
break;
57+
58+
case Resource::TYPE_FILE:
59+
/** @var File $resource */
60+
$keys[] = $resource->getBucket()->getSequence();
61+
break;
62+
63+
case Resource::TYPE_DEPLOYMENT:
64+
/** @var Deployment $resource */
65+
$keys[] = $resource->getFunction()->getSequence();
66+
break;
67+
68+
default:
69+
break;
70+
}
71+
72+
$keys[] = $resource->getSequence();
73+
74+
$joinedKey = implode('_', $keys);
75+
return $joinedKey;
76+
}
77+
2578
/**
2679
* Add Resource
2780
*
@@ -38,11 +91,10 @@ public function add(Resource $resource): void
3891
}
3992
$resource->setSequence(uniqid());
4093
}
41-
94+
$key = $this->getResourceCacheKey($resource);
4295
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
4396
$status = $resource->getStatus();
44-
$rowId = $resource->getSequence();
45-
$this->cache[$resource->getName()][$rowId] = $status;
97+
$this->cache[$resource->getName()][$key] = $status;
4698
return;
4799
}
48100

@@ -51,7 +103,7 @@ public function add(Resource $resource): void
51103
$resource->setData(''); // Prevent Memory Leak
52104
}
53105

54-
$this->cache[$resource->getName()][$resource->getSequence()] = $resource;
106+
$this->cache[$resource->getName()][$key] = $resource;
55107
}
56108

57109
/**
@@ -78,14 +130,14 @@ public function addAll(array $resources): void
78130
*/
79131
public function update(Resource $resource): void
80132
{
133+
$key = $this->getResourceCacheKey($resource);
81134
// if rows then updating the status counter only
82135
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
83-
$rowId = $resource->getSequence();
84-
if (!isset($this->cache[$resource->getName()][$rowId])) {
136+
if (!isset($this->cache[$resource->getName()][$key])) {
85137
$this->add($resource);
86138
} else {
87139
$status = $resource->getStatus();
88-
$this->cache[$resource->getName()][$rowId] = $status;
140+
$this->cache[$resource->getName()][$key] = $status;
89141
}
90142
return;
91143
}
@@ -94,7 +146,7 @@ public function update(Resource $resource): void
94146
$this->add($resource);
95147
}
96148

97-
$this->cache[$resource->getName()][$resource->getSequence()] = $resource;
149+
$this->cache[$resource->getName()][$key] = $resource;
98150
}
99151

100152
/**
@@ -119,16 +171,17 @@ public function updateAll(array $resources): void
119171
*/
120172
public function remove(Resource $resource): void
121173
{
174+
$key = $this->getResourceCacheKey($resource);
122175
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
123-
if (! isset($this->cache[$resource->getName()][$resource->getSequence()])) {
176+
if (! isset($this->cache[$resource->getName()][$key])) {
124177
throw new \Exception('Resource does not exist in cache');
125178
}
126179
}
127-
if (! in_array($resource, $this->cache[$resource->getName()])) {
180+
if (! in_array($resource, $this->cache[$key])) {
128181
throw new \Exception('Resource does not exist in cache');
129182
}
130183

131-
unset($this->cache[$resource->getName()][$resource->getSequence()]);
184+
unset($this->cache[$resource->getName()][$key]);
132185
}
133186

134187
/**

0 commit comments

Comments
 (0)