Skip to content

Commit 12e7482

Browse files
committed
fix(postgres): stop init on error in sql scripts
From psql docs (https://www.postgresql.org/docs/current/app-psql.html): > psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g., out of memory, file not found), 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set. Also resolves #107
1 parent 8d64812 commit 12e7482

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

nix/postgres/setup-script.nix

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
{ config, pkgs, lib }:
22
let
3-
psqlUserArg = lib.optionalString (config.superuser != null) "-U ${config.superuser}";
43
setupInitialSchema = dbName: schema: ''
54
echo "Applying database schema on ${dbName}"
65
if [ -f "${schema}" ]
76
then
87
echo "Running file ${schema}"
9-
awk 'NF' "${schema}" | psql ${psqlUserArg} -d ${dbName}
8+
awk 'NF' "${schema}" | psql_with_args -d ${dbName}
109
elif [ -d "${schema}" ]
1110
then
1211
# Read sql files in version order. Apply one file
1312
# at a time to handle files where the last statement
1413
# doesn't end in a ;.
1514
find "${schema}"/*.sql | while read -r f ; do
1615
echo "Applying sql file: $f"
17-
awk 'NF' "$f" | psql ${psqlUserArg} -d ${dbName}
16+
awk 'NF' "$f" | psql_with_args -d ${dbName}
1817
done
1918
else
2019
echo "ERROR: Could not determine how to apply schema with ${schema}"
@@ -29,27 +28,27 @@ let
2928
# Create initial databases
3029
dbAlreadyExists=$(
3130
echo "SELECT 1 as exists FROM pg_database WHERE datname = '${database.name}';" | \
32-
psql ${psqlUserArg} -d postgres | \
31+
psql_with_args -d postgres | \
3332
grep -c 'exists = "1"' || true
3433
)
3534
echo "$dbAlreadyExists"
3635
if [ 1 -ne "$dbAlreadyExists" ]; then
3736
echo "Creating database: ${database.name}"
38-
echo 'create database "${database.name}";' | psql ${psqlUserArg} -d postgres
37+
echo 'create database "${database.name}";' | psql_with_args -d postgres
3938
${lib.optionalString (database.schemas != null)
4039
(lib.concatMapStrings (schema: setupInitialSchema (database.name) schema) database.schemas)}
4140
fi
4241
'')
4342
config.initialDatabases)
4443
else
4544
lib.optionalString config.createDatabase ''
46-
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql ${psqlUserArg} -d postgres '';
45+
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql_with_args -d postgres '';
4746

4847

4948
runInitialScript =
5049
let
5150
scriptCmd = sqlScript: ''
52-
echo "${sqlScript}" | psql ${psqlUserArg} -d postgres
51+
echo "${sqlScript}" | psql_with_args -d postgres
5352
'';
5453
in
5554
{
@@ -79,7 +78,11 @@ in
7978
name = "setup-postgres";
8079
runtimeInputs = with pkgs; [ config.package coreutils gnugrep gawk ];
8180
text = ''
82-
set -euo pipefail
81+
set -x
82+
# Execute the `psql` command with default arguments
83+
function psql_with_args() {
84+
psql ${lib.optionalString (config.superuser != null) "-U ${config.superuser}"} -v "ON_ERROR_STOP=1" "$@"
85+
}
8386
# Setup postgres ENVs
8487
export PGDATA="${config.dataDir}"
8588
export PGPORT="${toString config.port}"

0 commit comments

Comments
 (0)