4
4
[ ! -z " $DEBUG " ] && set -x
5
5
6
6
# Default values
7
- PSQL_VERSION=" 15 "
7
+ PSQL_VERSION=" ALL "
8
8
MIGRATION_FILE=" "
9
9
PORTNO=" @PGSQL_DEFAULT_PORT@"
10
10
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
11
11
PGPASSWORD=" ${PGPASSWORD:- postgres} "
12
12
PGSQL_USER=" postgres"
13
13
FLAKE_URL=" github:supabase/postgres"
14
14
MIGRATIONS_DIR=" @MIGRATIONS_DIR@"
15
-
15
+ CURRENT_SYSTEM=@CURRENT_SYSTEM@
16
+ ANSIBLE_VARS=@ANSIBLE_VARS@
16
17
# Cleanup function
17
18
cleanup () {
18
19
echo " Cleaning up..."
@@ -43,14 +44,13 @@ cleanup() {
43
44
}
44
45
45
46
# Set up trap for cleanup on script exit
46
- trap cleanup EXIT
47
47
48
48
# Function to display help
49
49
print_help () {
50
50
echo " Usage: nix run .#dbmate-tool -- [options]"
51
51
echo
52
52
echo " Options:"
53
- echo " -v, --version [15|16|orioledb-17] Specify the PostgreSQL version to use (required defaults to --all)"
53
+ echo " -v, --version [15|16|orioledb-17|all ] Specify the PostgreSQL version to use (required defaults to --version all)"
54
54
echo " -p, --port PORT Specify the port number to use (default: 5435)"
55
55
echo " -h, --help Show this help message"
56
56
echo
@@ -116,8 +116,6 @@ while [[ "$#" -gt 0 ]]; do
116
116
esac
117
117
done
118
118
119
- PSQLBIN=$( nix build --no-link " $FLAKE_URL #psql_$PSQL_VERSION /bin" --json | jq -r ' .[].outputs.out + "/bin"' )
120
- echo " Using PostgreSQL version $PSQL_VERSION from $PSQLBIN "
121
119
# Function to wait for PostgreSQL to be ready
122
120
wait_for_postgres () {
123
121
local max_attempts=30 # Increased significantly
@@ -144,89 +142,122 @@ wait_for_postgres() {
144
142
return 1
145
143
}
146
144
147
- # Create Procfile
148
- cat > Procfile << EOF
145
+ trim_schema () {
146
+ case " $CURRENT_SYSTEM " in
147
+ " x86_64-darwin" |" aarch64-darwin" )
148
+ sed -i ' ' ' /INSERT INTO public.schema_migrations/,$d' " ./db/schema.sql"
149
+ echo " Matched: $CURRENT_SYSTEM "
150
+ ;;
151
+ * )
152
+ sed -i ' /INSERT INTO public.schema_migrations/,$d' " ./db/schema.sql"
153
+ ;;
154
+ esac
155
+ }
156
+ migrate_version () {
157
+ overmind kill || true
158
+ rm -f .overmind.sock Procfile || true
159
+ PSQLBIN=$( nix build --no-link " $FLAKE_URL #psql_$PSQL_VERSION /bin" --json | jq -r ' .[].outputs.out + "/bin"' )
160
+ echo " Using PostgreSQL version $PSQL_VERSION from $PSQLBIN "
161
+
162
+ # Create Procfile
163
+ cat > Procfile << EOF
149
164
postgres_${PSQL_VERSION} : exec nix run "$FLAKE_URL #start-server" "$PSQL_VERSION "
150
165
EOF
151
166
152
- echo " Starting PostgreSQL server with: exec nix run $FLAKE_URL #start-server $PSQL_VERSION "
153
- cat Procfile
167
+ echo " Starting PostgreSQL server with: exec nix run $FLAKE_URL #start-server $PSQL_VERSION "
168
+ cat Procfile
154
169
155
- # Start services with Overmind
156
- overmind start -D
170
+ # Start services with Overmind
171
+ overmind start -D
157
172
158
173
159
- echo " Waiting for overmind socket..."
160
- max_wait=5
161
- count=0
162
- while [ $count -lt $max_wait ]; do
163
- if [ -S " ./.overmind.sock" ]; then
164
- # Found the socket, give it a moment to be ready
165
- sleep 2
166
- echo " Socket file found and ready"
167
- break
168
- fi
169
- echo " Waiting for socket file (attempt $count /$max_wait )"
170
- sleep 1
171
- count=$(( count + 1 ))
172
- done
174
+ echo " Waiting for overmind socket..."
175
+ max_wait=5
176
+ count=0
177
+ while [ $count -lt $max_wait ]; do
178
+ if [ -S " ./.overmind.sock" ]; then
179
+ # Found the socket, give it a moment to be ready
180
+ sleep 2
181
+ echo " Socket file found and ready"
182
+ break
183
+ fi
184
+ echo " Waiting for socket file (attempt $count /$max_wait )"
185
+ sleep 1
186
+ count=$(( count + 1 ))
187
+ done
173
188
174
189
175
- echo " Waiting for PostgreSQL to be ready..."
190
+ echo " Waiting for PostgreSQL to be ready..."
176
191
177
- # Wait for PostgreSQL to be ready to accept connections
178
- if ! wait_for_postgres; then
179
- echo " Failed to connect to PostgreSQL server"
180
- exit 1
181
- fi
192
+ # Wait for PostgreSQL to be ready to accept connections
193
+ if ! wait_for_postgres; then
194
+ echo " Failed to connect to PostgreSQL server"
195
+ exit 1
196
+ fi
182
197
183
- echo " PostgreSQL server is ready"
198
+ echo " PostgreSQL server is ready"
184
199
185
- # Configure PostgreSQL roles and permissions
186
- if ! " ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U " $PGSQL_SUPERUSER " -p " $PORTNO " -h localhost -d postgres << -EOSQL
187
- create role postgres superuser login password '$PGPASSWORD ';
188
- alter database postgres owner to postgres;
200
+ # Configure PostgreSQL roles and permissions
201
+ if ! " ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U " $PGSQL_SUPERUSER " -p " $PORTNO " -h localhost -d postgres << -EOSQL
202
+ create role postgres superuser login password '$PGPASSWORD ';
203
+ alter database postgres owner to postgres;
189
204
EOSQL
190
- then
191
- echo " Failed to configure PostgreSQL roles and permissions"
192
- exit 1
193
- fi
194
- # set db url to run dbmate
195
- export DATABASE_URL=" postgres://$PGSQL_USER :$PGPASSWORD @localhost:$PORTNO /postgres?sslmode=disable"
196
-
197
- ls -la " $MIGRATIONS_DIR "
198
- pwd
199
- # First dbmate command
200
- if ! dbmate --migrations-dir " $MIGRATIONS_DIR /init-scripts" up; then
201
- echo " Error: Initial migration failed"
202
- exit 1
203
- fi
204
-
205
- # Password update command
206
- if ! " ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -c " ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD '" ; then
207
- echo " Error: Failed to update supabase_admin password"
208
- exit 1
209
- fi
210
-
211
- # Set up database URL
212
- export DATABASE_URL=" postgres://$PGSQL_SUPERUSER :$PGPASSWORD @localhost:$PORTNO /postgres?sslmode=disable"
213
-
214
- # Second dbmate command
215
- if ! dbmate --migrations-dir " $MIGRATIONS_DIR /migrations" up; then
216
- echo " Error: Final migration failed"
217
- exit 1
218
- fi
219
-
220
- if [ -f " ./db/schema.sql" ]; then
221
- mv " ./db/schema.sql" " ./db/schema-$PSQL_VERSION .sql"
222
- rm -rf ./db
223
- echo " Schema file moved to ./migrations/schema-$PSQL_VERSION .sql"
224
- else
225
- echo " Warning: schema.sql file not found in ./db directory"
226
- exit 1
227
- fi
205
+ then
206
+ echo " Failed to configure PostgreSQL roles and permissions"
207
+ exit 1
208
+ fi
209
+ # set db url to run dbmate
210
+ export DATABASE_URL=" postgres://$PGSQL_USER :$PGPASSWORD @localhost:$PORTNO /postgres?sslmode=disable"
211
+ # export path so dbmate can find correct psql and pg_dump
212
+ export PATH=" $PSQLBIN :$PATH "
213
+ # run init scripts
214
+ if ! dbmate --migrations-dir " $MIGRATIONS_DIR /init-scripts" up; then
215
+ echo " Error: Initial migration failed"
216
+ exit 1
217
+ fi
218
+
219
+ # Password update command
220
+ if ! " ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -c " ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD '" ; then
221
+ echo " Error: Failed to update supabase_admin password"
222
+ exit 1
223
+ fi
224
+
225
+ # Set up database URL
226
+ export DATABASE_URL=" postgres://$PGSQL_SUPERUSER :$PGPASSWORD @localhost:$PORTNO /postgres?sslmode=disable"
227
+ # Run migrations
228
+ if ! dbmate --migrations-dir " $MIGRATIONS_DIR /migrations" up; then
229
+ echo " Error: Final migration failed"
230
+ exit 1
231
+ fi
228
232
229
- " ${PSQLBIN} /pg_dump" -U " $PGSQL_SUPERUSER " -p " $PORTNO " -h localhost -d postgres -s -f " ./pg-dump-schema-$PSQL_VERSION .sql"
233
+ echo " Running dbmate dump with $PSQLBIN "
234
+ dbmate dump
230
235
231
- # If we get here, all commands succeeded
232
- echo " PostgreSQL migration completed successfully"
236
+ echo " CURRENT_SYSTEM: $CURRENT_SYSTEM "
237
+ if [ -f " ./db/schema.sql" ]; then
238
+ trim_schema
239
+ cp " ./db/schema.sql" " ./migrations/schema-$PSQL_VERSION .sql"
240
+ echo " Schema file moved to ./migrations/schema-$PSQL_VERSION .sql"
241
+ echo " PSQLBIN is $PSQLBIN "
242
+ else
243
+ echo " Warning: schema.sql file not found in ./db directory"
244
+ exit 1
245
+ fi
246
+
247
+ # If we get here, all commands succeeded
248
+ echo " PostgreSQL migration completed successfully"
249
+ }
250
+
251
+ if [ " $PSQL_VERSION " == " all" ]; then
252
+ VERSIONS=$( yq ' .postgres_major[]' " $ANSIBLE_VARS " | tr -d ' "' )
253
+ echo " $VERSIONS " | while read -r version; do
254
+ PSQL_VERSION=" $version "
255
+ echo " Migrating to PostgreSQL version $PSQL_VERSION "
256
+ migrate_version
257
+ cleanup
258
+ done
259
+ else
260
+ echo " Migrating to PostgreSQL version $PSQL_VERSION "
261
+ migrate_version
262
+ cleanup
263
+ fi
0 commit comments