8000 refactor: deduplicate host and port · foxgis/supabase-postgres@897bb88 · GitHub
[go: up one dir, main page]

Skip to content

Commit 897bb88

Browse files
committed
refactor: deduplicate host and port
1 parent 9669e7b commit 897bb88

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

flake.nix

Lines changed: 31 additions & 30 deletions
unset GRN_PLUGINS_DIR
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 {
@@ -349,15 +350,15 @@
349350
PGBOUNCER_AUTH_SCHEMA_SQL = "${paths.pgbouncerAuthSchemaSql}";
350351
STAT_EXTENSION_SQL = "${paths.statExtensionSql}";
351352
CURRENT_SYSTEM = "${system}";
352-
} // extraSubstitutions; # Merge in any extra substitutions
353+
} // extraSubstitutions; # Merge in any extra substitutions
353354
in
354355
pkgs.runCommand name
355356
{
356357
inherit (paths) migrationsDir postgresqlSchemaSql pgbouncerAuthSchemaSql statExtensionSql;
357358
} ''
358359
set -x
359360
mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql $out/extension-custom-scripts
360-
361+
361362
# Copy config files with error handling
362363
cp ${paths.supautilsConfigFile} $out/etc/postgresql-custom/supautils.conf || { echo "Failed to copy supautils.conf"; exit 1; }
363364
cp ${paths.pgconfigFile} $out/etc/postgresql/postgresql.conf || { echo "Failed to copy postgresql.conf"; exit 1; }
@@ -366,16 +367,16 @@
366367
cp ${paths.pgHbaConfigFile} $out/etc/postgresql/pg_hba.conf || { echo "Failed to copy pg_hba.conf"; exit 1; }
367368
cp ${paths.pgIdentConfigFile} $out/etc/postgresql/pg_ident.conf || { echo "Failed to copy pg_ident.conf"; exit 1; }
368369
cp -r ${paths.postgresqlExtensionCustomScriptsPath}/* $out/extension-custom-scripts/ || { echo "Failed to copy custom scripts"; exit 1; }
369-
370+
370371
echo "Copy operation completed"
371372
chmod 644 $out/etc/postgresql-custom/supautils.conf
372373
chmod 644 $out/etc/postgresql/postgresql.conf
373374
chmod 644 $out/etc/postgresql-custom/logging.conf
374375
chmod 644 $out/etc/postgresql/pg_hba.conf
375376
376377
substitute ${./nix/tools/run-server.sh.in} $out/bin/start-postgres-server \
377-
${builtins.concatStringsSep " " (builtins.attrValues (builtins.mapAttrs
378-
(name: value: "--subst-var-by '${name}' '${value}'")
378+
${builtins.concatStringsSep " " (builtins.attrValues (builtins.mapAttrs
379+
(name: value: "--subst-var-by '${name}' '${value}'")
379380
substitutions
380381
))}
381382
chmod +x $out/bin/start-postgres-server
@@ -565,7 +566,7 @@
565566
chmod +x $out/bin/pg-restore
566567
'';
567568
sync-exts-versions = pkgs.runCommand "sync-exts-versions" { } ''
568-
mkdir -p $out/bin
569+
mkdir -p $out/bin
569570
substitute ${./nix/tools/sync-exts-versions.sh.in} $out/bin/sync-exts-versions \
570571
--subst-var-by 'YQ' '${pkgs.yq}/bin/yq' \
571572
--subst-var-by 'JQ' '${pkgs.jq}/bin/jq' \
@@ -600,7 +601,7 @@
600601
makeWrapper
601602
];
602603
} ''
603-
mkdir -p $out/bin $out/migrations
604+
mkdir -p $out/bin $out/migrations
604605
cp -r ${migrationsDir}/* $out
605606
substitute ${./nix/tools/dbmate-tool.sh.in} $out/bin/dbmate-tool \
606607
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
@@ -1076,10 +1077,10 @@
10761077
# Wait for workflow to start and get the run ID
10771078
echo "Waiting for workflow to start..."
10781079
sleep 5
1079-
1080+
10801081
# Get the latest run ID for this workflow
10811082
RUN_ID=$(gh run list --workflow=nix-build.yml --limit 1 --json databaseId --jq '.[0].databaseId')
1082-
1083+
10831084
if [ -z "$RUN_ID" ]; then
10841085
echo "Error: Could not find workflow run ID"
10851086
exit 1
@@ -1121,22 +1122,22 @@
11211122
cat > $out/bin/pgsodium-getkey << 'EOF'
11221123
#!${pkgs.bash}/bin/bash
11231124
set -euo pipefail
1124-
1125+
11251126
TMPDIR_BASE=$(mktemp -d)
1126-
1127+
11271128
if [[ "$(uname)" == "Darwin" ]]; then
11281129
KEY_DIR="/private/tmp/pgsodium"
11291130
else
11301131
KEY_DIR="''${PGSODIUM_KEY_DIR:-$TMPDIR_BASE/pgsodium}"
11311132
fi
11321133
KEY_FILE="$KEY_DIR/pgsodium.key"
1133-
1134+
11341135
if ! mkdir -p "$KEY_DIR" 2>/dev/null; then
11351136
echo "Error: Could not create key directory $KEY_DIR" >&2
11361137
exit 1
11371138
fi
11381139
chmod 1777 "$KEY_DIR"
1139-
1140+
11401141
if [[ ! -f "$KEY_FILE" ]]; then
11411142
if ! (dd if=/dev/urandom bs=32 count=1 2>/dev/null | od -A n -t x1 | tr -d ' \n' > "$KEY_FILE"); then
11421143
if ! (openssl rand -hex 32 > "$KEY_FILE"); then
@@ -1146,7 +1147,7 @@
11461147
fi
11471148
chmod 644 "$KEY_FILE"
11481149
fi
1149-
1150+
11501151
if [[ -f "$KEY_FILE" && -r "$KEY_FILE" ]]; then
11511152
cat "$KEY_FILE"
11521153
else
@@ -1250,7 +1251,7 @@
12501251
substitute ${./nix/tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \
12511252
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey"
12521253
echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf
1253-
echo "port = 5435" >> "$PGTAP_CLUSTER"/postgresql.conf
1254+
echo "port = ${pgsqlDefaultPort}" >> "$PGTAP_CLUSTER"/postgresql.conf
12541255
echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf
12551256
echo "Checking shared_preload_libraries setting:"
12561257
grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf
@@ -1271,18 +1272,18 @@
12711272
12721273
# PostgreSQL startup
12731274
if [[ "$(uname)" == "Darwin" ]]; then
1274-
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p 5435 -d 5" start 2>&1
1275+
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p ${pgsqlDefaultPort} -d 5" start 2>&1
12751276
else
12761277
mkdir -p "$PGTAP_CLUSTER/sockets"
1277-
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p 5435 -d 5" start 2>&1
1278+
pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p ${pgsqlDefaultPort} -d 5" start 2>&1
12781279
fi || {
1279-
echo "pg_ctl failed to start PostgreSQL"
1280+
echo "pg_ctl failed to start PostgreSQL"
12801281
echo "Contents of postgresql.log:"
12811282
cat "$PGTAP_CLUSTER"/postgresql.log
12821283
exit 1
12831284
}
12841285
for i in {1..60}; do
1285-
if pg_isready -h localhost -p 5435; then
1286+
if pg_isready -h ${pgsqlDefaultHost} -p ${pgsqlDefaultPort}; then
12861287
echo "PostgreSQL is ready"
12871288
break
12881289
fi
@@ -1296,24 +1297,24 @@
12961297
exit 1
12971298
fi
12981299
done
1299-
createdb -p 5435 -h localhost --username=supabase_admin testing
1300-
if ! psql -p 5435 -h localhost --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
1300+
createdb -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --username=supabase_admin testing
1301+
if ! psql -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
13011302
echo "Error executing SQL file. PostgreSQL log content:"
13021303
cat "$PGTAP_CLUSTER"/postgresql.log
13031304
pg_ctl -D "$PGTAP_CLUSTER" stop
13041305
exit 1
13051306
fi
13061307
SORTED_DIR=$(mktemp -d)
13071308
for t in $(printf "%s\n" ${builtins.concatStringsSep " " sortedTestList}); do
1308-
psql -p 5435 -h localhost --username=supabase_admin -d testing -f "${./nix/tests/sql}/$t.sql" || true
1309+
psql -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --username=supabase_admin -d testing -f "${./nix/tests/sql}/$t.sql" || true
13091310
done
13101311
rm -rf "$SORTED_DIR"
13111312
pg_ctl -D "$PGTAP_CLUSTER" stop
13121313
rm -rf $PGTAP_CLUSTER
1313-
1314+
13141315
# End of pgtap tests
13151316
# from here on out we are running pg_regress tests, we use a different cluster for this
1316-
# which is start by the start-postgres-server-bin script
1317+
# which is start by the start-postgres-server-bin script
13171318
# start-postgres-server-bin script closely matches our AMI setup, configurations and migrations
13181319
13191320
# Ensure pgsodium key directory exists with proper permissions
@@ -1323,9 +1324,9 @@
13231324
fi
13241325
13251326
${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize
1326-
1327+
13271328
for i in {1..60}; do
1328-
if pg_isready -h localhost -p 5435 -U supabase_admin -q; then
1329+
if pg_isready -h ${pgsqlDefaultHost} -p ${pgsqlDefaultPort} -U supabase_admin -q; then
13291330
echo "PostgreSQL is ready"
13301331
break
13311332
fi
@@ -1336,7 +1337,7 @@
13361337
fi
13371338
done
13381339
1339-
if ! psql -p 5435 -h localhost --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
1340+
if ! psql -p ${pgsqlDefaultPort} -h ${pgsqlDefaultHost} --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xaf ${./nix/tests/prime.sql}; then
13401341
echo "Error executing SQL file"
13411342
exit 1
13421343
fi
@@ -1347,8 +1348,8 @@
13471348
--dbname=postgres \
13481349
--inputdir=${./nix/tests} \
13491350
--outputdir=$out/regression_output \
1350-
--host=localhost \
1351-
--port=5435 \
1351+
--host=${pgsqlDefaultHost} \
1352+
--port=${pgsqlDefaultPort} \
13521353
--user=supabase_admin \
13531354
${builtins.concatStringsSep " " sortedTestList}; then
13541355
echo "pg_regress tests failed"
@@ -1357,7 +1358,7 @@
13571358
fi
13581359
13591360
echo "Running migrations tests"
1360-
pg_prove -p 5435 -U supabase_admin -h localhost -d postgres -v ${./migrations/tests}/test.sql
1361+
pg_prove -p ${pgsqlDefaultPort} -U supabase_admin -h ${pgsqlDefaultHost} -d postgres -v ${./migrations/tests}/test.sql
13611362
13621363
# Copy logs to output
13631364
for logfile in $(find /tmp -name postgresql.log -type f); do

0 commit comments

Comments
 (0)
0