diff --git a/Makefile b/Makefile index 8c1f3d1..bae78d4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help setup run dev build fmt migrate-up migrate-down migrate-status migrate-create generate docker-build +.PHONY: help setup run dev build fmt migrate-up migrate-down migrate-status migrate-create db-reset generate docker-build .DEFAULT_GOAL := help @@ -34,6 +34,9 @@ migrate-status: ## Show migration status migrate-create: ## Create a new migration (name=add_widgets) ./scripts/goose-create.sh $(name) +db-reset: ## Wipe the database and reapply all migrations + ./scripts/reset-db.sh + generate: ## Run sqlc codegen sqlc generate diff --git a/scripts/reset-db.sh b/scripts/reset-db.sh new file mode 100755 index 0000000..9363702 --- /dev/null +++ b/scripts/reset-db.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Reset the Postgres database configured in .env: drop all its content, then +# reapply migrations from scratch. +# Usage: ./scripts/reset-db.sh +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +set -a +# shellcheck disable=SC1091 +source "$SCRIPT_DIR/../.env" +set +a + +DBSTRING="${GOOSE_DBSTRING:-postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSL_MODE}}" + +echo "Dropping and recreating schema public on ${DB_NAME}..." +psql "$DBSTRING" -v ON_ERROR_STOP=1 -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" + +"$SCRIPT_DIR/goose.sh" up