Skip to content

Commit 03c975d

Browse files
committed
postgres: Add setupSchemaScript option for Schema Setup
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 you change your schema dynamically, ensure that this script handles such cases gracefully to maintain database integrity.
1 parent efa9010 commit 03c975d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

examples/postgres/devenv.nix

+3
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
initialScript = ''
1212
CREATE EXTENSION IF NOT EXISTS postgis;
1313
'';
14+
setupSchemaScript = ''
15+
echo "script to run to setup or update database schema. This script must be idempotent."
16+
'';
1417
};
1518
}

src/modules/services/postgres.nix

+40
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ let
9898
''
9999
else "";
100100

101+
runSetupSchemaScript =
102+
if cfg.setupSchemaScript == null
103+
then ''
104+
echo "script not provided, skipping."
105+
''
106+
else ''
107+
${cfg.setupSchemaScript}
108+
'';
109+
101110
toStr = value:
102111
if true == value
103112
then "yes"
@@ -158,10 +167,29 @@ let
158167
fi
159168
unset POSTGRES_RUN_INITIAL_SCRIPT
160169
'';
170+
171+
setupSchemaScript = pkgs.writeShellScriptBin "setup-schema-script" ''
172+
echo
173+
echo "PostgreSQL is setting up the schema"
174+
echo
175+
OLDPGHOST="$PGHOST"
176+
PGHOST=${q runtimeDir}
177+
178+
pg_ctl -D "$PGDATA" -w start -o "-c unix_socket_directories=${runtimeDir} -c listen_addresses= -p ${toString cfg.port}"
179+
${runSetupSchemaScript}
180+
pg_ctl -D "$PGDATA" -m fast -w stop
181+
PGHOST="$OLDPGHOST"
182+
unset OLDPGHOST
183+
echo
184+
echo "PostgreSQL setup schema complete."
185+
echo
186+
'';
187+
161188
startScript = pkgs.writeShellScriptBin "start-postgres" ''
162189
set -euo pipefail
163190
mkdir -p ${q runtimeDir}
164191
${setupScript}/bin/setup-postgres
192+
${setupSchemaScript}/bin/setup-schema-script
165193
exec ${postgresPkg}/bin/postgres
166194
'';
167195
in
@@ -327,6 +355,18 @@ in
327355
'';
328356
};
329357

358+
setupSchemaScript = lib.mkOption {
359+
type = types.nullOr types.str;
360+
default = null;
361+
description = ''
362+
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.
363+
If your schema changes dynamically, ensure that this script handles such cases gracefully to maintain database integrity.
364+
'';
365+
example = lib.literalExpression ''
366+
"path/to/your/schema/setup/script.sh"
367+
'';
368+
};
369+
330370
hbaConf = lib.mkOption {
331371
type = types.nullOr types.str;
332372
default = null;

tests/postgresql-localhost/devenv.nix

+3
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
initialScript = ''
77
CREATE USER postgres SUPERUSER;
88
'';
9+
setupSchemaScript = ''
10+
echo "script to run to setup or update database schema. This script must be idempotent."
11+
'';
912
};
1013
}

0 commit comments

Comments
 (0)