-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTransfer.php
More file actions
416 lines (346 loc) · 12.9 KB
/
Transfer.php
File metadata and controls
416 lines (346 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
namespace Utopia\Migration;
class Transfer
{
public const GROUP_GENERAL = 'general';
public const GROUP_AUTH = 'auth';
public const GROUP_STORAGE = 'storage';
public const GROUP_FUNCTIONS = 'functions';
public const GROUP_SITES = 'sites';
// separating databases and tablesdb out for easier separation in extract services
// migration can use group_databases for mentioning all resources but when mentioning specific resources go with specific type databases
public const GROUP_DATABASES = 'databases';
public const GROUP_DATABASES_TABLES_DB = 'tablesdb';
public const GROUP_DATABASES_DOCUMENTS_DB = 'documentsdb';
public const GROUP_DATABASES_VECTOR_DB = 'vectorsdb';
public const GROUP_SETTINGS = 'settings';
public const GROUP_MESSAGING = 'messaging';
public const GROUP_BACKUPS = 'backups';
public const GROUP_AUTH_RESOURCES = [
Resource::TYPE_USER,
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_HASH
];
public const GROUP_STORAGE_RESOURCES = [
Resource::TYPE_FILE,
Resource::TYPE_BUCKET
];
public const GROUP_FUNCTIONS_RESOURCES = [
Resource::TYPE_FUNCTION,
Resource::TYPE_ENVIRONMENT_VARIABLE,
Resource::TYPE_DEPLOYMENT
];
public const GROUP_SITES_RESOURCES = [
Resource::TYPE_SITE,
Resource::TYPE_SITE_VARIABLE,
Resource::TYPE_SITE_DEPLOYMENT
];
public const GROUP_TABLESDB_RESOURCES = [
Resource::TYPE_DATABASE,
Resource::TYPE_TABLE,
Resource::TYPE_INDEX,
Resource::TYPE_COLUMN,
Resource::TYPE_ROW,
];
public const GROUP_DOCUMENTSDB_RESOURCES = [
Resource::TYPE_DATABASE_DOCUMENTSDB,
Resource::TYPE_COLLECTION,
Resource::TYPE_INDEX,
Resource::TYPE_DOCUMENT
];
public const GROUP_VECTORSDB_RESOURCES = [
Resource::TYPE_DATABASE_VECTORSDB,
Resource::TYPE_COLLECTION,
Resource::TYPE_ATTRIBUTE,
Resource::TYPE_INDEX,
Resource::TYPE_DOCUMENT
];
public const GROUP_DATABASES_RESOURCES = [
Resource::TYPE_DATABASE,
Resource::TYPE_DATABASE_DOCUMENTSDB,
Resource::TYPE_DATABASE_VECTORSDB,
Resource::TYPE_TABLE,
Resource::TYPE_INDEX,
Resource::TYPE_COLUMN,
Resource::TYPE_ROW,
Resource::TYPE_DOCUMENT,
Resource::TYPE_COLLECTION,
Resource::TYPE_ATTRIBUTE
];
public const GROUP_SETTINGS_RESOURCES = [];
public const GROUP_BACKUPS_RESOURCES = [
Resource::TYPE_BACKUP_POLICY,
];
public const GROUP_MESSAGING_RESOURCES = [
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
];
public const ALL_PUBLIC_RESOURCES = [
Resource::TYPE_USER,
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_FILE,
Resource::TYPE_BUCKET,
Resource::TYPE_FUNCTION,
Resource::TYPE_ENVIRONMENT_VARIABLE,
Resource::TYPE_DEPLOYMENT,
Resource::TYPE_SITE,
Resource::TYPE_SITE_VARIABLE,
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_DATABASE,
Resource::TYPE_TABLE,
Resource::TYPE_INDEX,
Resource::TYPE_COLUMN,
Resource::TYPE_ROW,
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,
// legacy
Resource::TYPE_DOCUMENT,
Resource::TYPE_ATTRIBUTE,
Resource::TYPE_COLLECTION,
];
public const ROOT_RESOURCES = [
Resource::TYPE_BUCKET,
Resource::TYPE_DATABASE,
Resource::TYPE_DATABASE_DOCUMENTSDB,
Resource::TYPE_DATABASE_VECTORSDB,
Resource::TYPE_FUNCTION,
Resource::TYPE_SITE,
Resource::TYPE_USER,
Resource::TYPE_TEAM,
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
Resource::TYPE_MESSAGE,
];
public const STORAGE_MAX_CHUNK_SIZE = 1024 * 1024 * 5; // 5MB
protected Source $source;
protected Destination $destination;
protected string $currentResource;
/**
* A local cache of resources that were transferred.
*/
protected Cache $cache;
/**
* @var array<string, mixed>
*/
protected array $options = [];
/**
* @var array<string, mixed>
*/
protected array $events = [];
/**
* @var array<string, mixed>
*/
protected array $resources = [];
public function __construct(Source $source, Destination $destination)
{
$this->source = $source;
$this->destination = $destination;
$this->cache = new Cache();
$this->source->registerCache($this->cache);
$this->destination->registerCache($this->cache);
$this->destination->setSource($source);
}
public function getStatusCounters(): array
{
$status = [];
foreach ($this->resources as $resource) {
$status[$resource] = [
Resource::STATUS_PENDING => 0,
Resource::STATUS_SUCCESS => 0,
Resource::STATUS_ERROR => 0,
Resource::STATUS_SKIPPED => 0,
Resource::STATUS_PROCESSING => 0,
Resource::STATUS_WARNING => 0,
];
}
if ($this->source->previousReport) {
foreach ($this->source->previousReport as $resource => $data) {
if ($resource != 'size' && $resource != 'version' && isset($status[$resource])) {
$status[$resource]['pending'] = $data;
}
}
}
foreach ($this->cache->getAll() as $resourceType => $resources) {
foreach ($resources as $k => $resource) {
if (($resourceType === Resource::TYPE_ROW || $resourceType === Resource::TYPE_DOCUMENT) && is_string($resource)) {
$resource = intval($resource);
$status[$resourceType][$k] = $resource;
if ($status[$resourceType]['pending'] > 0) {
$status[$resourceType]['pending'] -= \min($status[$resourceType]['pending'], $resource);
}
continue;
}
if (isset($status[$resource->getName()])) {
$status[$resource->getName()][$resource->getStatus()]++;
if ($status[$resource->getName()]['pending'] > 0) {
$status[$resource->getName()]['pending']--;
}
}
}
}
// Process Destination Errors
foreach ($this->destination->getErrors() as $error) {
if (isset($status[$error->getResourceGroup()])) {
$status[$error->getResourceGroup()][Resource::STATUS_ERROR]++;
}
}
// Process source errors
foreach ($this->source->getErrors() as $error) {
if (isset($status[$error->getResourceGroup()])) {
$status[$error->getResourceGroup()][Resource::STATUS_ERROR]++;
}
}
// Remove all empty resources
foreach ($status as $resource => $data) {
$allEmpty = true;
foreach ($data as $count) {
if ($count > 0) {
$allEmpty = false;
}
}
if ($allEmpty) {
unset($status[$resource]);
}
}
return $status;
}
/**
* Transfer Resources between adapters
*
* @param array<string> $resources Resources to transfer
* @param callable $callback Callback to run after transfer
* @param string|null $rootResourceId Root resource ID, If enabled you can only transfer a single root resource
* @throws \Exception
*/
public function run(
array $resources,
callable $callback,
?string $rootResourceId = null,
?string $rootResourceType = null,
): void {
// Allows you to push entire groups if you want.
$computedResources = [];
$rootResourceId = $rootResourceId ?? '';
$rootResourceType = $rootResourceType ?? '';
foreach ($resources as $resource) {
if (is_array($resource)) {
/** @var array<string> $resource */
$computedResources = array_merge($computedResources, $resource);
} else {
$computedResources[] = $resource;
}
}
/** @var array<string> $computedResources */
$computedResources = array_map('strtolower', $computedResources);
if ($rootResourceId !== '') {
if ($rootResourceType === '') {
throw new \Exception('Resource type must be set when resource ID is set.');
}
if (!in_array($rootResourceType, self::ROOT_RESOURCES)) {
throw new \Exception('Got '.$rootResourceType.' Resource type must be one of ' . implode(', ', self::ROOT_RESOURCES));
}
$rootResources = \array_intersect($computedResources, self::ROOT_RESOURCES);
if (\count($rootResources) > 1) {
throw new \Exception('Multiple root resources found. Only one root resource can be transferred at a time if using $rootResourceId.');
}
if (\count($rootResources) === 0) {
throw new \Exception('No root resources found.');
}
}
$this->resources = $computedResources;
$this->destination->run(
$computedResources,
$callback,
$rootResourceId,
$rootResourceType,
);
}
/**
* Get Cache
*/
public function getCache(): Cache
{
return $this->cache;
}
/**
* Get Current Resource
*
**/
public function getCurrentResource(): string
{
return $this->currentResource;
}
/**
* Get Transfer Report
*
* @param string $statusLevel If no status level is provided, all status types will be returned.
* @return array<array<string, mixed>>
*/
public function getReport(string $statusLevel = ''): array
{
$report = [];
$cache = $this->cache->getAll();
foreach ($cache as $type => $resources) {
foreach ($resources as $id => $resource) {
if (($type === Resource::TYPE_ROW || $type === Resource::TYPE_DOCUMENT) && is_string($resource)) {
if ($statusLevel && $resource !== $statusLevel) {
continue;
}
// no message for row is stored
$report[] = [
'resource' => $type,
'id' => $id,
'status' => $resource,
'message' => '',
];
continue;
}
if (!is_object($resource)) {
continue;
}
if ($statusLevel && $resource->getStatus() !== $statusLevel) {
continue;
}
$report[] = [
'resource' => $type,
'id' => $resource->getId(),
'status' => $resource->getStatus(),
'message' => $resource->getMessage(),
];
}
}
return $report;
}
/**
* @throws \Exception
*/
public static function extractServices(array $services): array
{
$resources = [];
foreach ($services as $service) {
$resources = match ($service) {
self::GROUP_FUNCTIONS => array_merge($resources, self::GROUP_FUNCTIONS_RESOURCES),
self::GROUP_SITES => array_merge($resources, self::GROUP_SITES_RESOURCES),
self::GROUP_STORAGE => array_merge($resources, self::GROUP_STORAGE_RESOURCES),
self::GROUP_GENERAL => array_merge($resources, []),
self::GROUP_AUTH => array_merge($resources, self::GROUP_AUTH_RESOURCES),
self::GROUP_DATABASES => array_merge($resources, self::GROUP_DATABASES_RESOURCES),
self::GROUP_DATABASES_TABLES_DB => array_merge($resources, self::GROUP_TABLESDB_RESOURCES),
self::GROUP_DATABASES_DOCUMENTS_DB => array_merge($resources, self::GROUP_DOCUMENTSDB_RESOURCES),
self::GROUP_DATABASES_VECTOR_DB => array_merge($resources, self::GROUP_VECTORSDB_RESOURCES),
self::GROUP_SETTINGS => array_merge($resources, self::GROUP_SETTINGS_RESOURCES),
self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES),
self::GROUP_BACKUPS => array_merge($resources, self::GROUP_BACKUPS_RESOURCES),
default => throw new \Exception('No service group found'),
};
}
return $resources;
}
}