From 2dcfdcab3bc071fe26d97a17647b718188e801ab Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:09:21 +0200 Subject: [PATCH 1/8] fix init scripts for postgres --- .../Project/Database/Postgresql/General.php | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/Livewire/Project/Database/Postgresql/General.php b/app/Livewire/Project/Database/Postgresql/General.php index c12fa49f3..642aa793d 100644 --- a/app/Livewire/Project/Database/Postgresql/General.php +++ b/app/Livewire/Project/Database/Postgresql/General.php @@ -9,8 +9,6 @@ use App\Models\StandalonePostgresql; use Exception; use Livewire\Component; -use function Aws\filter; - class General extends Component { public StandalonePostgresql $database; @@ -126,8 +124,31 @@ class General extends Component public function save_init_script($script) { - $this->database->init_scripts = filter($this->database->init_scripts, fn ($s) => $s['filename'] !== $script['filename']); - $this->database->init_scripts = array_merge($this->database->init_scripts, [$script]); + $initScripts = collect($this->database->init_scripts ?? []); + + $existingScript = $initScripts->firstWhere('filename', $script['filename']); + if ($existingScript && $existingScript['index'] !== $script['index']) { + $this->dispatch('error', 'A script with this filename already exists.'); + + return; + } + + $index = $initScripts->search(function ($item) use ($script) { + return $item['index'] === $script['index']; + }); + + if ($index !== false) { + $initScripts[$index] = $script; + } else { + $initScripts->push($script); + } + + $this->database->init_scripts = $initScripts->values()->map(function ($item, $index) { + $item['index'] = $index; + + return $item; + })->all(); + $this->database->save(); $this->dispatch('success', 'Init script saved.'); } From 66b458d807bc0ff9cc8acae47f57aef17359d15c Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:14:13 +0200 Subject: [PATCH 2/8] aws filter still used From 907c6cc4e54f6b82e0d0f1d35c2c1d56eaf84ace Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:16:06 +0200 Subject: [PATCH 3/8] format --- app/Livewire/Project/Database/Postgresql/General.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Livewire/Project/Database/Postgresql/General.php b/app/Livewire/Project/Database/Postgresql/General.php index 642aa793d..3abdcc22e 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']); + if ($existingScript && $existingScript['index'] !== $script['index']) { $this->dispatch('error', 'A script with this filename already exists.'); 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 4/8] 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 Date: Fri, 18 Oct 2024 21:07:23 +0200 Subject: [PATCH 5/8] fix: if an init script is renamed the old version is still on the server --- app/Actions/Database/StartPostgresql.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index 2a8e5476c..3a2f9ce10 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -197,9 +197,12 @@ class StartPostgresql private function generate_init_scripts() { + $this->commands[] = "rm -rf $this->configuration_dir/docker-entrypoint-initdb.d/*"; + if (is_null($this->database->init_scripts) || count($this->database->init_scripts) === 0) { return; } + foreach ($this->database->init_scripts as $init_script) { $filename = data_get($init_script, 'filename'); $content = data_get($init_script, 'content'); 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 6/8] 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) From 18201ece00132da40d51bf0ec4ee4bcba8ae4633 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:17:57 +0200 Subject: [PATCH 7/8] fix remove postgres config if it is null or not set --- app/Actions/Database/StartPostgresql.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index 3a2f9ce10..17cd6171f 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -214,10 +214,15 @@ class StartPostgresql private function add_custom_conf() { + $filename = 'custom-postgres.conf'; + $config_file_path = "$this->configuration_dir/$filename"; + if (is_null($this->database->postgres_conf) || empty($this->database->postgres_conf)) { + $this->commands[] = "rm -f $config_file_path"; + return; } - $filename = 'custom-postgres.conf'; + $content = $this->database->postgres_conf; if (! str($content)->contains('listen_addresses')) { $content .= "\nlisten_addresses = '*'"; @@ -225,6 +230,6 @@ class StartPostgresql $this->database->save(); } $content_base64 = base64_encode($content); - $this->commands[] = "echo '{$content_base64}' | base64 -d | tee $this->configuration_dir/{$filename} > /dev/null"; + $this->commands[] = "echo '{$content_base64}' | base64 -d | tee $config_file_path > /dev/null"; } } From 33ae10a6dcef0e0c937c3211dd999ae24a3ea88d Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 18 Dec 2024 10:44:56 +0100 Subject: [PATCH 8/8] fix --- app/Actions/Database/StartPostgresql.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index adf0623f3..ebfe61f82 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -23,6 +23,9 @@ class StartPostgresql $this->database = $database; $container_name = $this->database->uuid; $this->configuration_dir = database_configuration_dir().'/'.$container_name; + if (isDev()) { + $this->configuration_dir = '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/databases/'.$container_name; + } $this->commands = [ "echo 'Starting database.'", @@ -78,7 +81,7 @@ class StartPostgresql ], ], ]; - if (! is_null($this->database->limits_cpuset)) { + if (filled($this->database->limits_cpuset)) { data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { @@ -108,7 +111,7 @@ class StartPostgresql ]; } } - if (! is_null($this->database->postgres_conf) && ! empty($this->database->postgres_conf)) { + if (filled($this->database->postgres_conf)) { $docker_compose['services'][$container_name]['volumes'][] = [ 'type' => 'bind', 'source' => $this->configuration_dir.'/custom-postgres.conf', @@ -201,7 +204,7 @@ class StartPostgresql { $this->commands[] = "rm -rf $this->configuration_dir/docker-entrypoint-initdb.d/*"; - if (is_null($this->database->init_scripts) || count($this->database->init_scripts) === 0) { + if (blank($this->database->init_scripts) || count($this->database->init_scripts) === 0) { return; } @@ -219,7 +222,7 @@ class StartPostgresql $filename = 'custom-postgres.conf'; $config_file_path = "$this->configuration_dir/$filename"; - if (is_null($this->database->postgres_conf) || empty($this->database->postgres_conf)) { + if (blank($this->database->postgres_conf)) { $this->commands[] = "rm -f $config_file_path"; return;