44[ ! -z " $DEBUG " ] && set -x
55
66# Default values
7- PSQL_VERSION=" 15 "
7+ PSQL_VERSION=" ALL "
88MIGRATION_FILE=" "
99PORTNO=" @PGSQL_DEFAULT_PORT@"
1010PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
1111PGPASSWORD=" ${PGPASSWORD:- postgres} "
1212PGSQL_USER=" postgres"
1313FLAKE_URL=" github:supabase/postgres"
1414MIGRATIONS_DIR=" @MIGRATIONS_DIR@"
15-
15+ CURRENT_SYSTEM=@CURRENT_SYSTEM@
16+ ANSIBLE_VARS=@ANSIBLE_VARS@
1617# Cleanup function
1718cleanup () {
1819 echo " Cleaning up..."
@@ -43,14 +44,13 @@ cleanup() {
4344}
4445
4546# Set up trap for cleanup on script exit
46- trap cleanup EXIT
4747
4848# Function to display help
4949print_help () {
5050 echo " Usage: nix run .#dbmate-tool -- [options]"
5151 echo
5252 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)"
5454 echo " -p, --port PORT Specify the port number to use (default: 5435)"
5555 echo " -h, --help Show this help message"
5656 echo
@@ -116,8 +116,6 @@ while [[ "$#" -gt 0 ]]; do
116116 esac
117117done
118118
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 "
121119# Function to wait for PostgreSQL to be ready
122120wait_for_postgres () {
123121 local max_attempts=30 # Increased significantly
@@ -144,89 +142,122 @@ wait_for_postgres() {
144142 return 1
145143}
146144
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
149164postgres_${PSQL_VERSION} : exec nix run "$FLAKE_URL #start-server" "$PSQL_VERSION "
150165EOF
151166
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
154169
155- # Start services with Overmind
156- overmind start -D
170+ # Start services with Overmind
171+ overmind start -D
157172
158173
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
173188
174189
175- echo " Waiting for PostgreSQL to be ready..."
190+ echo " Waiting for PostgreSQL to be ready..."
176191
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
182197
183- echo " PostgreSQL server is ready"
198+ echo " PostgreSQL server is ready"
184199
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;
189204EOSQL
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
228232
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
230235
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