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

postgres: Add setupSchemaScript option for Schema Setup #1633

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions examples/postgres/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
initialScript = ''
CREATE EXTENSION IF NOT EXISTS postgis;
'';
setupSchemaScript = ''
echo "script to run to setup or update database schema. This script must be idempotent."
'';
};
}
40 changes: 40 additions & 0 deletions src/modules/services/postgres.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ let
''
else "";

runSetupSchemaScript =
if cfg.setupSchemaScript == null
then ''
echo "script not provided, skipping."
''
else ''
${cfg.setupSchemaScript}
'';

toStr = value:
if true == value
then "yes"
Expand Down Expand Up @@ -158,10 +167,29 @@ let
fi
unset POSTGRES_RUN_INITIAL_SCRIPT
'';

setupSchemaScript = pkgs.writeShellScriptBin "setup-schema-script" ''
echo
echo "PostgreSQL is setting up the schema"
echo
OLDPGHOST="$PGHOST"
PGHOST=${q runtimeDir}

pg_ctl -D "$PGDATA" -w start -o "-c unix_socket_directories=${runtimeDir} -c listen_addresses= -p ${toString cfg.port}"
${runSetupSchemaScript}
pg_ctl -D "$PGDATA" -m fast -w stop
PGHOST="$OLDPGHOST"
unset OLDPGHOST
echo
echo "PostgreSQL setup schema complete."
echo
'';

startScript = pkgs.writeShellScriptBin "start-postgres" ''
set -euo pipefail
mkdir -p ${q runtimeDir}
${setupScript}/bin/setup-postgres
${setupSchemaScript}/bin/setup-schema-script
exec ${postgresPkg}/bin/postgres
'';
in
Expand Down Expand Up @@ -327,6 +355,18 @@ in
'';
};

setupSchemaScript = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Path to a script that will set up or update the PostgreSQL database schema. This script must be idempotent, meaning it can be run multiple times without causing unintended side effects.
If your schema changes dynamically, ensure that this script handles such cases gracefully to maintain database integrity.
'';
example = lib.literalExpression ''
"path/to/your/schema/setup/script.sh"
'';
};

hbaConf = lib.mkOption {
type = types.nullOr types.str;
default = null;
Expand Down
3 changes: 3 additions & 0 deletions tests/postgresql-localhost/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
initialScript = ''
CREATE USER postgres SUPERUSER;
'';
setupSchemaScript = ''
echo "script to run to setup or update database schema. This script must be idempotent."
'';
};
}