Skip to content

Commit 5b47980

Browse files
committed
Development makefile improvement: db-reset should work properly
Previously it was difficult to run because it needed a running db server, but a stopped web. Now it automatically stops/starts web if needed, so it can be run when the compose config is already running. Also improved down to do a graceful stop, so that the pid file doesn't remain there.
1 parent af30645 commit 5b47980

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dev-prod-detach: ## Start dev stack but run Rails in production mode (uses dev c
1212
RAILS_ENV=production NODE_ENV=production RAILS_SERVE_STATIC_FILES=1 RAILS_LOG_TO_STDOUT=1 FORCE_SSL=false $(COMPOSE) up -d --build
1313

1414
down: ## Stop dev stack
15-
$(COMPOSE) down
15+
$(COMPOSE) stop
1616
rm -f tmp/pids/server.pid
1717

1818
shell: ## Open a shell in the web container
@@ -27,8 +27,18 @@ test: ## Run RSpec in the web container
2727
db-migrate: ## Run db:migrate
2828
$(COMPOSE) exec web bin/rails db:migrate
2929

30-
db-reset: ## Drop and prepare (create/migrate)
31-
$(COMPOSE) run --rm web bin/rails db:drop && bin/rails db:prepare
30+
db-reset: ## Drop and setup (create/migrate/seed) - stops web if running, restarts after
31+
@WEB_WAS_RUNNING=$$($(COMPOSE) ps --status running --format '{{.Service}}' | grep -q '^web$$' && echo 1 || echo 0); \
32+
if [ "$$WEB_WAS_RUNNING" = "1" ]; then \
33+
echo "Stopping web container..."; \
34+
$(COMPOSE) stop web; \
35+
fi; \
36+
echo "Running db:drop and db:setup..."; \
37+
$(COMPOSE) run --rm web bin/rails db:drop db:setup; \
38+
if [ "$$WEB_WAS_RUNNING" = "1" ]; then \
39+
echo "Restarting web container..."; \
40+
$(COMPOSE) start web; \
41+
fi
3242

3343
db-import: ## Drop dev DB and import a public dump (env: DUMP=/path/to/public-YYYY-MM.sql.gz)
3444
@if [ -z "$(DUMP)" ]; then echo "Set DUMP=/path/to/public-YYYY-MM.sql.gz"; exit 1; fi

0 commit comments

Comments
 (0)