diff --git a/nix/tools/dbmate-tool.sh.in b/nix/tools/dbmate-tool.sh.in index 621912bec..1197228af 100644 --- a/nix/tools/dbmate-tool.sh.in +++ b/nix/tools/dbmate-tool.sh.in @@ -18,8 +18,9 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@ # Start PostgreSQL using nix start_postgres() { - echo "Starting PostgreSQL..." - nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations --daemonize + DATDIR=$(mktemp -d) + echo "Starting PostgreSQL in directory: $DATDIR" # Create the DATDIR if it doesn't exist + nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations --daemonize --datdir "$DATDIR" echo "PostgreSQL started." } @@ -31,16 +32,15 @@ cleanup() { if pgrep -f "postgres" >/dev/null; then echo "Stopping PostgreSQL gracefully..." - # Use a more specific signal handling approach - pkill -15 -f "postgres" # Send SIGTERM + # Use pg_ctl to stop PostgreSQL + pg_ctl -D "$DATDIR" stop # Wait a bit for graceful shutdown sleep 5 - # If processes are still running, force kill + # Check if processes are still running if pgrep -f "postgres" >/dev/null; then - echo "Forcing PostgreSQL processes to stop..." - pkill -9 -f "postgres" # Send SIGKILL if SIGTERM didn't work + echo "Warning: Some PostgreSQL processes could not be stopped gracefully." fi else echo "PostgreSQL is not running, skipping stop." @@ -53,9 +53,6 @@ cleanup() { else echo "Cleanup completed successfully" fi - - # Explicitly exit with 0 to prevent workflow failure - exit 0 } diff --git a/nix/tools/run-server.sh.in b/nix/tools/run-server.sh.in index 22b1fb557..0586e010b 100644 --- a/nix/tools/run-server.sh.in +++ b/nix/tools/run-server.sh.in @@ -65,7 +65,6 @@ stop_postgres() { trap 'stop_postgres' SIGINT SIGTERM -# Parse arguments # Parse arguments while [[ "$#" -gt 0 ]]; do case "$1" in @@ -108,6 +107,15 @@ while [[ "$#" -gt 0 ]]; do print_help exit 0 ;; + --datdir) + if [[ -n "$2" && ! "$2" =~ ^- ]]; then + DATDIR="$2" + shift 2 + else + echo "Error: --datadir requires a directory path" + exit 1 + fi + ;; *) if [[ "$1" =~ ^- ]]; then echo "Unknown option: $1" @@ -165,7 +173,9 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@ MECAB_LIB=@MECAB_LIB@ # Setup directories and locale settings -DATDIR=$(mktemp -d) +if [[ -z "$DATDIR" ]]; then + DATDIR=$(mktemp -d) +fi LOCALE_ARCHIVE=@LOCALES@ CURRENT_SYSTEM=@CURRENT_SYSTEM@