add before and after options to initialScript in postgres (#5)

This commit is contained in:
Shivaraj B H 2023-07-10 21:10:52 +05:30 committed by GitHub
parent beaaca9f55
commit 4e181b2a0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 19 deletions

View File

@ -201,19 +201,41 @@ in
}; };
initialScript = lib.mkOption { initialScript = lib.mkOption {
type = types.submodule ({ config, ... }: {
options = {
before = lib.mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = null; default = null;
description = '' description = ''
Initial SQL commands to run during database initialization. This can be multiple SQL commands to run before the database initialization.
SQL expressions separated by a semi-colon.
NOTE: initialScript is run /before/ initialDatabases are created.
''; '';
example = lib.literalExpression '' example = lib.literalExpression ''
CREATE USER postgres SUPERUSER; CREATE USER postgres SUPERUSER;
CREATE USER bar; CREATE USER bar;
''; '';
}; };
after = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = ''
SQL commands to run after the database initialization.
'';
example = lib.literalExpression ''
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE
);
'';
};
};
});
default = { before = null; after = null; };
description = ''
Initial SQL commands to run during database initialization. This can be multiple
SQL expressions separated by a semi-colon.
'';
};
}; };
}); });
}; };
@ -280,12 +302,17 @@ in
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | postgres --single -E postgres ''; echo "CREATE DATABASE ''${USER:-$(id -nu)};" | postgres --single -E postgres '';
runInitialScript = runInitialScript =
if cfg.initialScript != null then let
'' scriptCmd = sqlScript: ''
echo "${cfg.initialScript}" | postgres --single -E postgres echo "${sqlScript}" | postgres --single -E postgres
'' '';
else in
""; {
before = with cfg.initialScript;
lib.optionalString (before != null) (scriptCmd before);
after = with cfg.initialScript;
lib.optionalString (after != null) (scriptCmd after);
};
toStr = value: toStr = value:
if true == value then if true == value then
@ -309,8 +336,9 @@ in
initdb ${lib.concatStringsSep " " cfg.initdbArgs} initdb ${lib.concatStringsSep " " cfg.initdbArgs}
set +x set +x
${runInitialScript} ${runInitialScript.before}
${setupInitialDatabases} ${setupInitialDatabases}
${runInitialScript.after}
else else
echo "Postgres data directory already exists. Skipping initialization." echo "Postgres data directory already exists. Skipping initialization."
fi fi

View File

@ -2,11 +2,10 @@
services.postgres = { services.postgres = {
enable = true; enable = true;
listen_addresses = "127.0.0.1"; listen_addresses = "127.0.0.1";
initialScript.before = "CREATE USER bar;";
initialScript.after = "CREATE DATABASE foo OWNER bar;";
}; };
testScript = testScript = let psql = "${config.services.postgres.package}/bin/psql"; in
let
psql = "${config.services.postgres.package}/bin/psql";
in
'' ''
process_compose.wait_until(lambda procs: process_compose.wait_until(lambda procs:
# TODO: Check for 'ready' # TODO: Check for 'ready'
@ -14,5 +13,9 @@
) )
machine.succeed("echo 'SELECT version();' | ${config.services.postgres.package}/bin/psql -h 127.0.0.1 -U tester") machine.succeed("echo 'SELECT version();' | ${config.services.postgres.package}/bin/psql -h 127.0.0.1 -U tester")
machine.succeed("echo 'SHOW hba_file;' | ${psql} -h 127.0.0.1 -U tester | ${pkgs.gawk}/bin/awk 'NR==3' | ${pkgs.gnugrep}/bin/grep '^ /nix/store'") machine.succeed("echo 'SHOW hba_file;' | ${psql} -h 127.0.0.1 -U tester | ${pkgs.gawk}/bin/awk 'NR==3' | ${pkgs.gnugrep}/bin/grep '^ /nix/store'")
# initialScript.before test
machine.succeed("echo \"SELECT 1 FROM pg_roles WHERE rolname = 'bar';\" | ${psql} -h 127.0.0.1 -U tester | grep -q 1")
# initialScript.after test
machine.succeed("echo \"SELECT 1 FROM pg_database WHERE datname = 'foo';\" | ${psql} -h 127.0.0.1 -U tester | grep -q 1")
''; '';
} }