Skip to content

Commit

Permalink
postgres: Add setupSchemaScript option for Schema Setup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
i-am-logger committed Dec 7, 2024
1 parent efa9010 commit 03c975d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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."
'';
};
}

0 comments on commit 03c975d

Please sign in to comment.