mirror of
https://github.com/tiennm99/coolify.git
synced 2026-08-21 08:25:45 +00:00
refactor(backups): centralize storage deletion guard
This commit is contained in:
@@ -4910,13 +4910,7 @@ class ApplicationsController extends Controller
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($storage->scheduledBackups()->exists()) {
|
||||
return response()->json([
|
||||
'message' => $storage instanceof LocalFileVolume
|
||||
? 'Delete this directory backup schedule and its archives before deleting the directory.'
|
||||
: 'Delete this volume backup schedule and its archives before deleting the volume.',
|
||||
], 422);
|
||||
}
|
||||
$storage->abortIfScheduledBackupsExist();
|
||||
|
||||
if ($storage instanceof LocalFileVolume) {
|
||||
$storage->deleteStorageOnServer();
|
||||
|
||||
@@ -4484,13 +4484,7 @@ class DatabasesController extends Controller
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($storage->scheduledBackups()->exists()) {
|
||||
return response()->json([
|
||||
'message' => $storage instanceof LocalFileVolume
|
||||
? 'Delete this directory backup schedule and its archives before deleting the directory.'
|
||||
: 'Delete this volume backup schedule and its archives before deleting the volume.',
|
||||
], 422);
|
||||
}
|
||||
$storage->abortIfScheduledBackupsExist();
|
||||
|
||||
if ($storage instanceof LocalFileVolume) {
|
||||
$storage->deleteStorageOnServer();
|
||||
|
||||
@@ -2911,13 +2911,7 @@ class ServicesController extends Controller
|
||||
], 422);
|
||||
}
|
||||
|
||||
if ($storage->scheduledBackups()->exists()) {
|
||||
return response()->json([
|
||||
'message' => $storage instanceof LocalFileVolume
|
||||
? 'Delete this directory backup schedule and its archives before deleting the directory.'
|
||||
: 'Delete this volume backup schedule and its archives before deleting the volume.',
|
||||
], 422);
|
||||
}
|
||||
$storage->abortIfScheduledBackupsExist();
|
||||
|
||||
if ($storage instanceof LocalFileVolume) {
|
||||
$storage->deleteStorageOnServer();
|
||||
|
||||
@@ -96,6 +96,13 @@ class LocalFileVolume extends BaseModel
|
||||
return $this->morphMany(ScheduledVolumeBackup::class, 'backupable');
|
||||
}
|
||||
|
||||
public function abortIfScheduledBackupsExist(): void
|
||||
{
|
||||
if ($this->scheduledBackups()->exists()) {
|
||||
abort(422, 'Delete this directory backup schedule and its archives before deleting the directory.');
|
||||
}
|
||||
}
|
||||
|
||||
public function loadStorageOnServer()
|
||||
{
|
||||
if ($this->is_host_file) {
|
||||
|
||||
@@ -56,6 +56,13 @@ class LocalPersistentVolume extends BaseModel
|
||||
return $this->morphMany(ScheduledVolumeBackup::class, 'backupable');
|
||||
}
|
||||
|
||||
public function abortIfScheduledBackupsExist(): void
|
||||
{
|
||||
if ($this->scheduledBackups()->exists()) {
|
||||
abort(422, 'Delete this volume backup schedule and its archives before deleting the volume.');
|
||||
}
|
||||
}
|
||||
|
||||
protected function customizeName($value)
|
||||
{
|
||||
return str($value)->trim()->value;
|
||||
|
||||
@@ -37,6 +37,7 @@ use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Livewire\Livewire;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
@@ -1456,6 +1457,26 @@ it('prevents a volume with tracked backup archives from being deleted', function
|
||||
expect($volume->fresh())->not->toBeNull();
|
||||
});
|
||||
|
||||
it('aborts storage deletion when scheduled backups exist', function () {
|
||||
$team = Team::factory()->create();
|
||||
[$application, $volume] = createVolumeBackupApplication($team);
|
||||
$directory = createApplicationBackupDirectory($application);
|
||||
|
||||
$volume->scheduledBackups()->create([
|
||||
'team_id' => $team->id,
|
||||
'frequency' => 'daily',
|
||||
]);
|
||||
$directory->scheduledBackups()->create([
|
||||
'team_id' => $team->id,
|
||||
'frequency' => 'daily',
|
||||
]);
|
||||
|
||||
expect(fn () => $volume->abortIfScheduledBackupsExist())
|
||||
->toThrow(HttpException::class, 'Delete this volume backup schedule and its archives before deleting the volume.')
|
||||
->and(fn () => $directory->abortIfScheduledBackupsExist())
|
||||
->toThrow(HttpException::class, 'Delete this directory backup schedule and its archives before deleting the directory.');
|
||||
});
|
||||
|
||||
it('deletes disabled volume backup archives before deleting their application', function () {
|
||||
Process::fake();
|
||||
Queue::fake();
|
||||
|
||||
Reference in New Issue
Block a user