Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Multiple Init script fixes and some other Postgres fixes #3950

Draft
wants to merge 7 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/Actions/Database/StartPostgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ private function generate_environment_variables()

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');
Expand All @@ -211,17 +214,22 @@ private function generate_init_scripts()

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 = '*'";
$this->database->postgres_conf = $content;
$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";
}
}
78 changes: 69 additions & 9 deletions app/Livewire/Project/Database/Postgresql/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Exception;
use Livewire\Component;

use function Aws\filter;

class General extends Component
{
public StandalonePostgresql $database;
Expand Down Expand Up @@ -126,23 +124,85 @@ public function instantSave()

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']);
$oldScript = $initScripts->firstWhere('index', $script['index']);

if ($existingScript && $existingScript['index'] !== $script['index']) {
$this->dispatch('error', 'A script with this filename already exists.');

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'];
});

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.');
$this->dispatch('success', 'Init script saved and updated.');
}

public function delete_init_script($script)
{
$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.');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<x-forms.button type="submit">Save</x-forms.button>
<x-modal-confirmation title="Confirm init-script deletion?" buttonTitle="Delete" isErrorButton
submitAction="delete" :actions="[
'The init-script of this database will be permanently deleted.',
'The init-script of this database will be permanently deleted form the database and the server.',
'If you are actively using this init-script, it could cause errors on redeployment.',
]" confirmationText="{{ $filename }}"
confirmationLabel="Please confirm the execution of the actions by entering the init-script name below"
Expand Down