Skip to content

Commit

Permalink
Add rules to manage dev database to the Makefile
Browse files Browse the repository at this point in the history
Cache a ready-to-use database. It is rebuilt when the fixtures or
migrations change.

Measurements on my machine:
- Creating the DB from scratch: 50s
- Using pg_restore of the cached dump: 15s
  • Loading branch information
francoisfreitag committed Sep 12, 2024
1 parent 1c03698 commit 21f30fe
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ LINTER_CHECKED_DIRS := config itou scripts tests
PGDATABASE ?= itou
REQUIREMENTS_PATH ?= requirements/dev.txt

ifdef $(XDG_CACHE_HOME)
CACHEDIR := $(XDG_CACHE_HOME)
else
CACHEDIR := $(HOME)/.cache
endif
CACHEDIR := $(CACHEDIR)/itou
DBDUMP ?= $(CACHEDIR)/development_db.dump

VIRTUAL_ENV ?= .venv
export PATH := $(VIRTUAL_ENV)/bin:$(PATH)

Expand Down Expand Up @@ -105,7 +113,31 @@ shell_on_postgres_container:
# Database.
# =============================================================================

.PHONY: restore_latest_backup
.PHONY: dumpcreate dumprestore resetdb restore_latest_backup

dumpcreate: $(VIRTUALENV)
dropdb --if-exists $(PGDATABASE)
createdb $(PGDATABASE)
python manage.py migrate
$(MAKE) populate_db
mkdir --parents $(CACHEDIR)
pg_dump --format=c --dbname=$(PGDATABASE) --file=$(DBDUMP)

# There are no dependencies on fixtures, allowing developers to
dumprestore: $(VIRTUALENV)
dropdb --if-exists $(PGDATABASE)
createdb $(PGDATABASE)
pg_restore --dbname=$(PGDATABASE) $(DBDUMP)
python manage.py migrate

DBREADY := 0
$(DBDUMP): itou/fixtures/*/*.sql itou/fixtures/*/*.json itou/siae_evaluations/fixtures.py
$(MAKE) dumpcreate
$(eval DBREADY := 1)

# Recreate the database when fixtures change.
resetdb: $(DBDUMP)
if (( $(DBREADY) == 0 )); then $(MAKE) dumprestore; fi

restore_latest_backup:
./scripts/restore_latest_backup.sh $(PGDATABASE)

0 comments on commit 21f30fe

Please sign in to comment.