Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: restore flake-url arg #1450

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
echo "EOF" >> $GITHUB_OUTPUT
- name: verify schema.sql is committed
run: |
nix run github:supabase/postgres/${{ github.sha }}#dbmate-tool -- --version ${{ env.PGMAJOR }}
nix run github:supabase/postgres/${{ github.sha }}#dbmate-tool -- --version ${{ env.PGMAJOR }} --flake-url github:supabase/postgres/${{ github.sha }}
if ! git diff --exit-code --quiet migrations/schema-${{ env.PGMAJOR }}.sql; then
echo "Detected changes in schema.sql:"
git diff migrations/schema-${{ env.PGMAJOR }}.sql
Expand Down
100 changes: 42 additions & 58 deletions nix/tools/dbmate-tool.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,46 @@ CURRENT_SYSTEM="@CURRENT_SYSTEM@"
ANSIBLE_VARS="@ANSIBLE_VARS@"
PGBOUNCER_AUTH_SCHEMA_SQL=@PGBOUNCER_AUTH_SCHEMA_SQL@
STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@

# Start PostgreSQL using nix
start_postgres() {
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."
}

# Cleanup function
cleanup() {
echo "Cleaning up..."

# Kill postgres processes first
# Check if PostgreSQL processes exist
if pgrep -f "postgres" >/dev/null; then
pkill -TERM postgres || true
sleep 2
fi

# Then kill overmind
if [ -S "./.overmind.sock" ]; then
overmind kill || true
sleep 2
echo "Stopping PostgreSQL gracefully..."

# Use pg_ctl to stop PostgreSQL
pg_ctl -D "$DATDIR" stop

# Wait a bit for graceful shutdown
sleep 5

# Check if processes are still running
if pgrep -f "postgres" >/dev/null; then
echo "Warning: Some PostgreSQL processes could not be stopped gracefully."
fi
else
echo "PostgreSQL is not running, skipping stop."
fi

# Kill tmux sessions explicitly
pkill -f "tmux.*overmind.*postgresql" || true
tmux ls 2>/dev/null | grep 'overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true

# Force kill any stragglers
pkill -9 -f "(postgres|tmux.*overmind.*postgresql)" || true

rm -f .overmind.sock Procfile

# Final verification
if ps aux | grep -E "(postgres|overmind|tmux.*postgresql)" | grep -v grep >/dev/null; then
ps aux | grep -E "(postgres|overmind|tmux.*postgresql)" | grep -v grep
return 1
# Always exit successfully, log any remaining processes
if pgrep -f "postgres" >/dev/null; then
echo "Warning: Some PostgreSQL processes could not be cleaned up:"
pgrep -f "postgres"
else
echo "Cleanup completed successfully"
fi
}

# Set up trap for cleanup on script exit

# Function to display help
print_help() {
Expand All @@ -57,7 +64,7 @@ print_help() {
echo " -v, --version [15|16|orioledb-17|all] Specify the PostgreSQL version to use (required defaults to --version all)"
echo " -p, --port PORT Specify the port number to use (default: 5435)"
echo " -h, --help Show this help message"
echo
echo " -f, --flake-url URL Specify the flake URL to use (default: github:supabase/postgres)"
echo "Description:"
echo " Runs 'dbmate up' against a locally running the version of database you specify. Or 'all' to run against all versions."
echo " NOTE: To create a migration, you must run 'nix develop' and then 'dbmate new <migration_name>' to create a new migration file."
Expand All @@ -66,9 +73,9 @@ print_help() {
echo " nix run .#dbmate-tool"
echo " nix run .#dbmate-tool -- --version 15"
echo " nix run .#dbmate-tool -- --version 16 --port 5433"
echo " nix run .#dbmate-tool -- --version 16 --port 5433 --flake-url github:supabase/postgres/<commithash>"
}


# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -125,7 +132,7 @@ wait_for_postgres() {
local max_attempts=30 # Increased significantly
local attempt=1

# Give overmind a moment to actually start the process
# Give PostgreSQL a moment to actually start the process
sleep 2

while [ $attempt -le $max_attempts ]; do
Expand All @@ -142,7 +149,6 @@ wait_for_postgres() {
done

echo "PostgreSQL failed to start after $max_attempts attempts"
overmind echo postgres
return 1
}

Expand Down Expand Up @@ -175,26 +181,7 @@ trim_schema() {
;;
esac
}
overmind_start() {
cat > Procfile << EOF
postgres_${PSQL_VERSION}: exec nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations
EOF
overmind start -D
echo "Waiting for overmind socket..."
max_wait=5
count=0
while [ $count -lt $max_wait ]; do
if [ -S "./.overmind.sock" ]; then
# Found the socket, give it a moment to be ready
sleep 5
echo "Socket file found and ready"
break
fi
echo "Waiting for socket file (attempt $count/$max_wait)"
sleep 1
count=$((count + 1))
done
}

perform_dump() {
local max_attempts=3
local attempt=1
Expand All @@ -214,21 +201,18 @@ perform_dump() {
echo "All dump attempts failed"
return 1
}

migrate_version() {
echo "PSQL_VERSION: $PSQL_VERSION"
overmind kill || true
rm -f .overmind.sock Procfile || true
#pkill -f "postgres" || true # Ensure PostgreSQL is stopped before starting
PSQLBIN=$(nix build --no-link "$FLAKE_URL#psql_$PSQL_VERSION/bin" --json | jq -r '.[].outputs.out + "/bin"')
echo "Using PostgreSQL version $PSQL_VERSION from $PSQLBIN"

# Start overmind
overmind_start
echo "Waiting for overmind socket..."


# Start PostgreSQL
start_postgres
echo "Waiting for PostgreSQL to be ready..."

#Wait for PostgreSQL to be ready to accept connections
# Wait for PostgreSQL to be ready to accept connections
if ! wait_for_postgres; then
echo "Failed to connect to PostgreSQL server"
exit 1
Expand All @@ -255,11 +239,11 @@ EOSQL
"${PSQLBIN}/psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$PGBOUNCER_AUTH_SCHEMA_SQL"
"${PSQLBIN}/psql" -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$STAT_EXTENSION_SQL"

#set db url to run dbmate
# Set db url to run dbmate
export DATABASE_URL="postgres://$PGSQL_USER:$PGPASSWORD@localhost:$PORTNO/postgres?sslmode=disable"
#export path so dbmate can find correct psql and pg_dump
# Export path so dbmate can find correct psql and pg_dump
export PATH="$PSQLBIN:$PATH"
# run init scripts
# Run init scripts
if ! dbmate --migrations-dir "$MIGRATIONS_DIR/init-scripts" up; then
echo "Error: Initial migration failed"
exit 1
Expand Down
23 changes: 20 additions & 3 deletions nix/tools/run-server.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ start_postgres() {
}

stop_postgres() {
pg_ctl stop -D "$DATDIR" -m fast
if [ "$DAEMONIZE" = true ]; then
echo "PostgreSQL is running in daemon mode. Please stop it using pg_ctl."
else
pg_ctl stop -D "$DATDIR" -m fast
fi
}

trap 'stop_postgres' SIGINT SIGTERM

# Parse arguments
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -104,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"
Expand Down Expand Up @@ -161,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@

Expand Down Expand Up @@ -209,6 +223,8 @@ sed -e "1i\\
include = '$DATDIR/supautils.conf'" \
-e "\$a\\
pgsodium.getkey_script = '$PGSODIUM_GETKEY_SCRIPT'" \
-e "\$a\\
vault.getkey_script = '$PGSODIUM_GETKEY_SCRIPT'" \
-e "s|data_directory = '/var/lib/postgresql/data'|data_directory = '$DATDIR'|" \
-e "s|hba_file = '/etc/postgresql/pg_hba.conf'|hba_file = '$DATDIR/pg_hba.conf'|" \
-e "s|ident_file = '/etc/postgresql/pg_ident.conf'|ident_file = '$DATDIR/pg_ident.conf'|" \
Expand Down Expand Up @@ -329,6 +345,7 @@ EOSQL
fi
fi
echo "Shutting down PostgreSQL..."

stop_postgres

# Step 4: Restart PostgreSQL in the foreground (with log output visible) or as a daemon
Expand Down
Loading