From 7f393eb2c26c470cdf1a86820ed40a8cf33bea01 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:51:51 +0200 Subject: [PATCH] fix indexing after deletion and make sure init script is removed form the server --- .../Project/Database/Postgresql/General.php | 38 +++++++++++++++---- .../project/database/init-script.blade.php | 2 +- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/Livewire/Project/Database/Postgresql/General.php b/app/Livewire/Project/Database/Postgresql/General.php index 3abdcc22e..018027912 100644 --- a/app/Livewire/Project/Database/Postgresql/General.php +++ b/app/Livewire/Project/Database/Postgresql/General.php @@ -144,11 +144,13 @@ class General extends Component $initScripts->push($script); } - $this->database->init_scripts = $initScripts->values()->map(function ($item, $index) { - $item['index'] = $index; + $this->database->init_scripts = $initScripts->values() + ->map(function ($item, $index) { + $item['index'] = $index; - return $item; - })->all(); + return $item; + }) + ->all(); $this->database->save(); $this->dispatch('success', 'Init script saved.'); @@ -159,12 +161,32 @@ class General extends Component $collection = collect($this->database->init_scripts); $found = $collection->firstWhere('filename', $script['filename']); if ($found) { - $this->database->init_scripts = $collection->filter(fn ($s) => $s['filename'] !== $script['filename'])->toArray(); + $container_name = $this->database->uuid; + $configuration_dir = database_configuration_dir().'/'.$container_name; + $file_path = "$configuration_dir/docker-entrypoint-initdb.d/{$script['filename']}"; + + $command = "rm -f $file_path"; + try { + instant_remote_process([$command], $this->server); + } catch (\Exception $e) { + $this->dispatch('error', 'Failed to remove init script from server: '.$e->getMessage()); + + return; + } + + $updatedScripts = $collection->filter(fn ($s) => $s['filename'] !== $script['filename']) + ->values() + ->map(function ($item, $index) { + $item['index'] = $index; + + return $item; + }) + ->all(); + + $this->database->init_scripts = $updatedScripts; $this->database->save(); $this->refresh(); - $this->dispatch('success', 'Init script deleted.'); - - return; + $this->dispatch('success', 'Init script deleted from the database and the server.'); } } diff --git a/resources/views/livewire/project/database/init-script.blade.php b/resources/views/livewire/project/database/init-script.blade.php index 8ba0db23c..1580e41cc 100644 --- a/resources/views/livewire/project/database/init-script.blade.php +++ b/resources/views/livewire/project/database/init-script.blade.php @@ -4,7 +4,7 @@ Save