-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSource.php
More file actions
214 lines (187 loc) · 6.77 KB
/
Source.php
File metadata and controls
214 lines (187 loc) · 6.77 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
<?php
namespace Utopia\Migration;
abstract class Source extends Target
{
protected static int $defaultBatchSize = 100;
/**
* @var callable(array<Resource>): void $transferCallback
*/
protected $transferCallback;
/**
* @var array<string, int>
*/
public array $previousReport = [];
public function getAuthBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getDatabasesBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getStorageBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getFunctionsBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getMessagingBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getSitesBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getBackupsBatchSize(): int
{
return static::$defaultBatchSize;
}
/**
* @param array<Resource> $resources
* @return void
*/
public function callback(array $resources): void
{
($this->transferCallback)($resources);
}
/**
* Transfer Resources into destination
*
* @param array<string> $resources Resources to transfer
* @param callable $callback Callback to run after transfer
* @param string $rootResourceId Root resource ID, If enabled you can only transfer a single root resource
*/
public function run(array $resources, callable $callback, string $rootResourceId = '', string $rootResourceType = ''): void
{
$this->rootResourceId = $rootResourceId;
$this->rootResourceType = $rootResourceType;
$this->transferCallback = function (array $returnedResources) use ($callback, $resources) {
$prunedResources = [];
foreach ($returnedResources as $resource) {
/** @var Resource $resource */
if (!in_array($resource->getName(), $resources)) {
$resource->setStatus(Resource::STATUS_SKIPPED);
} else {
/**
* Rows and documents are counted in cache in an aggregated way (per status count)
* to prevent memory blowups. Destination updates already account for these, and
* adding them here causes double counting (source + destination).
*/
if (\in_array($resource->getName(), [Resource::TYPE_ROW, Resource::TYPE_DOCUMENT], true)) {
continue;
}
$prunedResources[] = $resource;
}
}
$callback($returnedResources);
$this->cache->addAll($prunedResources);
};
$this->exportResources($resources);
}
/**
* Export Resources
*
* @param array<string> $resources Resources to export
*/
public function exportResources(array $resources): void
{
$groups = [];
foreach ($resources as $resource) {
$mapping = [
Transfer::GROUP_AUTH => Transfer::GROUP_AUTH_RESOURCES,
Transfer::GROUP_DATABASES => Transfer::GROUP_DATABASES_RESOURCES,
Transfer::GROUP_STORAGE => Transfer::GROUP_STORAGE_RESOURCES,
Transfer::GROUP_FUNCTIONS => Transfer::GROUP_FUNCTIONS_RESOURCES,
Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES,
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
];
foreach ($mapping as $group => $resources) {
if (\in_array($resource, $resources, true)) {
$groups[$group][] = $resource;
break;
}
}
}
if (empty($groups)) {
return;
}
foreach ($groups as $group => $resources) {
switch ($group) {
case Transfer::GROUP_AUTH:
$this->exportGroupAuth($this->getAuthBatchSize(), $resources);
break;
case Transfer::GROUP_DATABASES:
$this->exportGroupDatabases($this->getDatabasesBatchSize(), $resources);
break;
case Transfer::GROUP_STORAGE:
$this->exportGroupStorage($this->getStorageBatchSize(), $resources);
break;
case Transfer::GROUP_FUNCTIONS:
$this->exportGroupFunctions($this->getFunctionsBatchSize(), $resources);
break;
case Transfer::GROUP_MESSAGING:
$this->exportGroupMessaging($this->getMessagingBatchSize(), $resources);
break;
case Transfer::GROUP_SITES:
$this->exportGroupSites($this->getSitesBatchSize(), $resources);
break;
case Transfer::GROUP_BACKUPS:
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
break;
}
}
}
/**
* Export Auth Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupAuth(int $batchSize, array $resources): void;
/**
* Export Databases Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupDatabases(int $batchSize, array $resources): void;
/**
* Export Storage Group
*
* @param int $batchSize Max 5
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupStorage(int $batchSize, array $resources): void;
/**
* Export Functions Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupFunctions(int $batchSize, array $resources): void;
/**
* Export Messaging Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupMessaging(int $batchSize, array $resources): void;
/**
* Export Sites Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupSites(int $batchSize, array $resources): void;
/**
* Export Backups Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupBackups(int $batchSize, array $resources): void;
}