Skip to content

Commit b6af6d6

Browse files
committed
feat: working flake url args on linux
1 parent d9bb1cc commit b6af6d6

File tree

2 files changed

+57
-74
lines changed

2 files changed

+57
-74
lines changed

flake.nix

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -333,31 +333,28 @@
333333
'';
334334

335335
# Migrate between two data directories.
336-
migrate-tool =
337-
let
338-
configFile = ./nix/tests/postgresql.conf.in;
339-
getkeyScript = ./nix/tests/util/pgsodium_getkey.sh;
340-
primingScript = ./nix/tests/prime.sql;
341-
migrationsDir = ./migrations;
342-
pgupgradeTests = ./tests;
343-
pgProve = pg_prove;
344-
in
345-
pkgs.runCommand "migrate-postgres" { } ''
346-
mkdir -p $out/bin $out/migrations $out/tests
347-
cp -r ${migrationsDir}/* $out/migrations
348-
cp -r ${pgupgradeTests}/* $out/tests
349-
350-
substitute ${./nix/tools/migrate-tool.sh.in} $out/bin/migrate-postgres \
351-
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
352-
--subst-var-by 'PSQL_CONF_FILE' '${configFile}' \
353-
--subst-var-by 'PGSODIUM_GETKEY' '${getkeyScript}' \
354-
--subst-var-by 'PRIMING_SCRIPT' '${primingScript}' \
355-
--subst-var-by 'MIGRATIONS_DIR' "$out/migrations" \
356-
--subst-var-by 'PGUPGRADE_TESTS' "$out/tests" \
357-
--subst-var-by 'PG_PROVE' "${pgProve}"
358-
chmod +x $out/bin/migrate-postgres
359-
'';
360-
336+
migrate-tool =
337+
let
338+
configFile = ./nix/tests/postgresql.conf.in;
339+
getkeyScript = ./nix/tests/util/pgsodium_getkey.sh;
340+
primingScript = ./nix/tests/prime.sql;
341+
migrationsDir = ./migrations;
342+
pgupgradeTests = ./tests;
343+
pgProve = pg_prove;
344+
in
345+
pkgs.runCommand "migrate-postgres" { } ''
346+
mkdir -p $out/bin $out/migrations $out/tests
347+
cp -r ${migrationsDir}/* $out/migrations
348+
cp -r ${pgupgradeTests}/* $out/tests
349+
substitute ${./nix/tools/migrate-tool.sh.in} $out/bin/migrate-postgres \
350+
--subst-var-by 'PSQL_CONF_FILE' '${configFile}' \
351+
--subst-var-by 'PGSODIUM_GETKEY' '${getkeyScript}' \
352+
--subst-var-by 'PRIMING_SCRIPT' '${primingScript}' \
353+
--subst-var-by 'MIGRATIONS_DIR' "$out/migrations" \
354+
--subst-var-by 'PGUPGRADE_TESTS' "$out/tests" \
355+
--subst-var-by 'PG_PROVE' "${pgProve}"
356+
chmod +x $out/bin/migrate-postgres
357+
'';
361358
start-replica = pkgs.runCommand "start-postgres-replica" { } ''
362359
mkdir -p $out/bin
363360
substitute ${./nix/tools/run-replica.sh.in} $out/bin/start-postgres-replica \

nix/tools/migrate-tool.sh.in

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,35 @@
22

33
[ ! -z "$DEBUG" ] && set -x
44

5-
# first argument is the old version; a path 15 or 16
6-
if [[ $1 == /nix/store* ]]; then
7-
if [ ! -L "$1/receipt.json" ] || [ ! -e "$1/receipt.json" ]; then
8-
echo "ERROR: $1 does not look like a valid Postgres install"
5+
# Function to build flake and return the output path
6+
build_flake() {
7+
local flake_url="$1"
8+
local temp_dir=$(mktemp -d)
9+
if ! nix build "$flake_url" -o "$temp_dir/result"; then
10+
echo "ERROR: Failed to build flake $flake_url"
911
exit 1
1012
fi
11-
OLDVER="$1"
12-
elif [ "$1" == "15" ]; then
13-
PSQL15=@PSQL15_BINDIR@
14-
OLDVER="$PSQL15"
15-
elif [ "$1" == "16" ]; then
16-
PSQL16=@PSQL16_BINDIR@
17-
OLDVER="$PSQL16"
18-
else
19-
echo "Please provide a valid Postgres version (15 or 16), or a /nix/store path"
20-
exit 1
21-
fi
13+
echo "$temp_dir/result"
14+
}
2215

23-
# second argument is the new version; 15 or 16
24-
if [[ $2 == /nix/store* ]]; then
25-
if [ ! -L "$2/receipt.json" ] || [ ! -e "$2/receipt.json" ]; then
26-
echo "ERROR: $1 does not look like a valid Postgres install"
27-
exit 1
28-
fi
29-
NEWVER="$2"
30-
elif [ "$2" == "15" ]; then
31-
PSQL15=@PSQL15_BINDIR@
32-
NEWVER="$PSQL15"
33-
elif [ "$2" == "16" ]; then
34-
PSQL16=@PSQL16_BINDIR@
35-
NEWVER="$PSQL16"
36-
echo "NEWVER IS $NEWVER"
37-
else
38-
echo "Please provide a valid Postgres version (15 or 16), or a /nix/store path"
39-
exit 1
40-
fi
16+
# First argument is the old version flake URL
17+
OLDVER=$(build_flake "$1")
18+
19+
# Second argument is the new version flake URL
20+
NEWVER=$(build_flake "$2")
4121

42-
# thid argument is the upgrade method: either pg_dumpall or pg_ugprade
22+
# Third argument is the upgrade method: either pg_dumpall or pg_upgrade
4323
if [ "$3" != "pg_dumpall" ] && [ "$3" != "pg_upgrade" ]; then
4424
echo "Please provide a valid upgrade method (pg_dumpall or pg_upgrade)"
4525
exit 1
4626
fi
4727
UPGRADE_METHOD="$3"
4828

49-
echo "Old server build: PSQL $1"
50-
echo "New server build: PSQL $2"
29+
echo "Old server build: $OLDVER"
30+
echo "New server build: $NEWVER"
5131
echo "Upgrade method: $UPGRADE_METHOD"
5232

53-
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
33+
PORTNO="@PGSQL_DEFAULT_PORT@"
5434
DATDIR=$(mktemp -d)
5535
NEWDAT=$(mktemp -d)
5636
PGUSER=${PGUSER:-postgres}
@@ -59,7 +39,7 @@ PG_PROVE=@PG_PROVE@
5939
PGUPGRADE_TESTS=@PGUPGRADE_TESTS@
6040
mkdir -p "$DATDIR" "$NEWDAT"
6141

62-
echo "using temporary directory $DATDIR for PSQL $1 data, which will not be removed"
42+
echo "using temporary directory $DATDIR for old data, which will not be removed"
6343
echo "you are free to re-use this data directory at will"
6444
echo
6545

@@ -78,15 +58,14 @@ for x in "$DATDIR" "$NEWDAT"; do
7858
$PSQL_CONF_FILE > "$x/postgresql.conf"
7959
done
8060

81-
echo "Starting first server (v${1}) to load data into the system"
61+
echo "Starting first server to load data into the system"
8262
$OLDVER/bin/pg_ctl start -D "$DATDIR"
8363

8464
PRIMING_SCRIPT=@PRIMING_SCRIPT@
8565
MIGRATIONS_DIR=@MIGRATIONS_DIR@
8666

8767
echo "MIGRATIONS_DIR IS $MIGRATIONS_DIR"
8868

89-
# $OLDVER/bin/psql -h localhost -d postgres -Xf "$PRIMING_SCRIPT"
9069
for sql in "$MIGRATIONS_DIR"/db/init-scripts/*.sql; do
9170
echo "$0: running $sql"
9271
$OLDVER/bin/psql -h localhost -d postgres -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -f "$sql"
@@ -100,9 +79,6 @@ for sql in "$MIGRATIONS_DIR"/db/migrations/*.sql; do
10079
done
10180

10281
if [ "$UPGRADE_METHOD" == "pg_upgrade" ]; then
103-
# echo "Ensuring proper permissions on $DATDIR"
104-
# chmod 700 "$DATDIR"
105-
10682
echo "Stopping old server"
10783
$OLDVER/bin/pg_ctl stop -D "$DATDIR"
10884

@@ -130,23 +106,34 @@ if [ "$UPGRADE_METHOD" == "pg_upgrade" ]; then
130106
export PGBINOLD="$OLDVER/bin"
131107
export PGBINNEW="$NEWVER/bin"
132108

109+
# Create a temporary directory for pg_upgrade to work in
110+
UPGRADE_WORKDIR=$(mktemp -d)
111+
echo "Using temporary directory for pg_upgrade: $UPGRADE_WORKDIR"
112+
113+
# Change to the temporary directory before running pg_upgrade
114+
pushd "$UPGRADE_WORKDIR"
115+
133116
if ! $NEWVER/bin/pg_upgrade -U "$PGUSER" --check; then
134117
echo "ERROR: pg_upgrade check failed"
118+
popd
135119
exit 1
136120
fi
137121

138122
echo "pg_upgrade check passed, proceeding with migration"
139123
$NEWVER/bin/pg_upgrade -U "$PGUSER"
140-
rm -f delete_old_cluster.sh # we don't need this
124+
125+
# Change back to the original directory
126+
popd
127+
echo "Migration complete, running post-migration checks"
141128
echo "Migration complete, running post-migration checks"
129+
echo "NEWDAT IS $NEWDAT"
142130
$NEWVER/bin/pg_ctl start -D "$NEWDAT"
143-
echo "TURN off jit"
144-
POST_UPGRADE_EXTENSION_SCRIPT=$(mktemp)
145-
cat << EOF >> "$POST_UPGRADE_EXTENSION_SCRIPT"
131+
echo "Turning off JIT"
132+
cat << EOF > "$NEWDAT"/jit_off.sql
146133
ALTER SYSTEM SET jit = off;
147134
SELECT pg_reload_conf();
148135
EOF
149-
"$NEWVER"/bin/psql -h localhost -U supabase_admin -p 5432 -d postgres -f "$POST_UPGRADE_EXTENSION_SCRIPT"
136+
"$NEWVER"/bin/psql -h localhost -U supabase_admin -p 5432 -d postgres -f "$NEWDAT"/jit_off.sql
150137

151138
"$PG_PROVE"/bin/pg_prove --psql="$NEWVER"/bin/psql -h localhost -U supabase_admin -d postgres -p 5432 \
152139
"$PGUPGRADE_TESTS/pg_upgrade/tests/01-schema.sql"
@@ -158,7 +145,6 @@ EOF
158145
exit 0
159146
fi
160147

161-
162148
# if [ "$UPGRADE_METHOD" == "pg_dumpall" ]; then
163149
# SQLDAT="$DATDIR/dump.sql"
164150
# echo "Exporting data via pg_dumpall ($SQLDAT)"

0 commit comments

Comments
 (0)