Skip to content

Commit f0f1248

Browse files
committed
chore: try to have migrations check just use start-server directly
1 parent 20a02cd commit f0f1248

File tree

1 file changed

+40
-63
lines changed

1 file changed

+40
-63
lines changed

nix/tools/dbmate-tool.sh.in

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,36 @@ CURRENT_SYSTEM="@CURRENT_SYSTEM@"
1515
ANSIBLE_VARS="@ANSIBLE_VARS@"
1616
PGBOUNCER_AUTH_SCHEMA_SQL=@PGBOUNCER_AUTH_SCHEMA_SQL@
1717
STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
18+
PG_PID=""
19+
1820
# Cleanup function
1921
cleanup() {
2022
echo "Cleaning up..."
2123

22-
# Kill postgres processes first
23-
if pgrep -f "postgres" >/dev/null; then
24-
pkill -TERM postgres || true
24+
# Kill postgres process using PG_PID if available
25+
if [ -n "$PG_PID" ]; then
26+
echo "Stopping PostgreSQL (PID: $PG_PID)..."
27+
kill -TERM "$PG_PID" || true
2528
sleep 2
2629
fi
27-
28-
# Then kill overmind
29-
if [ -S "./.overmind.sock" ]; then
30-
overmind kill || true
30+
31+
# Kill any remaining postgres processes
32+
if pgrep -f "postgres" >/dev/null; then
33+
pkill -TERM postgres || true
3134
sleep 2
35+
# Force kill any remaining postgres processes
36+
pkill -9 -f "postgres" || true
3237
fi
3338

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-
4339
# 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
40+
if ps aux | grep -f "postgres" | grep -v grep >/dev/null; then
41+
ps aux | grep -f "postgres" | grep -v grep
4642
return 1
4743
fi
4844
}
4945

5046
# Set up trap for cleanup on script exit
47+
trap cleanup EXIT
5148

5249
# Function to display help
5350
print_help() {
@@ -175,62 +172,42 @@ trim_schema() {
175172
;;
176173
esac
177174
}
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-
}
198-
perform_dump() {
199-
local max_attempts=3
200-
local attempt=1
201-
202-
while [ $attempt -le $max_attempts ]; do
203-
echo "Attempting dbmate dump (attempt $attempt/$max_attempts)"
204-
205-
if dbmate dump; then
206-
return 0
207-
fi
208-
209-
echo "Dump attempt $attempt failed, waiting before retry..."
210-
sleep 5
211-
attempt=$((attempt + 1))
212-
done
213-
214-
echo "All dump attempts failed"
215-
return 1
175+
176+
# Start postgres function
177+
start_postgres() {
178+
echo "Starting PostgreSQL server version $PSQL_VERSION..."
179+
nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations &
180+
PG_PID=$!
181+
echo "PostgreSQL started with PID: $PG_PID"
182+
183+
# Give postgres a moment to start
184+
sleep 2
185+
186+
# Check if process is still running
187+
if ! kill -0 "$PG_PID" 2>/dev/null; then
188+
echo "PostgreSQL failed to start"
189+
PG_PID=""
190+
return 1
191+
fi
216192
}
193+
217194
migrate_version() {
218195
echo "PSQL_VERSION: $PSQL_VERSION"
219-
overmind kill || true
220-
rm -f .overmind.sock Procfile || true
221196
PSQLBIN=$(nix build --no-link "$FLAKE_URL#psql_$PSQL_VERSION/bin" --json | jq -r '.[].outputs.out + "/bin"')
222197
echo "Using PostgreSQL version $PSQL_VERSION from $PSQLBIN"
223198

224-
# Start overmind
225-
overmind_start
226-
echo "Waiting for overmind socket..."
227-
199+
# Start PostgreSQL
200+
if ! start_postgres; then
201+
echo "Failed to start PostgreSQL server"
202+
exit 1
203+
fi
228204

229205
echo "Waiting for PostgreSQL to be ready..."
230206

231-
#Wait for PostgreSQL to be ready to accept connections
207+
# Wait for PostgreSQL to be ready to accept connections
232208
if ! wait_for_postgres; then
233209
echo "Failed to connect to PostgreSQL server"
210+
cleanup
234211
exit 1
235212
fi
236213

0 commit comments

Comments
 (0)