@@ -15,39 +15,46 @@ CURRENT_SYSTEM="@CURRENT_SYSTEM@"
1515ANSIBLE_VARS=" @ANSIBLE_VARS@"
1616PGBOUNCER_AUTH_SCHEMA_SQL=@PGBOUNCER_AUTH_SCHEMA_SQL@
1717STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
18+
19+ # Start PostgreSQL using nix
20+ start_postgres () {
21+ DATDIR=$( mktemp -d)
22+ echo " Starting PostgreSQL in directory: $DATDIR " # Create the DATDIR if it doesn't exist
23+ nix run " $FLAKE_URL #start-server" -- " $PSQL_VERSION " --skip-migrations --daemonize --datdir " $DATDIR "
24+ echo " PostgreSQL started."
25+ }
26+
1827# Cleanup function
1928cleanup () {
2029 echo " Cleaning up..."
2130
22- # Kill postgres processes first
31+ # Check if PostgreSQL processes exist
2332 if pgrep -f " postgres" > /dev/null; then
24- pkill -TERM postgres || true
25- sleep 2
26- fi
27-
28- # Then kill overmind
29- if [ -S " ./.overmind.sock" ]; then
30- overmind kill || true
31- sleep 2
33+ echo " Stopping PostgreSQL gracefully..."
34+
35+ # Use pg_ctl to stop PostgreSQL
36+ pg_ctl -D " $DATDIR " stop
37+
38+ # Wait a bit for graceful shutdown
39+ sleep 5
40+
41+ # Check if processes are still running
42+ if pgrep -f " postgres" > /dev/null; then
43+ echo " Warning: Some PostgreSQL processes could not be stopped gracefully."
44+ fi
45+ else
46+ echo " PostgreSQL is not running, skipping stop."
3247 fi
3348
34- # Kill tmux sessions explicitly
35- pkill -f " tmux.*overmind.*postgresql" || true
36- tmux ls 2> /dev/null | grep ' overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true
37-
38- # Force kill any stragglers
39- pkill -9 -f " (postgres|tmux.*overmind.*postgresql)" || true
40-
41- rm -f .overmind.sock Procfile
42-
43- # Final verification
44- if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
45- ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
46- return 1
49+ # Always exit successfully, log any remaining processes
50+ if pgrep -f " postgres" > /dev/null; then
51+ echo " Warning: Some PostgreSQL processes could not be cleaned up:"
52+ pgrep -f " postgres"
53+ else
54+ echo " Cleanup completed successfully"
4755 fi
4856}
4957
50- # Set up trap for cleanup on script exit
5158
5259# Function to display help
5360print_help () {
@@ -57,7 +64,7 @@ print_help() {
5764 echo " -v, --version [15|16|orioledb-17|all] Specify the PostgreSQL version to use (required defaults to --version all)"
5865 echo " -p, --port PORT Specify the port number to use (default: 5435)"
5966 echo " -h, --help Show this help message"
60- echo
67+ echo " -f, --flake-url URL Specify the flake URL to use (default: github:supabase/postgres) "
6168 echo " Description:"
6269 echo " Runs 'dbmate up' against a locally running the version of database you specify. Or 'all' to run against all versions."
6370 echo " NOTE: To create a migration, you must run 'nix develop' and then 'dbmate new <migration_name>' to create a new migration file."
@@ -66,9 +73,9 @@ print_help() {
6673 echo " nix run .#dbmate-tool"
6774 echo " nix run .#dbmate-tool -- --version 15"
6875 echo " nix run .#dbmate-tool -- --version 16 --port 5433"
76+ echo " nix run .#dbmate-tool -- --version 16 --port 5433 --flake-url github:supabase/postgres/<commithash>"
6977}
7078
71-
7279# Parse arguments
7380while [[ " $# " -gt 0 ]]; do
7481 case " $1 " in
@@ -125,7 +132,7 @@ wait_for_postgres() {
125132 local max_attempts=30 # Increased significantly
126133 local attempt=1
127134
128- # Give overmind a moment to actually start the process
135+ # Give PostgreSQL a moment to actually start the process
129136 sleep 2
130137
131138 while [ $attempt -le $max_attempts ]; do
@@ -142,7 +149,6 @@ wait_for_postgres() {
142149 done
143150
144151 echo " PostgreSQL failed to start after $max_attempts attempts"
145- overmind echo postgres
146152 return 1
147153}
148154
@@ -175,26 +181,7 @@ trim_schema() {
175181 ;;
176182 esac
177183}
178- overmind_start () {
179- cat > Procfile << EOF
180- postgres_${PSQL_VERSION} : exec nix run "$FLAKE_URL #start-server" -- "$PSQL_VERSION " --skip-migrations
181- EOF
182- overmind start -D
183- echo " Waiting for overmind socket..."
184- max_wait=5
185- count=0
186- while [ $count -lt $max_wait ]; do
187- if [ -S " ./.overmind.sock" ]; then
188- # Found the socket, give it a moment to be ready
189- sleep 5
190- echo " Socket file found and ready"
191- break
192- fi
193- echo " Waiting for socket file (attempt $count /$max_wait )"
194- sleep 1
195- count=$(( count + 1 ))
196- done
197- }
184+
198185perform_dump () {
199186 local max_attempts=3
200187 local attempt=1
@@ -214,21 +201,18 @@ perform_dump() {
214201 echo " All dump attempts failed"
215202 return 1
216203}
204+
217205migrate_version () {
218206 echo " PSQL_VERSION: $PSQL_VERSION "
219- overmind kill || true
220- rm -f .overmind.sock Procfile || true
207+ # pkill -f "postgres" || true # Ensure PostgreSQL is stopped before starting
221208 PSQLBIN=$( nix build --no-link " $FLAKE_URL #psql_$PSQL_VERSION /bin" --json | jq -r ' .[].outputs.out + "/bin"' )
222209 echo " Using PostgreSQL version $PSQL_VERSION from $PSQLBIN "
223210
224- # Start overmind
225- overmind_start
226- echo " Waiting for overmind socket..."
227-
228-
211+ # Start PostgreSQL
212+ start_postgres
229213 echo " Waiting for PostgreSQL to be ready..."
230214
231- # Wait for PostgreSQL to be ready to accept connections
215+ # Wait for PostgreSQL to be ready to accept connections
232216 if ! wait_for_postgres; then
233217 echo " Failed to connect to PostgreSQL server"
234218 exit 1
@@ -255,11 +239,11 @@ EOSQL
255239 " ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $PGBOUNCER_AUTH_SCHEMA_SQL "
256240 " ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $STAT_EXTENSION_SQL "
257241
258- # set db url to run dbmate
242+ # Set db url to run dbmate
259243 export DATABASE_URL=" postgres://$PGSQL_USER :$PGPASSWORD @localhost:$PORTNO /postgres?sslmode=disable"
260- # export path so dbmate can find correct psql and pg_dump
244+ # Export path so dbmate can find correct psql and pg_dump
261245 export PATH=" $PSQLBIN :$PATH "
262- # run init scripts
246+ # Run init scripts
263247 if ! dbmate --migrations-dir " $MIGRATIONS_DIR /init-scripts" up; then
264248 echo " Error: Initial migration failed"
265249 exit 1
0 commit comments