|
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 { |
|
345 | 346 | PGBOUNCER_AUTH_SCHEMA_SQL = "${paths.pgbouncerAuthSchemaSql}"; |
346 | 347 | STAT_EXTENSION_SQL = "${paths.statExtensionSql}"; |
347 | 348 | CURRENT_SYSTEM = "${system}"; |
348 | | - } // extraSubstitutions; # Merge in any extra substitutions |
| 349 | + } // extraSubstitutions; # Merge in any extra substitutions |
349 | 350 | in |
350 | 351 | pkgs.runCommand name |
351 | 352 | { |
352 | 353 | inherit (paths) migrationsDir postgresqlSchemaSql pgbouncerAuthSchemaSql statExtensionSql; |
353 | 354 | } '' |
354 | 355 | set -x |
355 | 356 | mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql $out/extension-custom-scripts |
356 | | - |
| 357 | +
|
357 | 358 | # Copy config files with error handling |
358 | 359 | cp ${paths.supautilsConfigFile} $out/etc/postgresql-custom/supautils.conf || { echo "Failed to copy supautils.conf"; exit 1; } |
359 | 360 | cp ${paths.pgconfigFile} $out/etc/postgresql/postgresql.conf || { echo "Failed to copy postgresql.conf"; exit 1; } |
|
362 | 363 | cp ${paths.pgHbaConfigFile} $out/etc/postgresql/pg_hba.conf || { echo "Failed to copy pg_hba.conf"; exit 1; } |
363 | 364 | cp ${paths.pgIdentConfigFile} $out/etc/postgresql/pg_ident.conf || { echo "Failed to copy pg_ident.conf"; exit 1; } |
364 | 365 | cp -r ${paths.postgresqlExtensionCustomScriptsPath}/* $out/extension-custom-scripts/ || { echo "Failed to copy custom scripts"; exit 1; } |
365 | | - |
| 366 | +
|
366 | 367 | echo "Copy operation completed" |
367 | 368 | chmod 644 $out/etc/postgresql-custom/supautils.conf |
368 | 369 | chmod 644 $out/etc/postgresql/postgresql.conf |
369 | 370 | chmod 644 $out/etc/postgresql-custom/logging.conf |
370 | 371 | chmod 644 $out/etc/postgresql/pg_hba.conf |
371 | 372 |
|
372 | 373 | 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}'") |
375 | 376 | substitutions |
376 | 377 | ))} |
377 | 378 | chmod +x $out/bin/start-postgres-server |
|
560 | 561 | chmod +x $out/bin/pg-restore |
561 | 562 | ''; |
562 | 563 | sync-exts-versions = pkgs.runCommand "sync-exts-versions" { } '' |
563 | | - mkdir -p $out/bin |
| 564 | + mkdir -p $out/bin |
564 | 565 | substitute ${./nix/tools/sync-exts-versions.sh.in} $out/bin/sync-exts-versions \ |
565 | 566 | --subst-var-by 'YQ' '${pkgs.yq}/bin/yq' \ |
566 | 567 | --subst-var-by 'JQ' '${pkgs.jq}/bin/jq' \ |
|
595 | 596 | makeWrapper |
596 | 597 | ]; |
597 | 598 | } '' |
598 | | - mkdir -p $out/bin $out/migrations |
| 599 | + mkdir -p $out/bin $out/migrations |
599 | 600 | cp -r ${migrationsDir}/* $out |
600 | 601 | substitute ${./nix/tools/dbmate-tool.sh.in} $out/bin/dbmate-tool \ |
601 | 602 | --subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \ |
|
1071 | 1072 | # Wait for workflow to start and get the run ID |
1072 | 1073 | echo "Waiting for workflow to start..." |
1073 | 1074 | sleep 5 |
1074 | | - |
| 1075 | +
|
1075 | 1076 | # Get the latest run ID for this workflow |
1076 | 1077 | RUN_ID=$(gh run list --workflow=nix-build.yml --limit 1 --json databaseId --jq '.[0].databaseId') |
1077 | | - |
| 1078 | +
|
1078 | 1079 | if [ -z "$RUN_ID" ]; then |
1079 | 1080 | echo "Error: Could not find workflow run ID" |
1080 | 1081 | exit 1 |
|
1116 | 1117 | cat > $out/bin/pgsodium-getkey << 'EOF' |
1117 | 1118 | #!${pkgs.bash}/bin/bash |
1118 | 1119 | set -euo pipefail |
1119 | | - |
| 1120 | +
|
1120 | 1121 | TMPDIR_BASE=$(mktemp -d) |
1121 | | - |
| 1122 | +
|
1122 | 1123 | if [[ "$(uname)" == "Darwin" ]]; then |
1123 | 1124 | KEY_DIR="/private/tmp/pgsodium" |
1124 | 1125 | else |
1125 | 1126 | KEY_DIR="''${PGSODIUM_KEY_DIR:-$TMPDIR_BASE/pgsodium}" |
1126 | 1127 | fi |
1127 | 1128 | KEY_FILE="$KEY_DIR/pgsodium.key" |
1128 | | - |
| 1129 | +
|
1129 | 1130 | if ! mkdir -p "$KEY_DIR" 2>/dev/null; then |
1130 | 1131 | echo "Error: Could not create key directory $KEY_DIR" >&2 |
1131 | 1132 | exit 1 |
1132 | 1133 | fi |
1133 | 1134 | chmod 1777 "$KEY_DIR" |
1134 | | - |
| 1135 | +
|
1135 | 1136 | if [[ ! -f "$KEY_FILE" ]]; then |
1136 | 1137 | if ! (dd if=/dev/urandom bs=32 count=1 2>/dev/null | od -A n -t x1 | tr -d ' \n' > "$KEY_FILE"); then |
1137 | 1138 | if ! (openssl rand -hex 32 > "$KEY_FILE"); then |
|
1141 | 1142 | fi |
1142 | 1143 | chmod 644 "$KEY_FILE" |
1143 | 1144 | fi |
1144 | | - |
| 1145 | +
|
1145 | 1146 | if [[ -f "$KEY_FILE" && -r "$KEY_FILE" ]]; then |
1146 | 1147 | cat "$KEY_FILE" |
1147 | 1148 | else |
|
1245 | 1246 | substitute ${./nix/tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \ |
1246 | 1247 | --subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey" |
1247 | 1248 | echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf |
1248 | | - echo "port = 5435" >> "$PGTAP_CLUSTER"/postgresql.conf |
| 1249 | + echo "port = ${pgsqlDefaultPort}" >> "$PGTAP_CLUSTER"/postgresql.conf |
1249 | 1250 | echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf |
1250 | 1251 | echo "Checking shared_preload_libraries setting:" |
1251 | 1252 | grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf |
|
1266 | 1267 |
|
1267 | 1268 | # PostgreSQL startup |
1268 | 1269 | 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 |
1270 | 1271 | else |
1271 | 1272 | 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 |
1273 | 1274 | fi || { |
1274 | | - echo "pg_ctl failed to start PostgreSQL" |
| 1275 | + echo "pg_ctl failed to start PostgreSQL" |
1275 | 1276 | echo "Contents of postgresql.log:" |
1276 | 1277 | cat "$PGTAP_CLUSTER"/postgresql.log |
1277 | 1278 | exit 1 |
1278 | 1279 | } |
1279 | 1280 | for i in {1..60}; do |
1280 | | - if pg_isready -h localhost -p 5435; then |
| 1281 | + if pg_isready -h ${pgsqlDefaultHost} -p ${pgsqlDefaultPort}; then |
1281 | 1282 | echo "PostgreSQL is ready" |
1282 | 1283 | break |
1283 | 1284 | fi |
|
1291 | 1292 | exit 1 |
1292 | 1293 | fi |
1293 | 1294 | 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 |
1296 | 1297 | echo "Error executing SQL file. PostgreSQL log content:" |
1297 | 1298 | cat "$PGTAP_CLUSTER"/postgresql.log |
1298 | 1299 | pg_ctl -D "$PGTAP_CLUSTER" stop |
1299 | 1300 | exit 1 |
1300 | 1301 | fi |
1301 | 1302 | SORTED_DIR=$(mktemp -d) |
1302 | 1303 | 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 |
1304 | 1305 | done |
1305 | 1306 | rm -rf "$SORTED_DIR" |
1306 | 1307 | pg_ctl -D "$PGTAP_CLUSTER" stop |
1307 | 1308 | rm -rf $PGTAP_CLUSTER |
1308 | | - |
| 1309 | +
|
1309 | 1310 | # End of pgtap tests |
1310 | 1311 | # 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 |
1312 | 1313 | # start-postgres-server-bin script closely matches our AMI setup, configurations and migrations |
1313 | 1314 |
|
1314 | 1315 | # Ensure pgsodium key directory exists with proper permissions |
|
1318 | 1319 | fi |
1319 | 1320 | unset GRN_PLUGINS_DIR |
1320 | 1321 | ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize |
1321 | | - |
| 1322 | +
|
1322 | 1323 | 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 |
1324 | 1325 | echo "PostgreSQL is ready" |
1325 | 1326 | break |
1326 | 1327 | fi |
|
1331 | 1332 | fi |
1332 | 1333 | done |
1333 | 1334 |
|
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 |
1335 | 1336 | echo "Error executing SQL file" |
1336 | 1337 | exit 1 |
1337 | 1338 | fi |
|
1342 | 1343 | --dbname=postgres \ |
1343 | 1344 | --inputdir=${./nix/tests} \ |
1344 | 1345 | --outputdir=$out/regression_output \ |
1345 | | - --host=localhost \ |
1346 | | - --port=5435 \ |
| 1346 | + --host=${pgsqlDefaultHost} \ |
| 1347 | + --port=${pgsqlDefaultPort} \ |
1347 | 1348 | --user=supabase_admin \ |
1348 | 1349 | ${builtins.concatStringsSep " " sortedTestList}; then |
1349 | 1350 | echo "pg_regress tests failed" |
|
1352 | 1353 | fi |
1353 | 1354 |
|
1354 | 1355 | 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 |
1356 | 1357 |
|
1357 | 1358 | # Copy logs to output |
1358 | 1359 | for logfile in $(find /tmp -name postgresql.log -type f); do |
|
0 commit comments