Skip to content

Commit cb17376

Browse files
committed
refactor: deduplicate host and port
1 parent 29f0834 commit cb17376

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

flake.nix

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
flake-utils.lib.eachSystem ourSystems (system:
2323
let
2424
pgsqlDefaultPort = "5435";
25+
pgsqlDefaultHost = "localhost";
2526
pgsqlSuperuser = "supabase_admin";
2627

2728
pkgs = import nixpkgs {
@@ -345,15 +346,15 @@
345346
PGBOUNCER_AUTH_SCHEMA_SQL = "${paths.pgbouncerAuthSchemaSql}";
346347
STAT_EXTENSION_SQL = "${paths.statExtensionSql}";
347348
CURRENT_SYSTEM = "${system}";
348-
} // extraSubstitutions; # Merge in any extra substitutions
349+
} // extraSubstitutions; # Merge in any extra substitutions
349350
in
350351
pkgs.runCommand name
351352
{
352353
inherit (paths) migrationsDir postgresqlSchemaSql pgbouncerAuthSchemaSql statExtensionSql;
353354
} ''
354355
set -x
355356
mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql $out/extension-custom-scripts
356-
357+
357358
# Copy config files with error handling
358359
cp ${paths.supautilsConfigFile} $out/etc/postgresql-custom/supautils.conf || { echo "Failed to copy supautils.conf"; exit 1; }
359360
cp ${paths.pgconfigFile} $out/etc/postgresql/postgresql.conf || { echo "Failed to copy postgresql.conf"; exit 1; }
@@ -362,16 +363,16 @@
362363
cp ${paths.pgHbaConfigFile} $out/etc/postgresql/pg_hba.conf || { echo "Failed to copy pg_hba.conf"; exit 1; }
363364
cp ${paths.pgIdentConfigFile} $out/etc/postgresql/pg_ident.conf || { echo "Failed to copy pg_ident.conf"; exit 1; }
364365
cp -r ${paths.postgresqlExtensionCustomScriptsPath}/* $out/extension-custom-scripts/ || { echo "Failed to copy custom scripts"; exit 1; }
365-
366+
366367
echo "Copy operation completed"
367368
chmod 644 $out/etc/postgresql-custom/supautils.conf
368369
chmod 644 $out/etc/postgresql/postgresql.conf
369370
chmod 644 $out/etc/postgresql-custom/logging.conf
370371
chmod 644 $out/etc/postgresql/pg_hba.conf
371372
372373
substitute ${./nix/tools/run-server.sh.in} $out/bin/start-postgres-server \
373-
${builtins.concatStringsSep " " (builtins.attrValues (builtins.mapAttrs
374-
(name: value: "--subst-var-by '${name}' '${value}'")
374+
${builtins.concatStringsSep " " (builtins.attrValues (builtins.mapAttrs
375+
(name: value: "--subst-var-by '${name}' '${value}'")
375376
substitutions
376377
))}
377378
chmod +x $out/bin/start-postgres-server
@@ -560,7 +561,7 @@
560561
chmod +x $out/bin/pg-restore
561562
'';
562563
sync-exts-versions = pkgs.runCommand "sync-exts-versions" { } ''
563-
mkdir -p $out/bin
564+
mkdir -p $out/bin
564565
substitute ${./nix/tools/sync-exts-versions.sh.in} $out/bin/sync-exts-versions \
565566
--subst-var-by 'YQ' '${pkgs.yq}/bin/yq' \
566567
--subst-var-by 'JQ' '${pkgs.jq}/bin/jq' \
@@ -595,7 +596,7 @@
595596
makeWrapper
596597
];
597598
} ''
598-
mkdir -p $out/bin $out/migrations
599+
mkdir -p $out/bin $out/migrations
599600
cp -r ${migrationsDir}/* $out
600601
substitute ${./nix/tools/dbmate-tool.sh.in} $out/bin/dbmate-tool \
601602
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
@@ -1071,10 +1072,10 @@
10711072
# Wait for workflow to start and get the run ID
10721073
echo "Waiting for workflow to start..."
10731074
sleep 5
1074-
1075+
10751076
# Get the latest run ID for this workflow
10761077
RUN_ID=$(gh run list --workflow=nix-build.yml --limit 1 --json databaseId --jq '.[0].databaseId')
1077-
1078+
10781079
if [ -z "$RUN_ID" ]; then
10791080
echo "Error: Could not find workflow run ID"
10801081
exit 1
@@ -1116,22 +1117,22 @@
11161117
cat > $out/bin/pgsodium-getkey << 'EOF'
11171118
#!${pkgs.bash}/bin/bash
11181119
set -euo pipefail
1119-
1120+
11201121
TMPDIR_BASE=$(mktemp -d)
1121-
1122+
11221123
if [[ "$(uname)" == "Darwin" ]]; then
11231124
KEY_DIR="/private/tmp/pgsodium"
11241125
else
11251126
KEY_DIR="''${PGSODIUM_KEY_DIR:-$TMPDIR_BASE/pgsodium}"
11261127
fi
11271128
KEY_FILE="$KEY_DIR/pgsodium.key"
1128-
1129+
11291130
if ! mkdir -p "$KEY_DIR" 2>/dev/null; then
11301131
echo "Error: Could not create key directory $KEY_DIR" >&2
11311132
exit 1
11321133
fi
11331134
chmod 1777 "$KEY_DIR"
1134-
1135+
11351136
if [[ ! -f "$KEY_FILE" ]]; then
11361137
if ! (dd if=/dev/urandom bs=32 count=1 2>/dev/null | od -A n -t x1 | tr -d ' \n' > "$KEY_FILE"); then
11371138
if ! (openssl rand -hex 32 > "$KEY_FILE"); then
@@ -1141,7 +1142,7 @@
11411142
fi
11421143
chmod 644 "$KEY_FILE"
11431144
fi
1144-
1145+
11451146
if [[ -f "$KEY_FILE" && -r "$KEY_FILE" ]]; then
11461147
cat "$KEY_FILE"
11471148
else
@@ -1245,7 +1246,7 @@
12451246
substitute ${./nix/tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \
12461247
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey"
12471248
echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf
1248-
echo "port = 5435" >> "$PGTAP_CLUSTER"/postgresql.conf
1249+
echo "port = ${pgsqlDefaultPort}" >> "$PGTAP_CLUSTER"/postgresql.conf
12491250
echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf
12501251
echo "Checking shared_preload_libraries setting:"
12511252
grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf
@@ -1266,18 +1267,18 @@
12661267
12671268
# PostgreSQL startup
12681269
if [[ "$(uname)" == "Darwin" ]]; then
1269-
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p 5435 -d 5" start 2>&1
1270+
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p ${pgsqlDefaultPort} -d 5" start 2>&1
12701271
else
12711272
mkdir -p "$PGTAP_CLUSTER/sockets"
1272-
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p 5435 -d 5" start 2>&1
1273+
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p ${pgsqlDefaultPort} -d 5" start 2>&1
12731274
fi || {
1274-
echo "pg_ctl failed to start PostgreSQL"
1275+
echo "pg_ctl failed to start PostgreSQL"
12751276
echo "Contents of postgresql.log:"
12761277
cat "$PGTAP_CLUSTER"/postgresql.log
12771278
exit 1
12781279
}
12791280
for i in {1..60}; do
1280-
if pg_isready -h localhost -p 5435; then
1281+
if pg_isready -h ${pgsqlDefaultHost} -p ${pgsqlDefaultPort}; then
12811282
echo "PostgreSQL is ready"
12821283
break
12831284
fi
@@ -1291,24 +1292,24 @@
12911292
exit 1
12921293
fi
12931294
done
1294-
createdb -p 5435 -h localhost --username=supabase_admin testing
1295-
if ! psql -p 5435 -h localhost --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
1295+
createdb -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --username=supabase_admin testing
1296+
if ! psql -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
12961297
echo "Error executing SQL file. PostgreSQL log content:"
12971298
cat "$PGTAP_CLUSTER"/postgresql.log
12981299
pg_ctl -D "$PGTAP_CLUSTER" stop
12991300
exit 1
13001301
fi
13011302
SORTED_DIR=$(mktemp -d)
13021303
for t in $(printf "%s\n" ${builtins.concatStringsSep " " sortedTestList}); do
1303-
psql -p 5435 -h localhost --username=supabase_admin -d testing -f "${./nix/tests/sql}/$t.sql" || true
1304+
psql -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --username=supabase_admin -d testing -f "${./nix/tests/sql}/$t.sql" || true
13041305
done
13051306
rm -rf "$SORTED_DIR"
13061307
pg_ctl -D "$PGTAP_CLUSTER" stop
13071308
rm -rf $PGTAP_CLUSTER
1308-
1309+
13091310
# End of pgtap tests
13101311
# from here on out we are running pg_regress tests, we use a different cluster for this
1311-
# which is start by the start-postgres-server-bin script
1312+
# which is start by the start-postgres-server-bin script
13121313
# start-postgres-server-bin script closely matches our AMI setup, configurations and migrations
13131314
13141315
# Ensure pgsodium key directory exists with proper permissions
@@ -1318,9 +1319,9 @@
13181319
fi
13191320
unset GRN_PLUGINS_DIR
13201321
${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize
1321-
1322+
13221323
for i in {1..60}; do
1323-
if pg_isready -h localhost -p 5435 -U supabase_admin -q; then
1324+
if pg_isready -h ${pgsqlDefaultHost} -p ${pgsqlDefaultPort} -U supabase_admin -q; then
13241325
echo "PostgreSQL is ready"
13251326
break
13261327
fi
@@ -1331,7 +1332,7 @@
13311332
fi
13321333
done
13331334
1334-
if ! psql -p 5435 -h localhost --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
1335+
if ! psql -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
13351336
echo "Error executing SQL file"
13361337
exit 1
13371338
fi
@@ -1342,8 +1343,8 @@
13421343
--dbname=postgres \
13431344
--inputdir=${./nix/tests} \
13441345
--outputdir=$out/regression_output \
1345-
--host=localhost \
1346-
--port=5435 \
1346+
--host=${pgsqlDefaultHost} \
1347+
--port=${pgsqlDefaultPort} \
13471348
--user=supabase_admin \
13481349
${builtins.concatStringsSep " " sortedTestList}; then
13491350
echo "pg_regress tests failed"
@@ -1352,7 +1353,7 @@
13521353
fi
13531354
13541355
echo "Running migrations tests"
1355-
pg_prove -p 5435 -U supabase_admin -h localhost -d postgres -v ${./migrations/tests}/test.sql
1356+
pg_prove -p ${pgsqlDefaultPort} -U supabase_admin -h ${pgsqlDefaultHost} -d postgres -v ${./migrations/tests}/test.sql
13561357
13571358
# Copy logs to output
13581359
for logfile in $(find /tmp -name postgresql.log -type f); do

0 commit comments

Comments
 (0)