Skip to content

Commit 63df4dd

Browse files
committed
Merge branch 'main' of https://github.com/utopia-php/migration into joins
# Conflicts: # src/Migration/Resources/Database/Database.php # src/Migration/Sources/Appwrite.php
2 parents b1c5095 + 95dcbb9 commit 63df4dd

35 files changed

Lines changed: 3228 additions & 683 deletions

src/Migration/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function resolveResourceCacheKey(Resource $resource): string
4646
case Resource::TYPE_TABLE:
4747
case Resource::TYPE_COLLECTION:
4848
/** @var Table $resource */
49+
$keys[] = $resource->getDatabase()->getType();
4950
$keys[] = $resource->getDatabase()->getSequence();
5051
break;
5152

src/Migration/Destinations/Appwrite.php

Lines changed: 732 additions & 183 deletions
Large diffs are not rendered by default.

src/Migration/Destinations/CSV.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Utopia\Database\Exception\Conflict;
77
use Utopia\Database\Exception\Structure;
88
use Utopia\Migration\Destination;
9+
use Utopia\Migration\Exception as MigrationException;
910
use Utopia\Migration\Resource as UtopiaResource;
1011
use Utopia\Migration\Resources\Database\Row;
1112
use Utopia\Migration\Transfer;
@@ -90,13 +91,13 @@ protected function import(array $resources, callable $callback): void
9091
if (!isset($handle)) {
9192
$handle = \fopen($log, 'a');
9293
if ($handle === false) {
93-
throw new \Exception("Failed to open file for writing: $log");
94+
throw new \Exception("Failed to open file for writing: $log", MigrationException::CODE_INTERNAL);
9495
}
9596
}
9697

9798
foreach ($buffer['lines'] as $line) {
9899
if (!$this->writeCSVLine($handle, $line)) {
99-
throw new \Exception("Failed to write CSV line to file: $log");
100+
throw new \Exception("Failed to write CSV line to file: $log", MigrationException::CODE_INTERNAL);
100101
}
101102
}
102103

@@ -167,7 +168,7 @@ public function shutdown(): void
167168
$destPath = $this->deviceForFiles->getPath($this->directory . '/' . $filename);
168169

169170
if (!$this->local->exists($sourcePath)) {
170-
throw new \Exception("No data to export for resource: $this->resourceId");
171+
throw new \Exception("No data to export for resource: $this->resourceId", MigrationException::CODE_NOT_FOUND);
171172
}
172173

173174
try {
@@ -177,10 +178,13 @@ public function shutdown(): void
177178
$this->deviceForFiles
178179
);
179180
if ($result === false) {
180-
throw new \Exception('Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename);
181+
throw new \Exception(
182+
'Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename,
183+
MigrationException::CODE_INTERNAL
184+
);
181185
}
182186
if (!$this->deviceForFiles->exists($destPath)) {
183-
throw new \Exception('File not found on destination: ' . $destPath);
187+
throw new \Exception('File not found on destination: ' . $destPath, MigrationException::CODE_NOT_FOUND);
184188
}
185189
} finally {
186190
// Clean up the temporary directory
@@ -226,7 +230,7 @@ protected function createDirectory(string $path): void
226230
{
227231
if (!\file_exists($path)) {
228232
if (!\mkdir($path, 0755, true)) {
229-
throw new \Exception('Error creating directory: ' . $path);
233+
throw new \Exception('Error creating directory: ' . $path, MigrationException::CODE_INTERNAL);
230234
}
231235
}
232236
}

src/Migration/Destinations/JSON.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ protected function import(array $resources, callable $callback): void
139139
resourceGroup: $resource->getGroup(),
140140
resourceId: $resource->getId(),
141141
message: $e->getMessage(),
142+
code: MigrationException::CODE_VALIDATION,
142143
previous: $e,
143144
));
144145
continue;

src/Migration/Destinations/Local.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Utopia\Migration\Destinations;
44

55
use Utopia\Migration\Destination;
6+
use Utopia\Migration\Exception as MigrationException;
67
use Utopia\Migration\Resource;
78
use Utopia\Migration\Resources\Functions\Deployment;
89
use Utopia\Migration\Resources\Storage\File;
@@ -70,6 +71,12 @@ public static function getSupportedResources(): array
7071
Resource::TYPE_FUNCTION,
7172
Resource::TYPE_DEPLOYMENT,
7273
Resource::TYPE_ENVIRONMENT_VARIABLE,
74+
75+
// Messaging
76+
Resource::TYPE_PROVIDER,
77+
Resource::TYPE_TOPIC,
78+
Resource::TYPE_SUBSCRIBER,
79+
Resource::TYPE_MESSAGE,
7380
];
7481
}
7582

@@ -81,7 +88,7 @@ public function report(array $resources = [], array $resourceIds = []): array
8188
$report = [];
8289

8390
if (!\is_writable($this->path . '/backup.json')) {
84-
throw new \Exception('Unable to write to file: ' . $this->path);
91+
throw new \Exception('Unable to write to file: ' . $this->path, MigrationException::CODE_INTERNAL);
8592
}
8693

8794
return $report;
@@ -95,7 +102,10 @@ private function sync(): void
95102
$json = \json_encode($this->data, JSON_PRETTY_PRINT);
96103

97104
if ($json === false) {
98-
throw new \Exception('Unable to encode data to JSON, Are you accidentally encoding binary data?');
105+
throw new \Exception(
106+
'Unable to encode data to JSON, Are you accidentally encoding binary data?',
107+
MigrationException::CODE_VALIDATION
108+
);
99109
}
100110

101111
\file_put_contents($this->path . '/backup.json', $json);

src/Migration/Exception.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
class Exception extends \Exception implements \JsonSerializable
66
{
7+
public const CODE_VALIDATION = 400;
8+
public const CODE_UNAUTHORIZED = 401;
9+
public const CODE_FORBIDDEN = 403;
10+
public const CODE_NOT_FOUND = 404;
11+
public const CODE_CONFLICT = 409;
12+
public const CODE_RATE_LIMITED = 429;
13+
public const CODE_INTERNAL = 500;
14+
715
public string $resourceName;
816

917
public string $resourceGroup;
@@ -26,7 +34,7 @@ public function __construct(
2634
if (\is_numeric($code)) {
2735
$code = (int) $code;
2836
} else {
29-
$code = 500; // PDOException
37+
$code = self::CODE_INTERNAL; // PDOException
3038
}
3139
}
3240

src/Migration/Resource.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ abstract class Resource implements \JsonSerializable
3030

3131
public const TYPE_DATABASE = 'database';
3232

33+
public const TYPE_DATABASE_LEGACY = 'legacy';
34+
35+
public const TYPE_DATABASE_TABLESDB = 'tablesdb';
36+
37+
public const TYPE_DATABASE_DOCUMENTSDB = 'documentsdb';
38+
public const TYPE_DATABASE_VECTORSDB = 'vectorsdb';
39+
3340
public const TYPE_ROW = 'row';
3441

3542
public const TYPE_FILE = 'file';
@@ -46,6 +53,10 @@ abstract class Resource implements \JsonSerializable
4653

4754
public const TYPE_INDEX = 'index';
4855

56+
public const TYPE_PROVIDER = 'provider';
57+
58+
public const TYPE_TOPIC = 'topic';
59+
4960
// Children (Resources that are created by other resources)
5061

5162
public const TYPE_COLUMN = 'column';
@@ -60,6 +71,10 @@ abstract class Resource implements \JsonSerializable
6071

6172
public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable';
6273

74+
public const TYPE_SUBSCRIBER = 'subscriber';
75+
76+
public const TYPE_MESSAGE = 'message';
77+
6378
// legacy terminologies
6479
public const TYPE_DOCUMENT = 'document';
6580
public const TYPE_ATTRIBUTE = 'attribute';
@@ -76,6 +91,8 @@ abstract class Resource implements \JsonSerializable
7691
self::TYPE_BUCKET,
7792
self::TYPE_TABLE,
7893
self::TYPE_DATABASE,
94+
self::TYPE_DATABASE_VECTORSDB,
95+
self::TYPE_DATABASE_DOCUMENTSDB,
7996
self::TYPE_ROW,
8097
self::TYPE_FILE,
8198
self::TYPE_FUNCTION,
@@ -89,13 +106,50 @@ abstract class Resource implements \JsonSerializable
89106
self::TYPE_ENVIRONMENT_VARIABLE,
90107
self::TYPE_TEAM,
91108
self::TYPE_MEMBERSHIP,
109+
self::TYPE_PROVIDER,
110+
self::TYPE_TOPIC,
111+
self::TYPE_SUBSCRIBER,
112+
self::TYPE_MESSAGE,
92113

93114
// legacy
94115
self::TYPE_DOCUMENT,
95116
self::TYPE_ATTRIBUTE,
96117
self::TYPE_COLLECTION,
97118
];
98119

120+
// index terminology is same for all
121+
public const DATABASE_TYPE_RESOURCE_MAP = [
122+
self::TYPE_DATABASE => [
123+
'entity' => self::TYPE_TABLE,
124+
'field' => self::TYPE_COLUMN,
125+
'record' => self::TYPE_ROW,
126+
],
127+
self::TYPE_DATABASE_DOCUMENTSDB => [
128+
'entity' => self::TYPE_COLLECTION,
129+
// HACK: not required in documentsdb but adding it for consistency in the db reader(not gonna impact)
130+
'field' => self::TYPE_ATTRIBUTE,
131+
'record' => self::TYPE_DOCUMENT,
132+
],
133+
self::TYPE_DATABASE_VECTORSDB => [
134+
'entity' => self::TYPE_COLLECTION,
135+
'field' => self::TYPE_ATTRIBUTE,
136+
'record' => self::TYPE_DOCUMENT,
137+
]
138+
];
139+
140+
public const ENTITY_TYPE_RESOURCE_MAP = [
141+
self::TYPE_TABLE => [
142+
'field' => self::TYPE_COLUMN,
143+
'record' => self::TYPE_ROW,
144+
'index' => self::TYPE_INDEX
145+
],
146+
self::TYPE_COLLECTION => [
147+
'field' => self::TYPE_ATTRIBUTE,
148+
'record' => self::TYPE_DOCUMENT,
149+
'index' => self::TYPE_INDEX
150+
],
151+
];
152+
99153
protected string $id = '';
100154

101155
protected string $originalId = '';

src/Migration/Resources/Auth/User.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class User extends Resource
1919
* @param bool $phoneVerified
2020
* @param bool $disabled
2121
* @param array<string, mixed> $preferences
22+
* @param array<array<string, mixed>> $targets
2223
*/
2324
public function __construct(
2425
string $id,
@@ -31,7 +32,8 @@ public function __construct(
3132
private readonly bool $emailVerified = false,
3233
private readonly bool $phoneVerified = false,
3334
private readonly bool $disabled = false,
34-
private readonly array $preferences = []
35+
private readonly array $preferences = [],
36+
private readonly array $targets = [],
3537
) {
3638
$this->id = $id;
3739
}
@@ -53,7 +55,8 @@ public static function fromArray(array $array): self
5355
$array['emailVerified'] ?? false,
5456
$array['phoneVerified'] ?? false,
5557
$array['disabled'] ?? false,
56-
$array['preferences'] ?? []
58+
$array['preferences'] ?? [],
59+
$array['targets'] ?? [],
5760
);
5861
}
5962

@@ -74,6 +77,7 @@ public function jsonSerialize(): array
7477
'phoneVerified' => $this->phoneVerified,
7578
'disabled' => $this->disabled,
7679
'preferences' => $this->preferences,
80+
'targets' => $this->targets,
7781
];
7882
}
7983

@@ -172,4 +176,14 @@ public function getPreferences(): array
172176
{
173177
return $this->preferences;
174178
}
179+
180+
/**
181+
* Get Targets
182+
*
183+
* @return array<array<string, mixed>>
184+
*/
185+
public function getTargets(): array
186+
{
187+
return $this->targets;
188+
}
175189
}

0 commit comments

Comments
 (0)