@@ -53,71 +53,119 @@ echo "Upgrade method: $UPGRADE_METHOD"
53
53
PORTNO=" ${2:-@ PGSQL_DEFAULT_PORT@ } "
54
54
DATDIR=$( mktemp -d)
55
55
NEWDAT=$( mktemp -d)
56
+ PGUSER=${PGUSER:- postgres}
57
+ POSTGRES_PASSWORD=${POSTGRES_PASSWORD:- postgres}
58
+ PG_PROVE=@PG_PROVE@
59
+ PGUPGRADE_TESTS=@PGUPGRADE_TESTS@
56
60
mkdir -p " $DATDIR " " $NEWDAT "
57
61
58
- echo " NOTE: using temporary directory $DATDIR for PSQL $1 data, which will not be removed"
59
- echo " NOTE: you are free to re-use this data directory at will"
62
+ echo " using temporary directory $DATDIR for PSQL $1 data, which will not be removed"
63
+ echo " you are free to re-use this data directory at will"
60
64
echo
61
65
62
- $OLDVER /bin/initdb -D " $DATDIR " --locale=C
63
- $NEWVER /bin/initdb -D " $NEWDAT " --locale=C
66
+ echo " PGUSER IS $PGUSER "
67
+
68
+ $OLDVER /bin/initdb -U " $PGUSER " -D " $DATDIR " --locale=C
69
+ $NEWVER /bin/initdb -U " $PGUSER " -D " $NEWDAT " --locale=C
64
70
65
71
# NOTE (aseipp): we need to patch postgresql.conf to have the right pgsodium_getkey script
66
72
PSQL_CONF_FILE=@PSQL_CONF_FILE@
67
73
PGSODIUM_GETKEY_SCRIPT=@PGSODIUM_GETKEY@
68
- echo " NOTE: patching postgresql.conf files"
74
+ echo " patching postgresql.conf files"
69
75
for x in " $DATDIR " " $NEWDAT " ; do
70
76
sed \
71
77
" s#@PGSODIUM_GETKEY_SCRIPT@#$PGSODIUM_GETKEY_SCRIPT #g" \
72
78
$PSQL_CONF_FILE > " $x /postgresql.conf"
73
79
done
74
80
75
- echo " NOTE: Starting first server (v${1} ) to load data into the system"
81
+ echo " Starting first server (v${1} ) to load data into the system"
76
82
$OLDVER /bin/pg_ctl start -D " $DATDIR "
77
83
78
84
PRIMING_SCRIPT=@PRIMING_SCRIPT@
79
- MIGRATION_DATA=@MIGRATION_DATA@
85
+ MIGRATIONS_DIR=@MIGRATIONS_DIR@
86
+
87
+ echo " MIGRATIONS_DIR IS $MIGRATIONS_DIR "
80
88
81
- $OLDVER /bin/psql -h localhost -d postgres -Xf " $PRIMING_SCRIPT "
82
- $OLDVER /bin/psql -h localhost -d postgres -Xf " $MIGRATION_DATA "
89
+ # $OLDVER/bin/psql -h localhost -d postgres -Xf "$PRIMING_SCRIPT"
90
+ for sql in " $MIGRATIONS_DIR " /db/init-scripts/* .sql; do
91
+ echo " $0 : running $sql "
92
+ $OLDVER /bin/psql -h localhost -d postgres -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -f " $sql "
93
+ done
94
+
95
+ $OLDVER /bin/psql -h localhost -d postgres -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c " ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD '"
96
+ # run migrations as super user - postgres user demoted in post-setup
97
+ for sql in " $MIGRATIONS_DIR " /db/migrations/* .sql; do
98
+ echo " $0 : running $sql "
99
+ $OLDVER /bin/psql -h localhost -d postgres -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -f " $sql "
100
+ done
83
101
84
102
if [ " $UPGRADE_METHOD " == " pg_upgrade" ]; then
85
- echo " NOTE: Stopping old server (v${1} ) to prepare for migration"
103
+ # echo "Ensuring proper permissions on $DATDIR"
104
+ # chmod 700 "$DATDIR"
105
+
106
+ echo " Stopping old server"
107
+ $OLDVER /bin/pg_ctl stop -D " $DATDIR "
108
+
109
+ echo " Starting old server"
110
+ $OLDVER /bin/pg_ctl start -D " $DATDIR "
111
+
112
+ echo " Ensuring $PGUSER is a superuser in the old database"
113
+ " $OLDVER " /bin/psql -h localhost -U supabase_admin -p 5432 -d postgres -c " ALTER USER $PGUSER WITH SUPERUSER;" || true
114
+
115
+ echo " Running pre-migration checks"
116
+ " $PG_PROVE " /bin/pg_prove --psql=" $OLDVER " /bin/psql -h localhost -U supabase_admin -d postgres -p 5432 " $MIGRATIONS_DIR " /tests/test.sql
117
+
118
+ echo " Running fixtures"
119
+ " $OLDVER " /bin/psql -h localhost -U supabase_admin -p 5432 -d postgres -f " $PGUPGRADE_TESTS /pg_upgrade/tests/97-enable-extensions.sql"
120
+ " $OLDVER " /bin/psql -h localhost -U supabase_admin -p 5432 -d postgres -f " $PGUPGRADE_TESTS /pg_upgrade/tests/98-data-fixtures.sql"
121
+ " $OLDVER " /bin/psql -h localhost -U supabase_admin -p 5432 -d postgres -f " $PGUPGRADE_TESTS /pg_upgrade/tests/99-fixtures.sql"
122
+
123
+ echo " Stopping old server"
86
124
$OLDVER /bin/pg_ctl stop -D " $DATDIR "
87
125
88
- echo " NOTE: Migrating old data $DATDIR to $NEWDAT using pg_upgrade"
126
+ echo " Migrating old data $DATDIR to $NEWDAT using pg_upgrade"
89
127
90
128
export PGDATAOLD=" $DATDIR "
91
129
export PGDATANEW=" $NEWDAT "
92
130
export PGBINOLD=" $OLDVER /bin"
93
131
export PGBINNEW=" $NEWVER /bin"
94
132
95
- if ! $NEWVER /bin/pg_upgrade --check; then
133
+ if ! $NEWVER /bin/pg_upgrade -U " $PGUSER " - -check; then
96
134
echo " ERROR: pg_upgrade check failed"
97
135
exit 1
98
136
fi
99
137
100
- echo " NOTE: pg_upgrade check passed, proceeding with migration"
101
- $NEWVER /bin/pg_upgrade
138
+ echo " pg_upgrade check passed, proceeding with migration"
139
+ $NEWVER /bin/pg_upgrade -U " $PGUSER "
102
140
rm -f delete_old_cluster.sh # we don't need this
141
+ echo " Migration complete, running post-migration checks"
142
+ $NEWVER /bin/pg_ctl start -D " $NEWDAT "
143
+ " $PG_PROVE " /bin/pg_prove --psql=" $NEWVER " /bin/psql -h localhost -U supabase_admin -d postgres -p 5432 \
144
+ " $PGUPGRADE_TESTS /pg_upgrade/tests/01-schema.sql"
145
+ " $PG_PROVE " /bin/pg_prove --psql=" $NEWVER " /bin/psql -h localhost -U supabase_admin -d postgres -p 5432 \
146
+ " $PGUPGRADE_TESTS /pg_upgrade/tests/02-data.sql"
147
+ " $PG_PROVE " /bin/pg_prove --psql=" $NEWVER " /bin/psql -h localhost -U supabase_admin -d postgres -p 5432 \
148
+ " $PGUPGRADE_TESTS /pg_upgrade/tests/03-settings.sql"
149
+ $NEWVER /bin/pg_ctl stop -D " $NEWDAT "
103
150
exit 0
104
151
fi
105
152
106
- if [ " $UPGRADE_METHOD " == " pg_dumpall" ]; then
107
- SQLDAT=" $DATDIR /dump.sql"
108
- echo " NOTE: Exporting data via pg_dumpall ($SQLDAT )"
109
- $NEWVER /bin/pg_dumpall -h localhost > " $SQLDAT "
110
153
111
- echo " NOTE: Stopping old server (v${1} ) to prepare for migration"
112
- $OLDVER /bin/pg_ctl stop -D " $DATDIR "
154
+ # if [ "$UPGRADE_METHOD" == "pg_dumpall" ]; then
155
+ # SQLDAT="$DATDIR/dump.sql"
156
+ # echo "Exporting data via pg_dumpall ($SQLDAT)"
157
+ # $NEWVER/bin/pg_dumpall -h localhost > "$SQLDAT"
113
158
114
- echo " NOTE: Starting second server (v${2 } ) to load data into the system "
115
- $NEWVER /bin/pg_ctl start -D " $NEWDAT "
159
+ # echo "Stopping old server (v${1 }) to prepare for migration "
160
+ # $OLDVER /bin/pg_ctl stop -D "$DATDIR "
116
161
117
- echo " NOTE: Loading data into new server (v${2} ) via 'cat | psql' "
118
- cat " $SQLDAT " | $ NEWVER /bin/psql -h localhost -d postgres
162
+ # echo "Starting second server (v${2}) to load data into the system "
163
+ # $ NEWVER/bin/pg_ctl start -D "$NEWDAT"
119
164
120
- printf " \n\n\n\n"
121
- echo " NOTE: Done, check logs. Stopping the server; new database is located at $NEWDAT "
122
- $NEWVER /bin/pg_ctl stop -D " $NEWDAT "
123
- fi
165
+ # echo "Loading data into new server (v${2}) via 'cat | psql'"
166
+ # cat "$SQLDAT" | $NEWVER/bin/psql -h localhost -d postgres
167
+
168
+ # printf "\n\n\n\n"
169
+ # echo "Done, check logs. Stopping the server; new database is located at $NEWDAT"
170
+ # $NEWVER/bin/pg_ctl stop -D "$NEWDAT"
171
+ # fi
0 commit comments