Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Deployment Transfers #13

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions playground.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
}
);
Expand Down
5 changes: 2 additions & 3 deletions src/Migration/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Utopia\Migration;

use Utopia\Migration\Resources\Functions\Func;
use Utopia\Migration\Resources\Storage\File;

/**
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions src/Migration/Destinations/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 17 additions & 1 deletion src/Migration/Resources/Functions/Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 [
Expand All @@ -131,6 +146,7 @@ public function asArray(): array
'events' => $this->events,
'schedule' => $this->schedule,
'timeout' => $this->timeout,
'deployment' => $this->deployment,
];
}
}
34 changes: 21 additions & 13 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static function getSupportedResources(): array
// Functions
Resource::TYPE_FUNCTION,
Resource::TYPE_DEPLOYMENT,
Resource::TYPE_INACTIVE_DEPLOYMENTS,
Resource::TYPE_ENVVAR,

// Settings
Expand Down Expand Up @@ -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);

Expand All @@ -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());
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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"]
);

Expand Down