From 159c4aa7ac02fdae6849d8c6db7a7a837dd8f01a Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:08:45 +0200 Subject: [PATCH] remove old init script on server if it is renamed --- .../Project/Database/Postgresql/General.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Livewire/Project/Database/Postgresql/General.php b/app/Livewire/Project/Database/Postgresql/General.php index 018027912..88dd5c1a8 100644 --- a/app/Livewire/Project/Database/Postgresql/General.php +++ b/app/Livewire/Project/Database/Postgresql/General.php @@ -127,6 +127,7 @@ class General extends Component $initScripts = collect($this->database->init_scripts ?? []); $existingScript = $initScripts->firstWhere('filename', $script['filename']); + $oldScript = $initScripts->firstWhere('index', $script['index']); if ($existingScript && $existingScript['index'] !== $script['index']) { $this->dispatch('error', 'A script with this filename already exists.'); @@ -134,6 +135,21 @@ class General extends Component return; } + $container_name = $this->database->uuid; + $configuration_dir = database_configuration_dir().'/'.$container_name; + + if ($oldScript && $oldScript['filename'] !== $script['filename']) { + $old_file_path = "$configuration_dir/docker-entrypoint-initdb.d/{$oldScript['filename']}"; + $delete_command = "rm -f $old_file_path"; + try { + instant_remote_process([$delete_command], $this->server); + } catch (\Exception $e) { + $this->dispatch('error', 'Failed to remove old init script from server: '.$e->getMessage()); + + return; + } + } + $index = $initScripts->search(function ($item) use ($script) { return $item['index'] === $script['index']; }); @@ -153,7 +169,7 @@ class General extends Component ->all(); $this->database->save(); - $this->dispatch('success', 'Init script saved.'); + $this->dispatch('success', 'Init script saved and updated.'); } public function delete_init_script($script)