Skip to content

Commit

Permalink
fix(postgres): stop init on error in sql scripts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shivaraj-bh committed Jun 6, 2024
1 parent 3412c51 commit 9c92c70
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions nix/postgres/setup-script.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{ config, pkgs, lib }:
let
psqlUserArg = lib.optionalString (config.superuser != null) "-U ${config.superuser}";
setupInitialSchema = dbName: schema: ''
echo "Applying database schema on ${dbName}"
if [ -f "${schema}" ]
then
echo "Running file ${schema}"
awk 'NF' "${schema}" | psql ${psqlUserArg} -d ${dbName}
awk 'NF' "${schema}" | psql_with_args -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 ${psqlUserArg} -d ${dbName}
awk 'NF' "$f" | psql_with_args -d ${dbName}
done
else
echo "ERROR: Could not determine how to apply schema with ${schema}"
Expand All @@ -29,27 +28,27 @@ let
# Create initial databases
dbAlreadyExists=$(
echo "SELECT 1 as exists FROM pg_database WHERE datname = '${database.name}';" | \
psql ${psqlUserArg} -d postgres | \
psql_with_args -d postgres | \
grep -c 'exists = "1"' || true
)
echo "$dbAlreadyExists"
if [ 1 -ne "$dbAlreadyExists" ]; then
echo "Creating database: ${database.name}"
echo 'create database "${database.name}";' | psql ${psqlUserArg} -d postgres
echo 'create database "${database.name}";' | psql_with_args -d postgres
${lib.optionalString (database.schemas != null)
(lib.concatMapStrings (schema: setupInitialSchema (database.name) schema) database.schemas)}
fi
'')
config.initialDatabases)
else
lib.optionalString config.createDatabase ''
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql ${psqlUserArg} -d postgres '';
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | psql_with_args -d postgres '';


runInitialScript =
let
scriptCmd = sqlScript: ''
echo "${sqlScript}" | psql ${psqlUserArg} -d postgres
echo "${sqlScript}" | psql_with_args -d postgres
'';
in
{
Expand Down Expand Up @@ -79,7 +78,11 @@ in
name = "setup-postgres";
runtimeInputs = with pkgs; [ config.package coreutils gnugrep gawk ];
text = ''
set -euo pipefail
set -x
# Execute the `psql` command with default arguments
function psql_with_args() {
psql ${lib.optionalString (config.superuser != null) "-U ${config.superuser}"} -v "ON_ERROR_STOP=1" "$@"
}
# Setup postgres ENVs
export PGDATA="${config.dataDir}"
export PGPORT="${toString config.port}"
Expand Down

0 comments on commit 9c92c70

Please sign in to comment.