Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor(backups): centralize storage deletion guard

Andras Bacsaibf72fd5d8823
diff --git a/app/Http/Controllers/Api/ApplicationsController.php b/app/Http/Controllers/Api/ApplicationsController.php
index 1305ab7958..c2592d7f9b 100644
--- a/app/Http/Controllers/Api/ApplicationsController.php
+++ b/app/Http/Controllers/Api/ApplicationsController.php
@@ -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();
diff --git a/app/Http/Controllers/Api/DatabasesController.php b/app/Http/Controllers/Api/DatabasesController.php
index 00ce233cc3..41af38a724 100644
--- a/app/Http/Controllers/Api/DatabasesController.php
+++ b/app/Http/Controllers/Api/DatabasesController.php
@@ -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();
diff --git a/app/Http/Controllers/Api/ServicesController.php b/app/Http/Controllers/Api/ServicesController.php
index 0a1d156a74..9945dfcf9a 100644
--- a/app/Http/Controllers/Api/ServicesController.php
+++ b/app/Http/Controllers/Api/ServicesController.php
@@ -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();
diff --git a/app/Models/LocalFileVolume.php b/app/Models/LocalFileVolume.php
index a03ec486bd..86873d1a1c 100644
--- a/app/Models/LocalFileVolume.php
+++ b/app/Models/LocalFileVolume.php
@@ -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) {
diff --git a/app/Models/LocalPersistentVolume.php b/app/Models/LocalPersistentVolume.php
index 72bb3c38d4..6b4e0fe5b0 100644
--- a/app/Models/LocalPersistentVolume.php
+++ b/app/Models/LocalPersistentVolume.php
@@ -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;
diff --git a/tests/Feature/VolumeBackupTest.php b/tests/Feature/VolumeBackupTest.php
index 9b650b9426..8a8058ff47 100644
--- a/tests/Feature/VolumeBackupTest.php
+++ b/tests/Feature/VolumeBackupTest.php
@@ -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();