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.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat(database): unlink applications from sqlite and protect connected volumes
peaklabs-dev0687d1f55fbf
diff --git a/app/Http/Controllers/Api/DatabasesController.php b/app/Http/Controllers/Api/DatabasesController.phpindex a065dfcd01..e702a21a57 100644--- a/app/Http/Controllers/Api/DatabasesController.php+++ b/app/Http/Controllers/Api/DatabasesController.php@@ -2788,6 +2788,12 @@ class DatabasesController extends Controller$this->authorize('delete', $database);+ if ($database instanceof StandaloneSqlite && $database->connectedVolumes()->exists()) {+ return response()->json([+ 'message' => 'This database volume is mounted by '.$database->connectedApplicationNames()->implode(', ').'. Remove those mounts before deleting the database.',+ ], 422);+ }+$database->delete();DeleteResourceJob::dispatch(@@ -4741,6 +4747,12 @@ class DatabasesController extends Controller], 422);}+ if ($storage->isSharedWithAnotherResource()) {+ return response()->json([+ 'message' => 'This volume is mounted by an application. Unlink it on the SQLite database page first.',+ ], 422);+ }+$storage->abortIfScheduledBackupsExist();if ($storage instanceof LocalFileVolume) {diff --git a/app/Livewire/Project/Database/Sqlite/ConnectApplication.php b/app/Livewire/Project/Database/Sqlite/ConnectApplication.phpindex eabbcd5de3..b741159212 100644--- a/app/Livewire/Project/Database/Sqlite/ConnectApplication.php+++ b/app/Livewire/Project/Database/Sqlite/ConnectApplication.php@@ -100,6 +100,31 @@ class ConnectApplication extends Component}}+ /**+ * Volumes of applications that mount this database's data volume.+ *+ * @return Collection<int, LocalPersistentVolume>+ */+ public function getConnectionsProperty(): Collection+ {+ return $this->database->connectedVolumes()->with('resource')->orderBy('id')->get();+ }++ /**+ * Remove the mount from the application. The Docker volume and its data stay untouched.+ */+ public function unlink(int $volumeId): void+ {+ $this->authorize('view', $this->database);++ $volume = $this->database->connectedVolumes()->with('resource')->findOrFail($volumeId);+ $this->authorize('update', $volume->resource ?? $this->database);++ $volume->delete();++ $this->dispatch('success', 'Application unlinked. Redeploy it to apply the change.');+ }+public function render(){return view('livewire.project.database.sqlite.connect-application');diff --git a/app/Livewire/Project/Shared/Danger.php b/app/Livewire/Project/Shared/Danger.phpindex e9b3cffa22..6d63f27db3 100644--- a/app/Livewire/Project/Shared/Danger.php+++ b/app/Livewire/Project/Shared/Danger.php@@ -6,6 +6,7 @@ use App\Jobs\DeleteResourceJob;use App\Models\Service;use App\Models\ServiceApplication;use App\Models\ServiceDatabase;+use App\Models\StandaloneSqlite;use Illuminate\Foundation\Auth\Access\AuthorizesRequests;use Livewire\Component;@@ -98,6 +99,12 @@ class Danger extends Componentreturn 'Resource not found.';}+ if ($this->resource instanceof StandaloneSqlite && $this->resource->connectedVolumes()->exists()) {+ $this->dispatch('error', 'This database volume is mounted by '.$this->resource->connectedApplicationNames()->implode(', ').'. Remove those mounts before deleting the database.');++ return;+ }+if (! empty($selectedActions)) {$this->delete_volumes = in_array('delete_volumes', $selectedActions);$this->delete_connected_networks = in_array('delete_connected_networks', $selectedActions);diff --git a/app/Livewire/Project/Shared/Storages/All.php b/app/Livewire/Project/Shared/Storages/All.phpindex a7886484cf..c71d411586 100644--- a/app/Livewire/Project/Shared/Storages/All.php+++ b/app/Livewire/Project/Shared/Storages/All.php@@ -22,7 +22,7 @@ class All extends Component/*** Editable form state keyed by storage id.*- * @var array<int|string, array{name: string, mountPath: string, isPreviewSuffixEnabled: bool, isReadOnly: bool, canDeleteStale: bool}>+ * @var array<int|string, array{name: string, mountPath: string, isPreviewSuffixEnabled: bool, isReadOnly: bool, isShared: bool, canDeleteStale: bool}>*/public array $forms = [];@@ -68,7 +68,7 @@ class All extends Component$this->authorize('view', $this->resource);$this->resource->refresh();$this->resource->unsetRelation('persistentStorages');- $this->resource->load(['persistentStorages' => fn ($query) => $query->orderBy('id')]);+ $this->resource->load(['persistentStorages' => fn ($query) => $query->with('standaloneSqlite')->orderBy('id')]);foreach ($this->resource->persistentStorages as $storage) {$storage->setRelation('resource', $this->resource);@@ -95,7 +95,9 @@ class All extends Component}$form = $this->forms[$storageId];- $storage->name = $form['name'];+ if (! $storage->isSharedWithAnotherResource()) {+ $storage->name = $form['name'];+ }$storage->mount_path = $form['mountPath'];$storage->is_preview_suffix_enabled = (bool) $form['isPreviewSuffixEnabled'];$storage->save();@@ -133,6 +135,12 @@ class All extends Component$storage = $this->findStorageOrFail($storageId);+ if ($storage->isSharedWithAnotherResource()) {+ $this->dispatch('error', 'This volume is connected to a SQLite database. Unlink it on the SQLite database page.');++ return false;+ }+if ($this->isComposeOrService && $storage->isDeclaredInCompose()) {$this->dispatch('error', 'This volume is managed by the current Docker Compose file.');@@ -147,12 +155,6 @@ class All extends Component$this->deleteDockerVolume = in_array('deleteDockerVolume', $selectedActions, true);if ($this->deleteDockerVolume) {- if ($storage->isSharedWithAnotherResource()) {- $this->dispatch('error', 'This Docker volume is also mounted by another resource. Remove the mount without deleting the Docker volume.');-- return false;- }-$server = $this->resource instanceof Application? $this->resource->destination->server: $this->resource->service->server;@@ -201,6 +203,7 @@ class All extends Component'mountPath' => $storage->mount_path,'isPreviewSuffixEnabled' => (bool) ($storage->is_preview_suffix_enabled ?? true),'isReadOnly' => $storage->shouldBeReadOnlyInUI() || ! $this->canUpdate,+ 'isShared' => $storage->isSharedWithAnotherResource(),'canDeleteStale' => $this->canUpdate&& ($storage->isServiceResource() || $storage->isDockerComposeResource())&& ! $storage->isDeclaredInCompose(),diff --git a/app/Models/StandaloneSqlite.php b/app/Models/StandaloneSqlite.phpindex 0f1da4f7f7..6012ac4e0b 100644--- a/app/Models/StandaloneSqlite.php+++ b/app/Models/StandaloneSqlite.php@@ -11,6 +11,7 @@ use App\Traits\HasSecretManager;use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\SoftDeletes;+use Illuminate\Support\Collection;class StandaloneSqlite extends BaseModel{@@ -314,6 +315,20 @@ class StandaloneSqlite extends BaseModelreturn $this->hasMany(LocalPersistentVolume::class, 'standalone_sqlite_id');}+ /**+ * Names of the resources that mount this database's data volume.+ *+ * @return Collection<int, string>+ */+ public function connectedApplicationNames(): Collection+ {+ return $this->connectedVolumes()->with('resource')->get()+ ->map(fn (LocalPersistentVolume $volume) => $volume->resource?->name)+ ->filter()+ ->unique()+ ->values();+ }+public function scheduledBackups(){return $this->morphMany(ScheduledDatabaseBackup::class, 'database');diff --git a/resources/views/livewire/project/database/sqlite/connect-application.blade.php b/resources/views/livewire/project/database/sqlite/connect-application.blade.phpindex b9ff075554..8bef8cb400 100644--- a/resources/views/livewire/project/database/sqlite/connect-application.blade.php+++ b/resources/views/livewire/project/database/sqlite/connect-application.blade.php@@ -2,6 +2,24 @@<div><x-application.settings-section title="Connect application"description="Mount the data volume of this database into an application on the same server. The application must run as user 65532 or as root to access the files.">+ @if ($this->connections->isNotEmpty())+ <div class="mb-4 divide-y divide-neutral-200 rounded-lg border border-neutral-200 dark:divide-white/[0.08] dark:border-white/[0.08]">+ @foreach ($this->connections as $connection)+ <div wire:key="sqlite-connection-{{ $connection->id }}"+ class="flex items-center justify-between gap-3 px-3 py-2 text-[13px]">+ <span class="min-w-0 truncate">+ <a href="{{ $connection->resource?->link() }}"+ class="font-medium underline-offset-2 hover:underline">{{ $connection->resource?->name }}</a>+ <span class="text-neutral-500 dark:text-fg-dim">at {{ $connection->mount_path }}</span>+ </span>+ <x-forms.button type="button" wire:click="unlink({{ $connection->id }})" class="!px-2.5 !text-xs"+ canGate="update" :canResource="$database">+ Unlink+ </x-forms.button>+ </div>+ @endforeach+ </div>+ @endif@if (count($applicationOptions) === 0)<p class="text-[13px] leading-5 text-neutral-500 dark:text-fg-dim">No applications are deployed on this server yet.diff --git a/resources/views/livewire/project/shared/storages/all.blade.php b/resources/views/livewire/project/shared/storages/all.blade.phpindex e731c7f2ee..d38d7e605e 100644--- a/resources/views/livewire/project/shared/storages/all.blade.php+++ b/resources/views/livewire/project/shared/storages/all.blade.php@@ -166,10 +166,14 @@<span class="volumes-mobile-label volumes-field-label">Storage Name</span><div class="flex min-w-0 items-center gap-2"><div class="min-w-0 flex-1">- <x-forms.input id="forms.{{ $id }}.name" required />+ <x-forms.input id="forms.{{ $id }}.name" required :readonly="$form['isShared']" /></div></div>- @if (blank($storage->host_path))+ @if ($storage->standaloneSqlite)+ <a href="{{ $storage->standaloneSqlite->link() }}"+ class="block text-xs text-neutral-500 underline underline-offset-2 hover:text-black dark:text-fg-dim dark:hover:text-fg">SQLite+ database {{ $storage->standaloneSqlite->name }}</a>+ @elseif (blank($storage->host_path))<span class="block text-xs text-neutral-500 dark:text-fg-dim">Volume mount</span>@endif</div>@@ -234,7 +238,7 @@Update</x-forms.button>- @if ($showBackupAction)+ @if ($showBackupAction && ! $form['isShared'])<x-modal-input title="Configure Volume Backup" :wireIgnore="false"><x-slot:content><x-forms.button type="button" class="!px-2.5 !text-xs" canGate="update"@@ -265,6 +269,7 @@</x-modal-input>@endif+ @unless ($form['isShared'])<x-modal-confirmation title="Confirm persistent storage deletion?" isErrorButtonbuttonTitle="Delete" submitAction="delete({{ $id }})" :actions="['The selected persistent storage/volume will be permanently deleted.',@@ -272,6 +277,7 @@]" confirmationText="{{ $form['name'] }}"confirmationLabel="Please confirm the execution of the actions by entering the Storage Name below"shortConfirmationLabel="Storage Name" />+ @endunless</div></div></form>diff --git a/tests/Feature/SqliteConnectApplicationTest.php b/tests/Feature/SqliteConnectApplicationTest.phpindex 3b2a3c37fc..51d79f3c38 100644--- a/tests/Feature/SqliteConnectApplicationTest.php+++ b/tests/Feature/SqliteConnectApplicationTest.php@@ -1,6 +1,8 @@<?phpuse App\Livewire\Project\Database\Sqlite\ConnectApplication;+use App\Livewire\Project\Shared\Danger;+use App\Livewire\Project\Shared\Storages\All;use App\Models\Application;use App\Models\Environment;use App\Models\InstanceSettings;@@ -168,3 +170,44 @@ it('keeps a docker volume shared with another resource when the application voluProcess::assertRan(fn ($process) => str_contains($process->command, 'docker volume rm -f '.escapeshellarg($this->application->uuid.'-data')));Process::assertNotRan(fn ($process) => str_contains($process->command, 'sqlite-data-'.$this->sqlite->uuid));});++function connectSqliteVolume(Application $application, StandaloneSqlite $sqlite): LocalPersistentVolume+{+ return LocalPersistentVolume::create([+ 'name' => 'sqlite-data-'.$sqlite->uuid,+ 'mount_path' => StandaloneSqlite::DATA_DIRECTORY,+ 'host_path' => null,+ 'standalone_sqlite_id' => $sqlite->id,+ 'resource_id' => $application->id,+ 'resource_type' => $application->getMorphClass(),+ 'is_preview_suffix_enabled' => false,+ ]);+}++it('unlinks an application without touching the docker volume', function () {+ Process::fake();+ $volume = connectSqliteVolume($this->application, $this->sqlite);++ Livewire::test(ConnectApplication::class, ['database' => $this->sqlite])+ ->call('unlink', $volume->id)+ ->assertDispatched('success');++ Process::assertNothingRan();+ expect($volume->fresh())->toBeNull();+});++it('blocks deleting the sqlite volume or database while an application is connected', function () {+ connectSqliteVolume($this->application, $this->sqlite);+ $databaseVolume = $this->sqlite->persistentStorages()->sole();++ Livewire::test(All::class, ['resource' => $this->sqlite])+ ->call('delete', $databaseVolume->id, 'password')+ ->assertDispatched('error');++ Livewire::test(Danger::class, ['resource' => $this->sqlite])+ ->call('delete', 'password')+ ->assertDispatched('error');++ expect($databaseVolume->fresh())->not->toBeNull()+ ->and(StandaloneSqlite::find($this->sqlite->id))->not->toBeNull();+});