fix(backups): identify the instance database by id, not by name

A user database named coolify-db was backed up as the instance database,
which targeted the wrong container. It was also hidden from the server
database list and from status updates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai
2026-10-24 18:14:27 +02:00
co-authored by Claude Opus 5.5
parent d9a22ce10e
commit bbcadbd480
5 changed files with 84 additions and 3 deletions
+1 -1
View File
@@ -317,7 +317,7 @@ class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
return;
}
$this->backup_dir = backup_dir().'/databases/'.str($this->team->name)->slug().'-'.$this->team->id.'/'.$this->directory_name;
if ($this->database->name === 'coolify-db') {
if ($this->database instanceof StandalonePostgresql && $this->database->isInstanceDatabase()) {
$databasesToBackup = ['coolify'];
$this->directory_name = $this->container_name = 'coolify-db';
$ip = Str::slug($this->server->ip);
+1 -1
View File
@@ -551,7 +551,7 @@ class PushServerUpdateJob implements ShouldBeEncrypted, ShouldQueue, Silenced
->select($databaseColumns)
->where(fn ($query) => $this->scopeDestination($query, $standaloneDockerIds, $swarmDockerIds))
->get();
})->filter(fn ($database) => data_get($database, 'name') !== 'coolify-db')->values();
})->reject(fn ($database) => $database instanceof StandalonePostgresql && $database->isInstanceDatabase())->values();
}
private function serverDestinationIds(): array
+1 -1
View File
@@ -1893,7 +1893,7 @@ $siteAddress {
->concat($dragonflies)
->concat($clickhouses)
->concat($sqlites)
->filter(fn ($item) => data_get($item, 'name') !== 'coolify-db');
->reject(fn ($item) => $item instanceof StandalonePostgresql && $item->isInstanceDatabase());
}
public function applications()
+8
View File
@@ -320,6 +320,14 @@ class StandalonePostgresql extends BaseModel
return 'standalone-postgresql';
}
/**
* Coolify's own database is the instance sentinel row (id 0). Users can give any database the name "coolify-db".
*/
public function isInstanceDatabase(): bool
{
return $this->id === 0;
}
protected function internalDbUrl(): Attribute
{
return new Attribute(
@@ -0,0 +1,73 @@
<?php
use App\Events\BackupCreated;
use App\Jobs\DatabaseBackupJob;
use App\Models\Environment;
use App\Models\InstanceSettings;
use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\ScheduledDatabaseBackup;
use App\Models\ScheduledDatabaseBackupExecution;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
uses(RefreshDatabase::class);
beforeEach(function () {
Server::flushIdentityMap();
Storage::fake('ssh-keys');
Storage::fake('ssh-mux');
Event::fake([BackupCreated::class]);
Notification::fake();
InstanceSettings::unguarded(fn () => InstanceSettings::firstOrCreate(['id' => 0]));
});
it('backs up a user database named coolify-db as an ordinary database', function () {
$team = Team::factory()->create();
$server = Server::factory()->create([
'team_id' => $team->id,
'private_key_id' => PrivateKey::factory()->create(['team_id' => $team->id])->id,
]);
$server->settings()->update(['is_reachable' => true, 'is_usable' => true]);
$destination = StandaloneDocker::where('server_id', $server->id)->firstOrFail();
$environment = Environment::factory()->create(['project_id' => Project::factory()->create(['team_id' => $team->id])->id]);
$database = create_standalone_postgresql($environment->id, $destination, [
'name' => 'coolify-db',
'postgres_db' => 'appdb',
'status' => 'running:healthy',
]);
$backup = new ScheduledDatabaseBackup;
$backup->forceFill([
'enabled' => true,
'save_s3' => false,
'frequency' => 'daily',
'database_id' => $database->id,
'database_type' => $database->getMorphClass(),
'team_id' => $team->id,
])->save();
$commands = collect();
Process::fake(function ($process) use ($commands) {
$command = is_array($process->command) ? implode(' ', $process->command) : $process->command;
$commands->push($command);
return Process::result(output: str_contains($command, 'du -b') ? '128' : '');
});
(new DatabaseBackupJob($backup->fresh()))->handle();
$execution = ScheduledDatabaseBackupExecution::query()->sole();
$dumpCommand = $commands->first(fn (string $command) => str_contains($command, 'pg_dump'));
expect($database->isInstanceDatabase())->toBeFalse()
->and($execution->database_name)->toBe('appdb')
->and($execution->filename)->toContain('/databases/')
->and($execution->filename)->not->toContain('/coolify/coolify-db-')
->and($dumpCommand)->toContain($database->uuid)
->and($dumpCommand)->not->toContain('exec coolify-db ');
expect($server->databases()->pluck('id'))->toContain($database->id);
});