mirror of
https://github.com/juspay/services-flake.git
synced 2024-09-11 12:26:04 +03:00
Adding option initialDumps to Postgres (#67)
--------- Co-authored-by: Shivaraj B H <sbh69840@gmail.com>
This commit is contained in:
parent
22e121b246
commit
d6b73e401e
@ -230,6 +230,16 @@ in
|
|||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
initialDumps = lib.mkOption {
|
||||||
|
type = types.listOf types.path;
|
||||||
|
default = [ ];
|
||||||
|
description = ''List of SQL dumps to run during the database initialization.
|
||||||
|
These dumps are loaded after `initalScript` and `initialDatabases`.'';
|
||||||
|
example = lib.literalExpression ''
|
||||||
|
[ ./foo.sql ./bar.sql ]
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
initialScript = lib.mkOption {
|
initialScript = lib.mkOption {
|
||||||
type = types.submodule ({ config, ... }: {
|
type = types.submodule ({ config, ... }: {
|
||||||
options = {
|
options = {
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
listen_addresses = "127.0.0.1";
|
listen_addresses = "127.0.0.1";
|
||||||
initialScript.before = "CREATE USER bar;";
|
initialScript.before = "CREATE USER bar;";
|
||||||
initialScript.after = "CREATE DATABASE foo OWNER bar;";
|
initialScript.after = "CREATE DATABASE foo OWNER bar;";
|
||||||
|
initialDumps = [ ./test.sql ];
|
||||||
};
|
};
|
||||||
settings.processes.test =
|
settings.processes.test =
|
||||||
let
|
let
|
||||||
@ -21,6 +22,9 @@
|
|||||||
|
|
||||||
# initialScript.after test
|
# initialScript.after test
|
||||||
echo "SELECT 1 FROM pg_database WHERE datname = 'foo';" | psql -h 127.0.0.1 | grep -q 1
|
echo "SELECT 1 FROM pg_database WHERE datname = 'foo';" | psql -h 127.0.0.1 | grep -q 1
|
||||||
|
|
||||||
|
#intialDumps test
|
||||||
|
echo "SELECT * from users where user_name = 'test_user';" | psql -h 127.0.0.1 -d postgres | grep -q test_user
|
||||||
'';
|
'';
|
||||||
name = "postgres-test";
|
name = "postgres-test";
|
||||||
};
|
};
|
||||||
|
@ -43,6 +43,15 @@ let
|
|||||||
else
|
else
|
||||||
lib.optionalString config.createDatabase ''
|
lib.optionalString config.createDatabase ''
|
||||||
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql -d postgres '';
|
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql -d postgres '';
|
||||||
|
|
||||||
|
runInitialDumps =
|
||||||
|
let
|
||||||
|
scriptCmd = dump: ''
|
||||||
|
psql -d postgres < ${dump}
|
||||||
|
'';
|
||||||
|
in
|
||||||
|
builtins.concatStringsSep "\n" (map scriptCmd config.initialDumps);
|
||||||
|
|
||||||
runInitialScript =
|
runInitialScript =
|
||||||
let
|
let
|
||||||
scriptCmd = sqlScript: ''
|
scriptCmd = sqlScript: ''
|
||||||
@ -112,6 +121,7 @@ in
|
|||||||
${runInitialScript.before}
|
${runInitialScript.before}
|
||||||
${setupInitialDatabases}
|
${setupInitialDatabases}
|
||||||
${runInitialScript.after}
|
${runInitialScript.after}
|
||||||
|
${runInitialDumps}
|
||||||
pg_ctl -D "$PGDATA" -m fast -w stop
|
pg_ctl -D "$PGDATA" -m fast -w stop
|
||||||
remove_tmp_pg_init_sock_dir "$PGHOST"
|
remove_tmp_pg_init_sock_dir "$PGHOST"
|
||||||
else
|
else
|
||||||
|
4
nix/postgres/test.sql
Normal file
4
nix/postgres/test.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
CREATE TABLE users (id INT PRIMARY KEY, user_name VARCHAR(25));
|
||||||
|
|
||||||
|
INSERT INTO users values (1, 'test_user');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user