Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions app/Actions/Database/StartDatabaseProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|St

if ($database->getMorphClass() === \App\Models\ServiceDatabase::class) {
$databaseType = $database->databaseType();
$network = $database->service->uuid;
$server = data_get($database, 'service.destination.server');
$containerName = "{$database->name}-{$database->service->uuid}";
$network = $database->getNetwork();
$server = $database->getServer();
$containerName = "{$database->name}-{$database->getOwnerUuid()}";
}
$internalPort = match ($databaseType) {
'standalone-mariadb', 'standalone-mysql' => 3306,
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Database/StopDatabaseProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|St
$server = data_get($database, 'destination.server');
$uuid = $database->uuid;
if ($database->getMorphClass() === \App\Models\ServiceDatabase::class) {
$server = data_get($database, 'service.server');
$server = $database->getServer();
}
instant_remote_process(["docker rm -f {$uuid}-proxy"], $server);

Expand Down
9 changes: 5 additions & 4 deletions app/Jobs/DatabaseBackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function handle(): void
}
if (data_get($this->backup, 'database_type') === \App\Models\ServiceDatabase::class) {
$this->database = data_get($this->backup, 'database');
$this->server = $this->database->service->server;
$this->server = $this->database->getServer();
$this->s3 = $this->backup->s3;
} else {
$this->database = data_get($this->backup, 'database');
Expand All @@ -115,8 +115,9 @@ public function handle(): void
}
if (data_get($this->backup, 'database_type') === \App\Models\ServiceDatabase::class) {
$databaseType = $this->database->databaseType();
$serviceUuid = $this->database->service->uuid;
$serviceName = str($this->database->service->name)->slug();
$serviceUuid = $this->database->getOwnerUuid();
$owner = $this->database->getOwner();
$serviceName = str($owner->name)->slug();
if (str($databaseType)->contains('postgres')) {
$this->container_name = "{$this->database->name}-$serviceUuid";
$this->directory_name = $serviceName.'-'.$this->container_name;
Expand Down Expand Up @@ -630,7 +631,7 @@ private function upload_to_s3(): void
$endpoint = $this->s3->endpoint;
$this->s3->testConnection(shouldSave: true);
if (data_get($this->backup, 'database_type') === \App\Models\ServiceDatabase::class) {
$network = $this->database->service->destination->network;
$network = $this->database->getNetwork();
} else {
$network = $this->database->destination->network;
}
Expand Down
64 changes: 64 additions & 0 deletions app/Livewire/Project/Application/ComposeBackups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Livewire\Project\Application;

use App\Models\Application;
use App\Models\ServiceDatabase;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class ComposeBackups extends Component
{
use AuthorizesRequests;

public ?Application $application = null;

public ?ServiceDatabase $selectedDatabase = null;

public string $selectedDatabaseUuid = '';

public array $parameters;

public bool $isImportSupported = false;

protected $listeners = ['refreshScheduledBackups' => '$refresh'];

public function mount()
{
try {
$this->parameters = get_route_parameters();
$this->application = Application::whereUuid($this->parameters['application_uuid'])->first();
if (! $this->application || $this->application->build_pack !== 'dockercompose') {
return redirect()->route('dashboard');
}
$this->authorize('view', $this->application);

// Auto-select first database if available
$firstDb = $this->application->composeDatabases()->first();
if ($firstDb) {
$this->selectDatabase($firstDb->uuid);
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function selectDatabase(string $uuid)
{
$this->selectedDatabaseUuid = $uuid;
$this->selectedDatabase = $this->application->composeDatabases()->whereUuid($uuid)->first();

if ($this->selectedDatabase) {
$dbType = $this->selectedDatabase->databaseType();
$supportedTypes = ['mysql', 'mariadb', 'postgres', 'mongo'];
$this->isImportSupported = collect($supportedTypes)->contains(fn ($type) => str_contains($dbType, $type));
}
}

public function render()
{
return view('livewire.project.application.compose-backups', [
'composeDatabases' => $this->application->composeDatabases()->get(),
]);
}
}
10 changes: 9 additions & 1 deletion app/Livewire/Project/Database/BackupEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function delete($password)
try {
$server = null;
if ($this->backup->database instanceof \App\Models\ServiceDatabase) {
$server = $this->backup->database->service->destination->server;
$server = $this->backup->database->getServer();
} elseif ($this->backup->database->destination && $this->backup->database->destination->server) {
$server = $this->backup->database->destination->server;
}
Expand Down Expand Up @@ -185,6 +185,14 @@ public function delete($password)
if ($this->backup->database->getMorphClass() === \App\Models\ServiceDatabase::class) {
$serviceDatabase = $this->backup->database;

if ($serviceDatabase->isApplicationOwned()) {
return redirect()->route('project.application.compose-backups', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_uuid' => $this->parameters['environment_uuid'],
'application_uuid' => $serviceDatabase->application->uuid,
]);
}

return redirect()->route('project.service.database.backups', [
'project_uuid' => $this->parameters['project_uuid'],
'environment_uuid' => $this->parameters['environment_uuid'],
Expand Down
9 changes: 5 additions & 4 deletions app/Livewire/Project/Database/BackupExecutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public function deleteBackup($executionId, $password)
return;
}

$server = $execution->scheduledDatabaseBackup->database->getMorphClass() === \App\Models\ServiceDatabase::class
? $execution->scheduledDatabaseBackup->database->service->destination->server
: $execution->scheduledDatabaseBackup->database->destination->server;
$db = $execution->scheduledDatabaseBackup->database;
$server = $db->getMorphClass() === \App\Models\ServiceDatabase::class
? $db->getServer()
: $db->destination->server;

try {
if ($execution->filename) {
Expand Down Expand Up @@ -182,7 +183,7 @@ public function server()
$server = null;

if ($this->database instanceof \App\Models\ServiceDatabase) {
$server = $this->database->service->destination->server;
$server = $this->database->getServer();
} elseif ($this->database->destination && $this->database->destination->server) {
$server = $this->database->destination->server;
}
Expand Down
6 changes: 3 additions & 3 deletions app/Livewire/Project/Database/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ public function getContainers()

// Handle ServiceDatabase server access differently
if ($resource->getMorphClass() === \App\Models\ServiceDatabase::class) {
$server = $resource->service?->server;
$server = $resource->getServer();
if (! $server) {
abort(404, 'Server not found for this service database.');
}
$this->serverId = $server->id;
$this->container = $resource->name.'-'.$resource->service->uuid;
$this->container = $resource->name.'-'.$resource->getOwnerUuid();
$this->resourceUuid = $resource->uuid; // Use ServiceDatabase's own UUID

// Determine database type for ServiceDatabase
Expand Down Expand Up @@ -633,7 +633,7 @@ public function restoreFromS3()

// Get the database destination network
if ($this->resource->getMorphClass() === \App\Models\ServiceDatabase::class) {
$destinationNetwork = $this->resource->service->destination->network ?? 'coolify';
$destinationNetwork = $this->resource->getNetwork() ?? 'coolify';
} else {
$destinationNetwork = $this->resource->destination->network ?? 'coolify';
}
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ protected static function booted()
$application->docker_compose_domains = null;
$application->docker_compose_raw = null;

// Remove compose databases and their backups
$application->composeDatabases()->each(function ($db) {
$db->delete();
});

// Remove SERVICE_FQDN_* and SERVICE_URL_* environment variables
$application->environment_variables()
->where(function ($q) {
Expand Down Expand Up @@ -235,6 +240,9 @@ protected static function booted()
});
static::forceDeleting(function ($application) {
$application->update(['fqdn' => null]);
$application->composeDatabases()->each(function ($db) {
$db->delete();
});
$application->settings()->delete();
$application->persistentStorages()->delete();
$application->environment_variables()->delete();
Expand Down Expand Up @@ -490,6 +498,11 @@ public function settings()
return $this->hasOne(ApplicationSetting::class);
}

public function composeDatabases()
{
return $this->hasMany(ServiceDatabase::class);
}

public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
Expand Down
9 changes: 9 additions & 0 deletions app/Models/LocalFileVolume.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function loadStorageOnServer()
if ($isService) {
$workdir = $this->resource->service->workdir();
$server = $this->resource->service->server;
} elseif ($this->resource instanceof \App\Models\ServiceDatabase && $this->resource->isApplicationOwned()) {
$workdir = $this->resource->workdir();
$server = $this->resource->getServer();
} else {
$workdir = $this->resource->workdir();
$server = $this->resource->destination->server;
Expand Down Expand Up @@ -86,6 +89,9 @@ public function deleteStorageOnServer()
if ($isService) {
$workdir = $this->resource->service->workdir();
$server = $this->resource->service->server;
} elseif ($this->resource instanceof \App\Models\ServiceDatabase && $this->resource->isApplicationOwned()) {
$workdir = $this->resource->workdir();
$server = $this->resource->getServer();
} else {
$workdir = $this->resource->workdir();
$server = $this->resource->destination->server;
Expand Down Expand Up @@ -123,6 +129,9 @@ public function saveStorageOnServer()
if ($isService) {
$workdir = $this->resource->service->workdir();
$server = $this->resource->service->server;
} elseif ($this->resource instanceof \App\Models\ServiceDatabase && $this->resource->isApplicationOwned()) {
$workdir = $this->resource->workdir();
$server = $this->resource->getServer();
} else {
$workdir = $this->resource->workdir();
$server = $this->resource->destination->server;
Expand Down
4 changes: 2 additions & 2 deletions app/Models/ScheduledDatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public function server()
{
if ($this->database) {
if ($this->database instanceof ServiceDatabase) {
$destination = data_get($this->database->service, 'destination');
$server = data_get($destination, 'server');
$server = $this->database->getServer();
} else {
$destination = data_get($this->database, 'destination');
$server = data_get($destination, 'server');
Expand All @@ -80,4 +79,5 @@ public function server()

return null;
}

}
Loading