Skip to content

Commit 1bc788c

Browse files
added new key for the resources to have a parent child relationship
1 parent cf6b192 commit 1bc788c

1 file changed

Lines changed: 64 additions & 10 deletions

File tree

src/Migration/Cache.php

Lines changed: 64 additions & 10 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,56 @@ public function __construct()
2226
$this->cache = [];
2327
}
2428

29+
/**
30+
* Get cache key from 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+
78+
2579
/**
2680
* Add Resource
2781
*
@@ -38,11 +92,10 @@ public function add(Resource $resource): void
3892
}
3993
$resource->setSequence(uniqid());
4094
}
41-
95+
$key = $this->getResourceCacheKey($resource);
4296
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
4397
$status = $resource->getStatus();
44-
$rowId = $resource->getSequence();
45-
$this->cache[$resource->getName()][$rowId] = $status;
98+
$this->cache[$resource->getName()][$key] = $status;
4699
return;
47100
}
48101

@@ -51,7 +104,7 @@ public function add(Resource $resource): void
51104
$resource->setData(''); // Prevent Memory Leak
52105
}
53106

54-
$this->cache[$resource->getName()][$resource->getSequence()] = $resource;
107+
$this->cache[$resource->getName()][$key] = $resource;
55108
}
56109

57110
/**
@@ -78,14 +131,14 @@ public function addAll(array $resources): void
78131
*/
79132
public function update(Resource $resource): void
80133
{
134+
$key = $this->getResourceCacheKey($resource);
81135
// if rows then updating the status counter only
82136
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
83-
$rowId = $resource->getSequence();
84-
if (!isset($this->cache[$resource->getName()][$rowId])) {
137+
if (!isset($this->cache[$resource->getName()][$key])) {
85138
$this->add($resource);
86139
} else {
87140
$status = $resource->getStatus();
88-
$this->cache[$resource->getName()][$rowId] = $status;
141+
$this->cache[$resource->getName()][$key] = $status;
89142
}
90143
return;
91144
}
@@ -94,7 +147,7 @@ public function update(Resource $resource): void
94147
$this->add($resource);
95148
}
96149

97-
$this->cache[$resource->getName()][$resource->getSequence()] = $resource;
150+
$this->cache[$resource->getName()][$key] = $resource;
98151
}
99152

100153
/**
@@ -119,16 +172,17 @@ public function updateAll(array $resources): void
119172
*/
120173
public function remove(Resource $resource): void
121174
{
175+
$key = $this->getResourceCacheKey($resource);
122176
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
123-
if (! isset($this->cache[$resource->getName()][$resource->getSequence()])) {
177+
if (! isset($this->cache[$resource->getName()][$key])) {
124178
throw new \Exception('Resource does not exist in cache');
125179
}
126180
}
127181
if (! in_array($resource, $this->cache[$resource->getName()])) {
128182
throw new \Exception('Resource does not exist in cache');
129183
}
130184

131-
unset($this->cache[$resource->getName()][$resource->getSequence()]);
185+
unset($this->cache[$resource->getName()][$key]);
132186
}
133187

134188
/**

0 commit comments

Comments
 (0)