From 20ef3a843395a98d484b187356ac08578265ad60 Mon Sep 17 00:00:00 2001 From: Jiri Semmler Date: Tue, 16 Dec 2025 21:34:23 +0100 Subject: [PATCH 1/2] batch switch backend --- .../Command/OrganizationStorageBackend.php | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Keboola/Console/Command/OrganizationStorageBackend.php b/src/Keboola/Console/Command/OrganizationStorageBackend.php index f0ce408..cb2f6be 100644 --- a/src/Keboola/Console/Command/OrganizationStorageBackend.php +++ b/src/Keboola/Console/Command/OrganizationStorageBackend.php @@ -39,11 +39,9 @@ protected function configure(): void InputArgument::OPTIONAL, 'Keboola Connection Hostname Suffix', 'keboola.com' - ) - ; + ); } - protected function execute(InputInterface $input, OutputInterface $output): int { $storageBackendId = $input->getArgument(self::ARGUMENT_STORAGE_BACKEND_ID); @@ -80,18 +78,47 @@ protected function execute(InputInterface $input, OutputInterface $output): int $storageBackendId ) ); - $params = [$project['id'], $storageBackendId]; + $params = [(string) $project['id'], (string) $storageBackendId]; if ($force) { $params[] = '--force'; - $manageClient->runCommand([ + $result = $manageClient->runCommand([ 'command' => 'manage:switch-storage-backend', - 'parameters' => $params + 'parameters' => $params, ]); - $manageClient->assignProjectStorageBackend($project['id'], $storageBackendId); + $output->writeln(sprintf('INFO: Storage backend switch for project "%s" in progress using command "%s".', $project['id'], $result['commandExecutionId'])); + + if ($this->waitForProjectToMigrate($manageClient, $project['id'], (int) $storageBackendId, $output)) { + $output->writeln(sprintf('SUSCCESS: Storage backend switch for project "%s" in progress using command "%s".', $project['id'], $result['commandExecutionId'])); + } else { + $output->writeln(sprintf('ERROR: Storage backend switch for project "%s" to backend "%s" timed out.', $project['id'], $storageBackendId)); + } } } $output->writeln('All done.'); - + return 0; } + + private function waitForProjectToMigrate( + Client $manageClient, + int $projectId, + int $requestedProjectId, + OutputInterface $output, + ): bool { + $timeout = 300; + $start = time(); + while (time() - $start < $timeout) { + $projectDetail = $manageClient->getProject($projectId); + $currentBackednId = (int) $projectDetail['backends']['snowflake']['id']; + + if ($currentBackednId === $requestedProjectId) { + $output->writeln(sprintf(' - Project "%s" migrated successfully to backend "%s".', $projectId, $requestedProjectId)); + return true; + } + $output->writeln(sprintf(' - Project "%s" did not migrate to backend "%s" yet...waiting', $projectId, $requestedProjectId)); + sleep(2); + } + + return false; + } } From 5b804d1dcb78661f6b05fb2a1dad5722091c0ec3 Mon Sep 17 00:00:00 2001 From: Jiri Semmler Date: Fri, 19 Dec 2025 14:07:43 +0100 Subject: [PATCH 2/2] wait a second --- src/Keboola/Console/Command/OrganizationStorageBackend.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Keboola/Console/Command/OrganizationStorageBackend.php b/src/Keboola/Console/Command/OrganizationStorageBackend.php index cb2f6be..9361d24 100644 --- a/src/Keboola/Console/Command/OrganizationStorageBackend.php +++ b/src/Keboola/Console/Command/OrganizationStorageBackend.php @@ -89,6 +89,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($this->waitForProjectToMigrate($manageClient, $project['id'], (int) $storageBackendId, $output)) { $output->writeln(sprintf('SUSCCESS: Storage backend switch for project "%s" in progress using command "%s".', $project['id'], $result['commandExecutionId'])); + // wait a second so the lock can be released + sleep(5); } else { $output->writeln(sprintf('ERROR: Storage backend switch for project "%s" to backend "%s" timed out.', $project['id'], $storageBackendId)); }