diff --git a/playground.php b/playground.php index 91129932..4c46bc08 100644 --- a/playground.php +++ b/playground.php @@ -45,14 +45,14 @@ $_ENV['NHOST_TEST_PASSWORD'] ?? '', ); -$sourceSupabase = new Supabase( - $_ENV['SUPABASE_TEST_ENDPOINT'] ?? '', - $_ENV['SUPABASE_TEST_KEY'] ?? '', - $_ENV['SUPABASE_TEST_HOST'] ?? '', - $_ENV['SUPABASE_TEST_DATABASE'] ?? '', - $_ENV['SUPABASE_TEST_USERNAME'] ?? '', - $_ENV['SUPABASE_TEST_PASSWORD'] ?? '', -); +// $sourceSupabase = new Supabase( +// $_ENV['SUPABASE_TEST_ENDPOINT'] ?? '', +// $_ENV['SUPABASE_TEST_KEY'] ?? '', +// $_ENV['SUPABASE_TEST_HOST'] ?? '', +// $_ENV['SUPABASE_TEST_DATABASE'] ?? '', +// $_ENV['SUPABASE_TEST_USERNAME'] ?? '', +// $_ENV['SUPABASE_TEST_PASSWORD'] ?? '', +// ); /** * Initialise All Destination Adapters @@ -69,16 +69,16 @@ * Initialise Transfer Class */ $transfer = new Transfer( - $sourceFirebase, + $sourceAppwrite, $destinationLocal ); -$sourceFirebase->report(); +$sourceAppwrite->report(); // /** // * Run Transfer // */ -$transfer->run($sourceFirebase->getSupportedResources(), +$transfer->run(Appwrite::getSupportedResources(), function (array $resources) { } ); diff --git a/src/Migration/Cache.php b/src/Migration/Cache.php index b25545da..1eb505c7 100644 --- a/src/Migration/Cache.php +++ b/src/Migration/Cache.php @@ -2,7 +2,6 @@ namespace Utopia\Migration; -use Utopia\Migration\Resources\Functions\Func; use Utopia\Migration\Resources\Storage\File; /** @@ -37,8 +36,8 @@ public function add($resource) $resource->setInternalId(uniqid()); } - if ($resource->getName() == Resource::TYPE_FILE || $resource->getName() == Resource::TYPE_FUNCTION) { - /** @var File|Func $resource */ + if ($resource->getName() == Resource::TYPE_FILE || $resource->getName() == Resource::TYPE_DEPLOYMENT) { + /** @var File|Deployment $resource */ $resource->setData(''); // Prevent Memory Leak } diff --git a/src/Migration/Destinations/Local.php b/src/Migration/Destinations/Local.php index 11156d89..457ea904 100644 --- a/src/Migration/Destinations/Local.php +++ b/src/Migration/Destinations/Local.php @@ -104,7 +104,7 @@ protected function import(array $resources, callable $callback): void case Resource::TYPE_DEPLOYMENT: /** @var Deployment $resource */ if ($resource->getStart() === 0) { - $this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray(); + $this->data[$resource->getGroup()][$resource->getName()][] = $resource->asArray(); } file_put_contents($this->path.'deployments/'.$resource->getId().'.tar.gz', $resource->getData(), FILE_APPEND); @@ -135,7 +135,7 @@ protected function import(array $resources, callable $callback): void $resource->setData(''); break; default: - $this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray(); + $this->data[$resource->getGroup()][$resource->getName()][] = $resource->asArray(); break; } diff --git a/src/Migration/Resource.php b/src/Migration/Resource.php index bd294fa2..825da5c2 100644 --- a/src/Migration/Resource.php +++ b/src/Migration/Resource.php @@ -44,16 +44,18 @@ abstract class Resource public const TYPE_INDEX = 'index'; + public const TYPE_INACTIVE_DEPLOYMENTS = 'inactive-deployments'; + + public const TYPE_DEPLOYMENT = 'deployment'; + + public const TYPE_ENVVAR = 'envvar'; + // Children (Resources that are created by other resources) public const TYPE_ATTRIBUTE = 'attribute'; - public const TYPE_DEPLOYMENT = 'deployment'; - public const TYPE_HASH = 'hash'; - public const TYPE_ENVVAR = 'envvar'; - public const ALL_RESOURCES = [ self::TYPE_ATTRIBUTE, self::TYPE_BUCKET, diff --git a/src/Migration/Resources/Functions/Func.php b/src/Migration/Resources/Functions/Func.php index e6760bef..2a99e970 100644 --- a/src/Migration/Resources/Functions/Func.php +++ b/src/Migration/Resources/Functions/Func.php @@ -21,7 +21,9 @@ class Func extends Resource protected int $timeout; - public function __construct(string $name, string $id, string $runtime, array $execute = [], bool $enabled = true, array $events = [], string $schedule = '', int $timeout = 0) + protected string $deployment; + + public function __construct(string $name, string $id, string $runtime, array $execute = [], bool $enabled = true, array $events = [], string $schedule = '', int $timeout = 0, string $deployment = '') { $this->name = $name; $this->id = $id; @@ -31,6 +33,7 @@ public function __construct(string $name, string $id, string $runtime, array $ex $this->events = $events; $this->schedule = $schedule; $this->timeout = $timeout; + $this->deployment = $deployment; } public static function getName(): string @@ -120,6 +123,18 @@ public function setTimeout(int $timeout): self return $this; } + public function getDeployment(): string + { + return $this->deployment; + } + + public function setDeployment(string $deployment): self + { + $this->deployment = $deployment; + + return $this; + } + public function asArray(): array { return [ @@ -131,6 +146,7 @@ public function asArray(): array 'events' => $this->events, 'schedule' => $this->schedule, 'timeout' => $this->timeout, + 'deployment' => $this->deployment, ]; } } diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index bc4a9b0d..310abf3f 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -101,6 +101,7 @@ public static function getSupportedResources(): array // Functions Resource::TYPE_FUNCTION, Resource::TYPE_DEPLOYMENT, + Resource::TYPE_INACTIVE_DEPLOYMENTS, Resource::TYPE_ENVVAR, // Settings @@ -987,15 +988,15 @@ private function exportFileData(File $file) protected function exportGroupFunctions(int $batchSize, array $resources) { if (in_array(Resource::TYPE_FUNCTION, $resources)) { - $this->exportFunctions($batchSize); + $this->exportFunctions($batchSize, $resources); } if (in_array(Resource::TYPE_DEPLOYMENT, $resources)) { - $this->exportDeployments($batchSize); + $this->exportDeployments($batchSize, in_array(Resource::TYPE_INACTIVE_DEPLOYMENTS, $resources)); } } - private function exportFunctions(int $batchSize) + private function exportFunctions(int $batchSize, array $resources) { $functionsClient = new Functions($this->client); @@ -1016,24 +1017,27 @@ private function exportFunctions(int $batchSize) $function['enabled'], $function['events'], $function['schedule'], - $function['timeout'] + $function['timeout'], + $function['deployment'] ); $convertedResources[] = $convertedFunc; - foreach ($function['vars'] as $var) { - $convertedResources[] = new EnvVar( - $convertedFunc, - $var['key'], - $var['value'], - ); + if (in_array(Resource::TYPE_ENVVAR, $resources)) { + foreach ($function['vars'] as $var) { + $convertedResources[] = new EnvVar( + $convertedFunc, + $var['key'], + $var['value'], + ); + } } } $this->callback($convertedResources); } - private function exportDeployments(int $batchSize) + private function exportDeployments(int $batchSize, bool $includeInactive = false) { $functionsClient = new Functions($this->client); $functions = $this->cache->get(Func::getName()); @@ -1061,6 +1065,10 @@ private function exportDeployments(int $batchSize) ); foreach ($response['deployments'] as $deployment) { + if (! $includeInactive && ($deployment['$id'] != $func->getDeployment())) { + continue; + } + $this->exportDeploymentData($func, $deployment); $lastDocument = $deployment['$id']; @@ -1097,13 +1105,13 @@ private function exportDeploymentData(Func $func, array $deployment) $deployment['activate'] ); - $deployment->setInternalId($deployment->getId()); + $deployment->setOriginalId($deployment->getId()); // Loop until the entire file is downloaded while ($start < $fileSize) { $chunkData = $this->call( 'GET', - "/functions/{$func->getId()}/deployments/{$deployment->getInternalId()}/download", + "/functions/{$func->getId()}/deployments/{$deployment->getOriginalId()}/download", ['range' => "bytes=$start-$end"] );