Skip to content

Commit 2e4b0ae

Browse files
samrosesoedirgo
authored andcommitted
chore: allow other scripts to stop the start-server cluster
1 parent 1a45609 commit 2e4b0ae

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

nix/tools/dbmate-tool.sh.in

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
1818

1919
# Start PostgreSQL using nix
2020
start_postgres() {
21-
echo "Starting PostgreSQL..."
22-
nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations --daemonize
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"
2324
echo "PostgreSQL started."
2425
}
2526

@@ -31,16 +32,15 @@ cleanup() {
3132
if pgrep -f "postgres" >/dev/null; then
3233
echo "Stopping PostgreSQL gracefully..."
3334

34-
# Use a more specific signal handling approach
35-
pkill -15 -f "postgres" # Send SIGTERM
35+
# Use pg_ctl to stop PostgreSQL
36+
pg_ctl -D "$DATDIR" stop
3637

3738
# Wait a bit for graceful shutdown
3839
sleep 5
3940

40-
# If processes are still running, force kill
41+
# Check if processes are still running
4142
if pgrep -f "postgres" >/dev/null; then
42-
echo "Forcing PostgreSQL processes to stop..."
43-
pkill -9 -f "postgres" # Send SIGKILL if SIGTERM didn't work
43+
echo "Warning: Some PostgreSQL processes could not be stopped gracefully."
4444
fi
4545
else
4646
echo "PostgreSQL is not running, skipping stop."
@@ -53,9 +53,6 @@ cleanup() {
5353
else
5454
echo "Cleanup completed successfully"
5555
fi
56-
57-
# Explicitly exit with 0 to prevent workflow failure
58-
exit 0
5956
}
6057

6158

nix/tools/run-server.sh.in

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ stop_postgres() {
6565

6666
trap 'stop_postgres' SIGINT SIGTERM
6767

68-
# Parse arguments
6968
# Parse arguments
7069
while [[ "$#" -gt 0 ]]; do
7170
case "$1" in
@@ -108,6 +107,15 @@ while [[ "$#" -gt 0 ]]; do
108107
print_help
109108
exit 0
110109
;;
110+
--datdir)
111+
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
112+
DATDIR="$2"
113+
shift 2
114+
else
115+
echo "Error: --datadir requires a directory path"
116+
exit 1
117+
fi
118+
;;
111119
*)
112120
if [[ "$1" =~ ^- ]]; then
113121
echo "Unknown option: $1"
@@ -165,7 +173,9 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
165173
MECAB_LIB=@MECAB_LIB@
166174

167175
# Setup directories and locale settings
168-
DATDIR=$(mktemp -d)
176+
if [[ -z "$DATDIR" ]]; then
177+
DATDIR=$(mktemp -d)
178+
fi
169179
LOCALE_ARCHIVE=@LOCALES@
170180
CURRENT_SYSTEM=@CURRENT_SYSTEM@
171181

0 commit comments

Comments
 (0)