new deployer command

This commit is contained in:
Mark Gregory
2026-09-16 13:22:16 +01:00
parent fac5df8cc3
commit 3410ab85fe
15 changed files with 26429 additions and 441 deletions
+21 -44
View File
@@ -18,31 +18,28 @@ class DatabaseStage extends Command
{
$local = $this->localConfiguration();
$stage = $this->stageConfiguration();
$exitCode = self::SUCCESS;
if (! $this->validateStageConfiguration($stage)) {
return self::FAILURE;
$exitCode = self::FAILURE;
} else {
[$localDump, $remoteDump] = $this->dumpPaths();
$this->displayCloneSummary($local, $stage);
if (! $this->confirmed($stage)) {
$this->warn('Cancelled.');
} else {
try {
$this->runClone($local, $stage, $localDump, $remoteDump);
$this->reportSuccess($local, $stage);
} catch (\Throwable $exception) {
$this->reportFailure($exception, $localDump, $remoteDump);
$exitCode = self::FAILURE;
}
}
}
[$localDump, $remoteDump] = $this->dumpPaths();
$this->displayCloneSummary($local, $stage);
if (! $this->confirmed($stage)) {
$this->warn('Cancelled.');
return self::SUCCESS;
}
try {
$this->runClone($local, $stage, $localDump, $remoteDump);
} catch (\Throwable $exception) {
$this->reportFailure($exception, $localDump, $remoteDump);
return self::FAILURE;
}
$this->reportSuccess($local, $stage);
return self::SUCCESS;
return $exitCode;
}
/** @return array<string, mixed> */
@@ -130,7 +127,7 @@ class DatabaseStage extends Command
{
$this->ensureDumpDirectory($localDump);
$this->createDump($local, $localDump);
$this->line(' Dump size: ' . $this->formatBytes((int) filesize($localDump)));
$this->line(' Dump size: ' . number_format((int) filesize($localDump) / 1048576, 2) . ' MB');
$this->verifyDump($localDump);
$this->uploadDump($stage, $localDump, $remoteDump);
$this->recreateStageDatabase($stage);
@@ -195,8 +192,8 @@ class DatabaseStage extends Command
private function recreateStageDatabase(array $stage): void
{
$this->components->task('Recreating stage database', function () use ($stage): bool {
$database = $this->quoteSqlIdentifier((string) $stage['database']);
$owner = $this->quoteSqlIdentifier((string) $stage['username']);
$database = '"' . str_replace('"', '""', (string) $stage['database']) . '"';
$owner = '"' . str_replace('"', '""', (string) $stage['username']) . '"';
$terminateDatabase = str_replace("'", "''", (string) $stage['database']);
$sql = "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{$terminateDatabase}' AND pid <> pg_backend_pid();\n"
. "DROP DATABASE IF EXISTS {$database};\nCREATE DATABASE {$database} WITH OWNER {$owner} ENCODING 'UTF8' TEMPLATE template0;";
@@ -291,24 +288,4 @@ class DatabaseStage extends Command
$this->components->info('Stage PostgreSQL database cloned successfully.');
$this->line("Local {$local['database']} → Stage {$stage['database']}");
}
private function quoteSqlIdentifier(string $identifier): string
{
return '"' . str_replace('"', '""', $identifier) . '"';
}
private function formatBytes(int $bytes): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$power = $bytes > 0
? min((int) floor(log($bytes, 1024)), count($units) - 1)
: 0;
return number_format(
$bytes / (1024 ** $power),
$power === 0 ? 0 : 2,
) . ' ' . $units[$power];
}
}