@@ -15,52 +15,41 @@ CURRENT_SYSTEM="@CURRENT_SYSTEM@"
15
15
ANSIBLE_VARS=" @ANSIBLE_VARS@"
16
16
PGBOUNCER_AUTH_SCHEMA_SQL=@PGBOUNCER_AUTH_SCHEMA_SQL@
17
17
STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
18
+
19
+ # Start PostgreSQL using nix
20
+ start_postgres () {
21
+ echo " Starting PostgreSQL..."
22
+ nix run " $FLAKE_URL #start-server" -- " $PSQL_VERSION " --skip-migrations --daemonize &
23
+ echo " PostgreSQL started."
24
+ }
25
+
18
26
# Cleanup function
19
27
cleanup () {
20
28
echo " Cleaning up..."
21
29
22
- # First, gracefully stop Overmind if it's running
23
- if [ -S " ./.overmind.sock" ]; then
24
- echo " Stopping Overmind gracefully..."
25
- overmind quit || true
26
- sleep 5 # Give Overmind time to shut down
27
- fi
28
-
29
30
# Stop PostgreSQL processes gracefully
30
31
if pgrep -f " postgres" > /dev/null; then
31
32
echo " Stopping PostgreSQL gracefully..."
32
- pkill -TERM postgres || true
33
+ pkill -f " postgres" || true # Adjust this if you have a specific way to stop the service
33
34
sleep 5 # Wait for PostgreSQL to shut down
34
35
fi
35
36
36
- # Clean up any remaining tmux sessions
37
- if tmux ls 2> /dev/null | grep ' overmind' ; then
38
- echo " Cleaning up tmux sessions..."
39
- tmux ls 2> /dev/null | grep ' overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true
40
- sleep 2
41
- fi
42
-
43
- # Final cleanup: Force kill if necessary (as a last resort)
44
- 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
48
- fi
49
-
50
- # Remove socket and Procfile
51
- rm -f .overmind.sock Procfile
52
-
53
37
# Verify cleanup
54
- 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:"
56
- ps aux | grep -E " ( postgres|overmind|tmux.*postgresql) " | grep -v grep
38
+ if pgrep -f " postgres" > /dev/null; then
39
+ echo " Warning: Some PostgreSQL processes could not be cleaned up:"
40
+ pgrep -f " postgres"
57
41
return 1
58
42
else
59
43
echo " Cleanup completed successfully"
60
44
fi
61
45
}
46
+
62
47
# Set up trap for cleanup on script exit
63
48
trap cleanup EXIT INT TERM
49
+
50
+ # Start PostgreSQL
51
+ start_postgres
52
+
64
53
# Function to display help
65
54
print_help () {
66
55
echo " Usage: nix run .#dbmate-tool -- [options]"
@@ -81,7 +70,6 @@ print_help() {
81
70
echo " nix run .#dbmate-tool -- --version 16 --port 5433 --flake-url github:supabase/postgres/<commithash>"
82
71
}
83
72
84
-
85
73
# Parse arguments
86
74
while [[ " $# " -gt 0 ]]; do
87
75
case " $1 " in
@@ -138,7 +126,7 @@ wait_for_postgres() {
138
126
local max_attempts=30 # Increased significantly
139
127
local attempt=1
140
128
141
- # Give overmind a moment to actually start the process
129
+ # Give PostgreSQL a moment to actually start the process
142
130
sleep 2
143
131
144
132
while [ $attempt -le $max_attempts ]; do
@@ -155,7 +143,6 @@ wait_for_postgres() {
155
143
done
156
144
157
145
echo " PostgreSQL failed to start after $max_attempts attempts"
158
- overmind echo postgres
159
146
return 1
160
147
}
161
148
@@ -188,26 +175,7 @@ trim_schema() {
188
175
;;
189
176
esac
190
177
}
191
- overmind_start () {
192
- cat > Procfile << EOF
193
- postgres_${PSQL_VERSION} : exec nix run "$FLAKE_URL #start-server" -- "$PSQL_VERSION " --skip-migrations
194
- EOF
195
- overmind start -D
196
- echo " Waiting for overmind socket..."
197
- max_wait=5
198
- count=0
199
- while [ $count -lt $max_wait ]; do
200
- if [ -S " ./.overmind.sock" ]; then
201
- # Found the socket, give it a moment to be ready
202
- sleep 5
203
- echo " Socket file found and ready"
204
- break
205
- fi
206
- echo " Waiting for socket file (attempt $count /$max_wait )"
207
- sleep 1
208
- count=$(( count + 1 ))
209
- done
210
- }
178
+
211
179
perform_dump () {
212
180
local max_attempts=3
213
181
local attempt=1
@@ -227,21 +195,18 @@ perform_dump() {
227
195
echo " All dump attempts failed"
228
196
return 1
229
197
}
198
+
230
199
migrate_version () {
231
200
echo " PSQL_VERSION: $PSQL_VERSION "
232
- overmind kill || true
233
- rm -f .overmind.sock Procfile || true
201
+ pkill -f " postgres" || true # Ensure PostgreSQL is stopped before starting
234
202
PSQLBIN=$( nix build --no-link " $FLAKE_URL #psql_$PSQL_VERSION /bin" --json | jq -r ' .[].outputs.out + "/bin"' )
235
203
echo " Using PostgreSQL version $PSQL_VERSION from $PSQLBIN "
236
204
237
- # Start overmind
238
- overmind_start
239
- echo " Waiting for overmind socket..."
240
-
241
-
205
+ # Start PostgreSQL
206
+ start_postgres
242
207
echo " Waiting for PostgreSQL to be ready..."
243
208
244
- # Wait for PostgreSQL to be ready to accept connections
209
+ # Wait for PostgreSQL to be ready to accept connections
245
210
if ! wait_for_postgres; then
246
211
echo " Failed to connect to PostgreSQL server"
247
212
exit 1
@@ -268,11 +233,11 @@ EOSQL
268
233
" ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $PGBOUNCER_AUTH_SCHEMA_SQL "
269
234
" ${PSQLBIN} /psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $STAT_EXTENSION_SQL "
270
235
271
- # set db url to run dbmate
236
+ # Set db url to run dbmate
272
237
export DATABASE_URL=" postgres://$PGSQL_USER :$PGPASSWORD @localhost:$PORTNO /postgres?sslmode=disable"
273
- # export path so dbmate can find correct psql and pg_dump
238
+ # Export path so dbmate can find correct psql and pg_dump
274
239
export PATH=" $PSQLBIN :$PATH "
275
- # run init scripts
240
+ # Run init scripts
276
241
if ! dbmate --migrations-dir " $MIGRATIONS_DIR /init-scripts" up; then
277
242
echo " Error: Initial migration failed"
278
243
exit 1
0 commit comments