|
22 | 22 | flake-utils.lib.eachSystem ourSystems (system:
|
23 | 23 | let
|
24 | 24 | pgsqlDefaultPort = "5435";
|
| 25 | + pgsqlDefaultHost = "localhost"; |
25 | 26 | pgsqlSuperuser = "supabase_admin";
|
26 | 27 |
|
27 | 28 | pkgs = import nixpkgs {
|
|
349 | 350 | PGBOUNCER_AUTH_SCHEMA_SQL = "${paths.pgbouncerAuthSchemaSql}";
|
350 | 351 | STAT_EXTENSION_SQL = "${paths.statExtensionSql}";
|
351 | 352 | CURRENT_SYSTEM = "${system}";
|
352 |
| - } // extraSubstitutions; # Merge in any extra substitutions |
| 353 | + } // extraSubstitutions; # Merge in any extra substitutions |
353 | 354 | in
|
354 | 355 | pkgs.runCommand name
|
355 | 356 | {
|
356 | 357 | inherit (paths) migrationsDir postgresqlSchemaSql pgbouncerAuthSchemaSql statExtensionSql;
|
357 | 358 | } ''
|
358 | 359 | set -x
|
359 | 360 | mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql $out/extension-custom-scripts
|
360 |
| - |
| 361 | +
|
361 | 362 | # Copy config files with error handling
|
362 | 363 | cp ${paths.supautilsConfigFile} $out/etc/postgresql-custom/supautils.conf || { echo "Failed to copy supautils.conf"; exit 1; }
|
363 | 364 | cp ${paths.pgconfigFile} $out/etc/postgresql/postgresql.conf || { echo "Failed to copy postgresql.conf"; exit 1; }
|
|
366 | 367 | cp ${paths.pgHbaConfigFile} $out/etc/postgresql/pg_hba.conf || { echo "Failed to copy pg_hba.conf"; exit 1; }
|
367 | 368 | cp ${paths.pgIdentConfigFile} $out/etc/postgresql/pg_ident.conf || { echo "Failed to copy pg_ident.conf"; exit 1; }
|
368 | 369 | cp -r ${paths.postgresqlExtensionCustomScriptsPath}/* $out/extension-custom-scripts/ || { echo "Failed to copy custom scripts"; exit 1; }
|
369 |
| - |
| 370 | +
|
370 | 371 | echo "Copy operation completed"
|
371 | 372 | chmod 644 $out/etc/postgresql-custom/supautils.conf
|
372 | 373 | chmod 644 $out/etc/postgresql/postgresql.conf
|
373 | 374 | chmod 644 $out/etc/postgresql-custom/logging.conf
|
374 | 375 | chmod 644 $out/etc/postgresql/pg_hba.conf
|
375 | 376 |
|
376 | 377 | 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}'") |
379 | 380 | substitutions
|
380 | 381 | ))}
|
381 | 382 | chmod +x $out/bin/start-postgres-server
|
|
565 | 566 | chmod +x $out/bin/pg-restore
|
566 | 567 | '';
|
567 | 568 | sync-exts-versions = pkgs.runCommand "sync-exts-versions" { } ''
|
568 |
| - mkdir -p $out/bin |
| 569 | + mkdir -p $out/bin |
569 | 570 | substitute ${./nix/tools/sync-exts-versions.sh.in} $out/bin/sync-exts-versions \
|
570 | 571 | --subst-var-by 'YQ' '${pkgs.yq}/bin/yq' \
|
571 | 572 | --subst-var-by 'JQ' '${pkgs.jq}/bin/jq' \
|
|
600 | 601 | makeWrapper
|
601 | 602 | ];
|
602 | 603 | } ''
|
603 |
| - mkdir -p $out/bin $out/migrations |
| 604 | + mkdir -p $out/bin $out/migrations |
604 | 605 | cp -r ${migrationsDir}/* $out
|
605 | 606 | substitute ${./nix/tools/dbmate-tool.sh.in} $out/bin/dbmate-tool \
|
606 | 607 | --subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
|
|
1076 | 1077 | # Wait for workflow to start and get the run ID
|
1077 | 1078 | echo "Waiting for workflow to start..."
|
1078 | 1079 | sleep 5
|
1079 |
| - |
| 1080 | +
|
1080 | 1081 | # Get the latest run ID for this workflow
|
1081 | 1082 | RUN_ID=$(gh run list --workflow=nix-build.yml --limit 1 --json databaseId --jq '.[0].databaseId')
|
1082 |
| - |
| 1083 | +
|
1083 | 1084 | if [ -z "$RUN_ID" ]; then
|
1084 | 1085 | echo "Error: Could not find workflow run ID"
|
1085 | 1086 | exit 1
|
|
1121 | 1122 | cat > $out/bin/pgsodium-getkey << 'EOF'
|
1122 | 1123 | #!${pkgs.bash}/bin/bash
|
1123 | 1124 | set -euo pipefail
|
1124 |
| - |
| 1125 | +
|
1125 | 1126 | TMPDIR_BASE=$(mktemp -d)
|
1126 |
| - |
| 1127 | +
6D40
span> |
1127 | 1128 | if [[ "$(uname)" == "Darwin" ]]; then
|
1128 | 1129 | KEY_DIR="/private/tmp/pgsodium"
|
1129 | 1130 | else
|
1130 | 1131 | KEY_DIR="''${PGSODIUM_KEY_DIR:-$TMPDIR_BASE/pgsodium}"
|
1131 | 1132 | fi
|
1132 | 1133 | KEY_FILE="$KEY_DIR/pgsodium.key"
|
1133 |
| - |
| 1134 | +
|
1134 | 1135 | if ! mkdir -p "$KEY_DIR" 2>/dev/null; then
|
1135 | 1136 | echo "Error: Could not create key directory $KEY_DIR" >&2
|
1136 | 1137 | exit 1
|
1137 | 1138 | fi
|
1138 | 1139 | chmod 1777 "$KEY_DIR"
|
1139 |
| - |
| 1140 | +
|
1140 | 1141 | if [[ ! -f "$KEY_FILE" ]]; then
|
1141 | 1142 | if ! (dd if=/dev/urandom bs=32 count=1 2>/dev/null | od -A n -t x1 | tr -d ' \n' > "$KEY_FILE"); then
|
1142 | 1143 | if ! (openssl rand -hex 32 > "$KEY_FILE"); then
|
|
1146 | 1147 | fi
|
1147 | 1148 | chmod 644 "$KEY_FILE"
|
1148 | 1149 | fi
|
1149 |
| - |
| 1150 | +
|
1150 | 1151 | if [[ -f "$KEY_FILE" && -r "$KEY_FILE" ]]; then
|
1151 | 1152 | cat "$KEY_FILE"
|
1152 | 1153 | else
|
|
1250 | 1251 | substitute ${./nix/tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \
|
1251 | 1252 | --subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey"
|
1252 | 1253 | echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf
|
1253 |
| - echo "port = 5435" >> "$PGTAP_CLUSTER"/postgresql.conf |
| 1254 | + echo "port = ${pgsqlDefaultPort}" >> "$PGTAP_CLUSTER"/postgresql.conf |
1254 | 1255 | echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf
|
1255 | 1256 | echo "Checking shared_preload_libraries setting:"
|
1256 | 1257 | grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf
|
|
1271 | 1272 |
|
1272 | 1273 | # PostgreSQL startup
|
1273 | 1274 | 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 |
1275 | 1276 | else
|
1276 | 1277 | 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 |
1278 | 1279 | fi || {
|
1279 |
| - echo "pg_ctl failed to start PostgreSQL" |
| 1280 | + echo "pg_ctl failed to start PostgreSQL" |
1280 | 1281 | echo "Contents of postgresql.log:"
|
1281 | 1282 | cat "$PGTAP_CLUSTER"/postgresql.log
|
1282 | 1283 | exit 1
|
1283 | 1284 | }
|
1284 | 1285 | for i in {1..60}; do
|
1285 |
| - if pg_isready -h localhost -p 5435; then |
| 1286 | + if pg_isready -h ${pgsqlDefaultHost} -p ${pgsqlDefaultPort}; then |
1286 | 1287 | echo "PostgreSQL is ready"
|
1287 | 1288 | break
|
1288 | 1289 | fi
|
|
1296 | 1297 | exit 1
|
1297 | 1298 | fi
|
1298 | 1299 | 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 |
1301 | 1302 | echo "Error executing SQL file. PostgreSQL log content:"
|
1302 | 1303 | cat "$PGTAP_CLUSTER"/postgresql.log
|
1303 | 1304 | pg_ctl -D "$PGTAP_CLUSTER" stop
|
1304 | 1305 | exit 1
|
1305 | 1306 | fi
|
1306 | 1307 | SORTED_DIR=$(mktemp -d)
|
1307 | 1308 | 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 |
1309 | 1310 | done
|
1310 | 1311 | rm -rf "$SORTED_DIR"
|
1311 | 1312 | pg_ctl -D "$PGTAP_CLUSTER" stop
|
1312 | 1313 | rm -rf $PGTAP_CLUSTER
|
1313 |
| - |
| 1314 | +
|
1314 | 1315 | # End of pgtap tests
|
1315 | 1316 | # 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 |
1317 | 1318 | # start-postgres-server-bin script closely matches our AMI setup, configurations and migrations
|
1318 | 1319 |
|
1319 | 1320 | # Ensure pgsodium key directory exists with proper permissions
|
|
1323 | 1324 | fi
|
1324 | 1325 | unset GRN_PLUGINS_DIR
1325 | 1326 | ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize
|
1326 |
| - |
| 1327 | +
|
1327 | 1328 | 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 |
1329 | 1330 | echo "PostgreSQL is ready"
|
1330 | 1331 | break
|
1331 | 1332 | fi
|
|
1336 | 1337 | fi
|
1337 | 1338 | done
|
1338 | 1339 |
|
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 |
1340 | 1341 | echo "Error executing SQL file"
|
1341 | 1342 | exit 1
|
1342 | 1343 | fi
|
|
1347 | 1348 | --dbname=postgres \
|
1348 | 1349 | --inputdir=${./nix/tests} \
|
1349 | 1350 | --outputdir=$out/regression_output \
|
1350 |
| - --host=localhost \ |
1351 |
| - --port=5435 \ |
| 1351 | + --host=${pgsqlDefaultHost} \ |
| 1352 | + --port=${pgsqlDefaultPort} \ |
1352 | 1353 | --user=supabase_admin \
|
1353 | 1354 | ${builtins.concatStringsSep " " sortedTestList}; then
|
1354 | 1355 | echo "pg_regress tests failed"
|
|
1357 | 1358 | fi
|
1358 | 1359 |
|
1359 | 1360 | 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 |
1361 | 1362 |
|
1362 | 1363 | # Copy logs to output
|
1363 | 1364 | for logfile in $(find /tmp -name postgresql.log -type f); do
|
|
0 commit comments