From e5e9faba356dec071d39abff5da507b4a72fe352 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sat, 9 Sep 2023 13:18:49 +0200 Subject: [PATCH] fix: delete database related things when delete database --- app/Console/Kernel.php | 5 +++++ app/Jobs/DatabaseBackupJob.php | 18 +++++------------- app/Models/StandalonePostgresql.php | 5 +++++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index dbe48200d..276a27067 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -74,6 +74,11 @@ private function check_scheduled_backups($schedule) if (!$scheduled_backup->enabled) { continue; } + if (is_null(data_get($scheduled_backup,'database'))) { + ray('database not found'); + $scheduled_backup->delete(); + continue; + } if (isset(VALID_CRON_STRINGS[$scheduled_backup->frequency])) { $scheduled_backup->frequency = VALID_CRON_STRINGS[$scheduled_backup->frequency]; diff --git a/app/Jobs/DatabaseBackupJob.php b/app/Jobs/DatabaseBackupJob.php index 3d9e72d3a..d8ad4ec12 100644 --- a/app/Jobs/DatabaseBackupJob.php +++ b/app/Jobs/DatabaseBackupJob.php @@ -26,10 +26,8 @@ class DatabaseBackupJob implements ShouldQueue public ?Team $team = null; public Server $server; - public ?ScheduledDatabaseBackup $backup = null; - public string $database_type; - public ?StandalonePostgresql $database = null; - public ?string $database_status = null; + public ScheduledDatabaseBackup $backup; + public StandalonePostgresql $database; public ?string $container_name = null; public ?ScheduledDatabaseBackupExecution $backup_log = null; @@ -45,13 +43,8 @@ public function __construct($backup) { $this->backup = $backup; $this->team = Team::find($backup->team_id); - $this->database = data_get($this->backup,'database'); - if (is_null($this->database)) { - ray('Database not found'); - } - $this->database_type = $this->database->type(); + $this->database = data_get($this->backup, 'database'); $this->server = $this->database->destination->server; - $this->database_status = data_get($this->database,'status'); $this->s3 = $this->backup->s3; } @@ -68,7 +61,7 @@ public function uniqueId(): int public function handle(): void { try { - if ($this->database_status !== 'running') { + if (data_get($this->database, 'status') !== 'running') { ray('database not running'); return; } @@ -87,7 +80,7 @@ public function handle(): void 'filename' => $this->backup_location, 'scheduled_database_backup_id' => $this->backup->id, ]); - if ($this->database_type === 'standalone-postgresql') { + if ($this->database->type() === 'standalone-postgresql') { $this->backup_standalone_postgresql(); } $this->calculate_size(); @@ -102,7 +95,6 @@ public function handle(): void send_internal_notification('DatabaseBackupJob failed with: ' . $th->getMessage()); throw $th; } - } private function backup_standalone_postgresql(): void diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.php index 6c00e302c..e0b95c5db 100644 --- a/app/Models/StandalonePostgresql.php +++ b/app/Models/StandalonePostgresql.php @@ -28,6 +28,11 @@ protected static function booted() 'is_readonly' => true ]); }); + static::deleted(function ($database) { + $database->scheduledBackups()->delete(); + $database->persistentStorages()->delete(); + instant_remote_process(['docker volume rm postgres-data-' . $database->uuid], $database->destination->server, false); + }); } public function portsMappings(): Attribute