@@ -18,41 +18,106 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
1818# Cleanup function
1919cleanup () {
2020 echo " Cleaning up..."
21-
22- # First, gracefully stop Overmind if it's running
21+
22+ # 1. Gracefully stop Overmind if it's running
2323 if [ -S " ./.overmind.sock" ]; then
2424 echo " Stopping Overmind gracefully..."
25- overmind quit || true
26- sleep 5 # Give Overmind time to shut down
25+ if overmind quit; then
26+ echo " Overmind stopped successfully"
27+ else
28+ echo " Warning: Overmind quit command failed"
29+ fi
30+
31+ # Wait for Overmind to fully shut down
32+ max_wait=10
33+ count=0
34+ while [ -S " ./.overmind.sock" ] && [ $count -lt $max_wait ]; do
35+ echo " Waiting for Overmind socket to close (attempt $count /$max_wait )..."
36+ sleep 1
37+ count=$(( count + 1 ))
38+ done
39+
40+ if [ -S " ./.overmind.sock" ]; then
41+ echo " Warning: Overmind socket still exists after waiting"
42+ else
43+ echo " Overmind socket closed successfully"
44+ fi
2745 fi
2846
29- # Stop PostgreSQL processes gracefully
47+ # 2. Gracefully stop PostgreSQL using pg_ctl if possible
3048 if pgrep -f " postgres" > /dev/null; then
31- echo " Stopping PostgreSQL gracefully..."
32- pkill -TERM postgres || true
33- sleep 5 # Wait for PostgreSQL to shut down
49+ echo " Attempting to stop PostgreSQL gracefully..."
50+
51+ # Try to find the PostgreSQL data directory
52+ PGDATA=$( ps aux | grep postgres | grep -v grep | grep -oP ' (?<= -D ).*?(?=\s)' )
53+
54+ if [ -n " $PGDATA " ] && [ -d " $PGDATA " ]; then
55+ # Use pg_ctl for graceful shutdown if available
56+ if command -v " ${PSQLBIN} /pg_ctl" > /dev/null; then
57+ echo " Using pg_ctl to stop PostgreSQL..."
58+ " ${PSQLBIN} /pg_ctl" -D " $PGDATA " stop -m smart
59+
60+ # Wait for PostgreSQL to shut down
61+ max_wait=30
62+ count=0
63+ while pgrep -f " postgres" > /dev/null && [ $count -lt $max_wait ]; do
64+ echo " Waiting for PostgreSQL to stop (attempt $count /$max_wait )..."
65+ sleep 1
66+ count=$(( count + 1 ))
67+ done
68+
69+ if pgrep -f " postgres" > /dev/null; then
70+ echo " Warning: PostgreSQL did not shut down gracefully, attempting fast shutdown..."
71+ " ${PSQLBIN} /pg_ctl" -D " $PGDATA " stop -m fast
72+ sleep 5
73+ fi
74+ else
75+ echo " Warning: pg_ctl not found, falling back to signal-based shutdown"
76+ pkill -TERM postgres || true
77+ sleep 5
78+ fi
79+ else
80+ echo " Warning: Could not determine PostgreSQL data directory, using signal-based shutdown"
81+ pkill -TERM postgres || true
82+ sleep 5
83+ fi
3484 fi
3585
36- # Clean up any remaining tmux sessions
86+ # 3. Clean up tmux sessions gracefully
3787 if tmux ls 2> /dev/null | grep ' overmind' ; then
3888 echo " Cleaning up tmux sessions..."
39- tmux ls 2> /dev/null | grep ' overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true
89+ tmux ls 2> /dev/null | grep ' overmind' | cut -d: -f1 | while IFS= read -r session; do
90+ echo " Stopping tmux session: $session "
91+ tmux send-keys -t " $session " C-c # Send Ctrl+C to gracefully stop processes
92+ sleep 2
93+ tmux kill-session -t " $session " || true
94+ done
4095 sleep 2
4196 fi
4297
43- # Final cleanup: Force kill if necessary (as a last resort)
98+ # 4. Final verification and cleanup
4499 if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
45- echo " Force killing remaining processes..."
46- pkill -9 -f " (postgres|overmind|tmux.*postgresql)" || true
47- sleep 2
100+ echo " Warning: Some processes are still running after graceful shutdown attempts:"
101+ ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
102+
103+ # Last resort: Use SIGINT before SIGKILL
104+ echo " Sending SIGINT to remaining processes..."
105+ pkill -INT -f " (postgres|overmind|tmux.*postgresql)" || true
106+ sleep 5
107+
108+ # If processes still remain, use SIGKILL as a final measure
109+ if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
110+ echo " Force killing remaining processes..."
111+ pkill -KILL -f " (postgres|overmind|tmux.*postgresql)" || true
112+ fi
48113 fi
49114
50115 # Remove socket and Procfile
51116 rm -f .overmind.sock Procfile
52117
53- # Verify cleanup
118+ # Final verification
54119 if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
55- echo " Warning: Some processes could not be cleaned up :"
120+ echo " Error: Cleanup incomplete. Remaining processes :"
56121 ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
57122 return 1
58123 else
@@ -70,7 +135,7 @@ print_help() {
70135 echo " -v, --version [15|16|orioledb-17|all] Specify the PostgreSQL version to use (required defaults to --version all)"
71136 echo " -p, --port PORT Specify the port number to use (default: 5435)"
72137 echo " -h, --help Show this help message"
73- echo
138+ echo " -f, --flake-url URL Specify the flake URL to use (default: github:supabase/postgres) "
74139 echo " Description:"
75140 echo " Runs 'dbmate up' against a locally running the version of database you specify. Or 'all' to run against all versions."
76141 echo " NOTE: To create a migration, you must run 'nix develop' and then 'dbmate new <migration_name>' to create a new migration file."
@@ -79,6 +144,7 @@ print_help() {
79144 echo " nix run .#dbmate-tool"
80145 echo " nix run .#dbmate-tool -- --version 15"
81146 echo " nix run .#dbmate-tool -- --version 16 --port 5433"
147+ echo " nix run .#dbmate-tool -- --version 16 --port 5433 --flake-url github:supabase/postgres/<commithash>"
82148}
83149
84150
0 commit comments