Skip to content
Merged
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
45 changes: 37 additions & 8 deletions src/Keboola/Console/Command/OrganizationStorageBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -80,18 +78,49 @@ 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']));
// 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));
}
}
}
$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;
}
}