mirror of
https://github.com/juspay/services-flake.git
synced 2024-08-16 15:50:38 +03:00
[Postgres]: Use schemas
in-place of initialDumps
(#73)
* (postgres) rename schema to schemas; rm initialDumps, use schemas instead
This commit is contained in:
parent
2211042f3e
commit
62e13715f8
@ -33,7 +33,7 @@
|
||||
initialDatabases = [
|
||||
{
|
||||
name = dbName;
|
||||
schema = "${inputs.northwind}/northwind.sql";
|
||||
schemas = [ "${inputs.northwind}/northwind.sql" ];
|
||||
}
|
||||
];
|
||||
};
|
||||
|
@ -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.
|
||||
'';
|
||||
};
|
||||
@ -223,7 +223,7 @@ in
|
||||
[
|
||||
{
|
||||
name = "foodatabase";
|
||||
schema = ./foodatabase.sql;
|
||||
schemas = [ ./fooschemas ./bar.sql ];
|
||||
}
|
||||
{ name = "bardatabase"; }
|
||||
]
|
||||
@ -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 = {
|
||||
|
@ -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
|
||||
@ -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";
|
||||
};
|
||||
|
@ -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
|
||||
@ -15,28 +37,7 @@ 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)
|
||||
@ -44,13 +45,6 @@ let
|
||||
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
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user