Skip to content

Commit

Permalink
[Postgres]: Use schemas in-place of initialDumps (#73)
Browse files Browse the repository at this point in the history
* (postgres) rename schema to schemas; rm initialDumps, use schemas instead
  • Loading branch information
shivaraj-bh authored Jan 13, 2024
1 parent 2211042 commit 62e1371
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion example/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
initialDatabases = [
{
name = dbName;
schema = "${inputs.northwind}/northwind.sql";
schemas = [ "${inputs.northwind}/northwind.sql" ];
}
];
};
Expand Down
18 changes: 4 additions & 14 deletions nix/postgres/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ in
The name of the database to create.
'';
};
schema = lib.mkOption {
type = types.nullOr types.path;
schemas = lib.mkOption {
type = types.nullOr (types.listOf types.path);
default = null;
description = ''
The initial schema of the database; if null (the default),
The initial list of schemas for the database; if null (the default),
an empty database is created.
'';
};
Expand All @@ -223,7 +223,7 @@ in
[
{
name = "foodatabase";
schema = ./foodatabase.sql;
schemas = [ ./fooschemas ./bar.sql ];
}
{ name = "bardatabase"; }
]
Expand All @@ -248,16 +248,6 @@ in
default = null;
};

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 {
type = types.submodule ({ config, ... }: {
options = {
Expand Down
17 changes: 14 additions & 3 deletions nix/postgres/postgres_test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
listen_addresses = "127.0.0.1";
initialScript.before = "CREATE USER bar;";
initialScript.after = "CREATE DATABASE foo OWNER bar;";
initialDumps = [ ./test.sql ];
};
services.postgres."pg2" = {
enable = true;
port = 5433;
listen_addresses = "127.0.0.1";
# INFO: pg1 creates $USER database while pg2 doesn't because `initialDatabases` is present
initialDatabases = [
{
name = "sample-db";
schemas = [ ./test.sql ];
}
];
};
settings.processes.test =
let
Expand All @@ -23,8 +34,8 @@
# initialScript.after test
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
# schemas test
echo "SELECT * from users where user_name = 'test_user';" | psql -h 127.0.0.1 -p 5433 -d sample-db | grep -q test_user
'';
name = "postgres-test";
};
Expand Down
53 changes: 23 additions & 30 deletions nix/postgres/setup-script.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
{ config, pkgs, lib }:
let
setupInitialSchema = dbName: schema: ''
${lib.optionalString (schema != null) ''
echo "Applying database schema on ${dbName}"
if [ -f "${schema}" ]
then
echo "Running file ${schema}"
awk 'NF' "${schema}" | psql -d ${dbName}
elif [ -d "${schema}" ]
then
# Read sql files in version order. Apply one file
# at a time to handle files where the last statement
# doesn't end in a ;.
find "${schema}"/*.sql | while read -r f ; do
echo "Applying sql file: $f"
awk 'NF' "$f" | psql -d ${dbName}
done
else
echo "ERROR: Could not determine how to apply schema with ${schema}"
exit 1
fi
''}
'';
setupInitialDatabases =
if config.initialDatabases != [ ] then
(lib.concatMapStrings
Expand All @@ -15,42 +37,14 @@ let
if [ 1 -ne "$dbAlreadyExists" ]; then
echo "Creating database: ${database.name}"
echo 'create database "${database.name}";' | psql -d postgres
${lib.optionalString (database.schema != null) ''
echo "Applying database schema on ${database.name}"
if [ -f "${database.schema}" ]
then
echo "Running file ${database.schema}"
awk 'NF' "${database.schema}" | psql -d ${database.name}
elif [ -d "${database.schema}" ]
then
# Read sql files in version order. Apply one file
# at a time to handle files where the last statement
# doesn't end in a ;.
find "${database.schema}"/*.sql | while read -r f ; do
echo "Applying sql file: $f"
awk 'NF' "$f" | psql -d ${database.name}
done
else
echo "ERROR: Could not determine how to apply schema with ${database.schema}"
exit 1
fi
''}
${lib.concatMapStrings (schema: setupInitialSchema (database.name) schema) database.schemas}
fi
'')
config.initialDatabases)
else
lib.optionalString config.createDatabase ''
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 =
let
Expand Down Expand Up @@ -121,7 +115,6 @@ in
${runInitialScript.before}
${setupInitialDatabases}
${runInitialScript.after}
${runInitialDumps}
pg_ctl -D "$PGDATA" -m fast -w stop
remove_tmp_pg_init_sock_dir "$PGHOST"
else
Expand Down

0 comments on commit 62e1371

Please sign in to comment.