Skip to content

Commit ea8ea5e

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 ea8ea5e

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-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

+33
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,22 @@ 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+
${runSetupSchemaScript}
176+
echo
177+
echo "PostgreSQL setup schema complete."
178+
echo
179+
'';
180+
161181
startScript = pkgs.writeShellScriptBin "start-postgres" ''
162182
set -euo pipefail
163183
mkdir -p ${q runtimeDir}
164184
${setupScript}/bin/setup-postgres
185+
${setupSchemaScript}/bin/setup-schema-script
165186
exec ${postgresPkg}/bin/postgres
166187
'';
167188
in
@@ -327,6 +348,18 @@ in
327348
'';
328349
};
329350

351+
setupSchemaScript = lib.mkOption {
352+
type = types.nullOr types.str;
353+
default = null;
354+
description = ''
355+
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.
356+
If your schema changes dynamically, ensure that this script handles such cases gracefully to maintain database integrity.
357+
'';
358+
example = lib.literalExpression ''
359+
"path/to/your/schema/setup/script.sh"
360+
'';
361+
};
362+
330363
hbaConf = lib.mkOption {
331364
type = types.nullOr types.str;
332365
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)