From 919b5ac06ded738afa697f9258f38f7d582526bc Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 12 Aug 2024 11:15:14 +0300 Subject: [PATCH] Use document credentials --- src/Appwrite/Platform/Workers/Migrations.php | 41 +++++++++++++------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 7cad7f8b5e..1ae127dc03 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -38,7 +38,7 @@ class Migrations extends Action protected Document $project; - protected array $credentials = []; + //protected array $credentials = []; public static function getName(): string { @@ -127,9 +127,9 @@ class Migrations extends Action $credentials['port'], ), SourceAppwrite::getName() => new SourceAppwrite( - $this->credentials['projectId'], - $this->credentials['endpoint'], - $this->credentials['apiKey'] + $credentials['projectId'], + $credentials['endpoint'], + $credentials['apiKey'], ), default => throw new \Exception('Invalid source type'), }; @@ -141,12 +141,13 @@ class Migrations extends Action protected function processDestination(Document $migration): Destination { $destination = $migration->getAttribute('destination'); + $credentials = $migration->getAttribute('credentials'); return match ($destination) { DestinationAppwrite::getName() => new DestinationAppwrite( - $this->credentials['projectId'], - $this->credentials['endpoint'], - $this->credentials['apiKey'], + $credentials['projectId'], + $credentials['endpoint'], + $credentials['apiKey'], $this->dbForProject, Config::getParam('collections', [])['databases']['collections'], ), @@ -269,16 +270,30 @@ class Migrations extends Action $projectDocument = $this->dbForConsole->getDocument('projects', $project->getId()); $tempAPIKey = $this->generateAPIKey($projectDocument); - $this->credentials = [ - 'projectId' => $projectDocument->getId(), - 'endpoint' => 'http://appwrite/v1', - 'apiKey' => $tempAPIKey['secret'], - ]; - + // $this->credentials = [ + // 'projectId' => $projectDocument->getId(), + // 'endpoint' => 'http://appwrite/v1', + // 'apiKey' => $tempAPIKey['secret'], + // ]; + $transfer = $source = $destination = null; try { $migration = $this->dbForProject->getDocument('migrations', $migration->getId()); + + if ( + $migration->getAttribute('source') === SourceAppwrite::getName() || + $migration->getAttribute('destination') === DestinationAppwrite::getName() + ) { + $credentials = $migration->getAttribute('credentials', []); + + $credentials['projectId'] = $credentials['projectId'] ?? $projectDocument->getId(); + $credentials['endpoint'] = $credentials['endpoint'] ?? 'http://appwrite/v1'; + $credentials['apiKey'] = $credentials['apiKey'] ?? $tempAPIKey['secret']; + + $migration->setAttribute('credentials', $credentials); + } + $migration->setAttribute('stage', 'processing'); $migration->setAttribute('status', 'processing'); $log->addBreadcrumb(new Breadcrumb('debug', 'migration', "Migration hit stage 'processing'", \microtime(true)));