Skip to content

Commit

Permalink
Merge branch 'backups' of github.com:utopia-php/migration into backups
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Migration/Source.php
  • Loading branch information
abnegate committed Aug 6, 2024
2 parents b90f593 + 9088c29 commit 577ff40
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
5 changes: 2 additions & 3 deletions src/Migration/Destination.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ public function setSource(Source $source): self
* @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
* @param int $batchSize The number of resources to transfer in a single batch
*/
public function run(
array $resources,
callable $callback,
string $rootResourceId = '',
int $batchSize = 100,
string $rootResourceType = '',
): void {
$this->source->run(
$resources,
function (array $resources) use ($callback) {
$this->import($resources, $callback);
},
$rootResourceId,
$batchSize
$rootResourceType,
);
}

Expand Down
5 changes: 0 additions & 5 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1337,9 +1337,4 @@ private function importDeployment(Deployment $deployment): Resource

return $deployment;
}

public function getBatchSize(): int
{
return 200;
}
}
21 changes: 10 additions & 11 deletions src/Migration/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ public function callback(array $resources): void
* @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 = '',
int $batchSize = 100
): void
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 = [];
Expand All @@ -54,21 +50,20 @@ public function run(
$this->cache->addAll($prunedResources);
};

$batchSize = $this->getBatchSize();

$this->exportResources($resources, $batchSize);
$this->exportResources($resources);
}

/**
* Export Resources
*
* @param array<string> $resources Resources to export
* @param int $batchSize Max 100
*/
public function exportResources(array $resources, int $batchSize): void
public function exportResources(array $resources): void
{
// Convert Resources back into their relevant groups

$batchSize = $this->getBatchSize();

$groups = [];
foreach ($resources as $resource) {
if (\in_array($resource, Transfer::GROUP_AUTH_RESOURCES)) {
Expand Down Expand Up @@ -104,6 +99,10 @@ public function exportResources(array $resources, int $batchSize): void
}
}
}
public function getBatchSize(): int
{
return 100;
}

/**
* Export Auth Group
Expand Down
17 changes: 11 additions & 6 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private function exportUsers(int $batchSize): void

$queries = [Query::limit($batchSize)];

if (!empty($this->rootResourceId)) {
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_USER) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}
Expand Down Expand Up @@ -433,7 +433,7 @@ private function exportTeams(int $batchSize): void

$queries = [Query::limit($batchSize)];

if (!empty($this->rootResourceId)) {
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_TEAM) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}
Expand Down Expand Up @@ -872,7 +872,7 @@ private function exportDatabases(int $batchSize): void
while (true) {
$queries = [Query::limit($batchSize)];

if (!empty($this->rootResourceId)) {
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_DATABASE) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ private function exportBuckets(int $batchSize): void
{
$queries = [];

if (!empty($this->rootResourceId)) {
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_BUCKET) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}
Expand Down Expand Up @@ -1284,7 +1284,7 @@ private function exportFunctions(int $batchSize): void

$queries = [];

if (!empty($this->rootResourceId)) {
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_FUNCTION) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}
Expand Down Expand Up @@ -1471,4 +1471,9 @@ private function exportDeploymentData(Func $func, array $deployment): void
}
}
}
}

public function getBatchSize(): int
{
return 250;
}
}
7 changes: 2 additions & 5 deletions src/Migration/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract class Target

protected string $rootResourceId = '';

protected string $rootResourceType = '';

abstract public static function getName(): string;

abstract public static function getSupportedResources(): array;
Expand Down Expand Up @@ -225,9 +227,4 @@ public function addWarning(Warning $warning): void
public function shutdown(): void
{
}

public function getBatchSize(): int
{
return 100;
}
}
19 changes: 10 additions & 9 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,19 @@ public function getStatusCounters(): array
* @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
* @param int $batchSize
* @throws \Exception
*/
public function run(
array $resources,
callable $callback,
string $rootResourceId = null,
int $batchSize = 100
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)) {
$computedResources = array_merge($computedResources, $resource);
Expand All @@ -202,13 +204,12 @@ public function run(

$computedResources = array_map('strtolower', $computedResources);

// Check we don't have multiple root resources if rootResourceId is set
$rootResourceId = $rootResourceId ?? '';
if ($rootResourceId) {
$rootResourceCount = \count(\array_intersect($computedResources, self::ROOT_RESOURCES));
if ($rootResourceCount > 1) {
throw new \Exception('Multiple root resources found. Only one root resource can be transferred at a time if using $rootResourceId.');
if ($rootResourceId !== '') {
if ($rootResourceType === '') {
throw new \Exception('Please $rootResourceId while using $rootResourceId');
}

$computedResources = [$rootResourceType];
}

$this->resources = $computedResources;
Expand All @@ -217,7 +218,7 @@ public function run(
$computedResources,
$callback,
$rootResourceId,
$batchSize
$rootResourceType,
);
}

Expand Down

0 comments on commit 577ff40

Please sign in to comment.