Skip to content

Commit 9664006

Browse files
committed
Migration script
1 parent 2e178c3 commit 9664006

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ MAKEFLAGS += --no-print-directory
99
# User-configurable variables (edit this)
1010
# INFRA_SERVICES: long-running infra (db, broker, cache, ...)
1111
# INFRA_INIT_SERVICES: one-shot services that prepare INFRA_SERVICES
12+
# MIGRATION_DB_SERVICE: transactional db service used by alembic (empty = no migrations)
13+
# STAIRWAY_TEST: path to stairway test (empty = skip stairway step)
1214
# -----------------------------
1315
PROJECT_NAME ?= $(notdir $(abspath .))
1416
INFRA_SERVICES ?= db_pg
1517
INFRA_INIT_SERVICES ?=
18+
MIGRATION_DB_SERVICE ?= db_pg
19+
STAIRWAY_TEST ?= tests/integration/with_infra/test_stairway.py
1620

1721
# -----------------------------
1822
# Internal vars / aliases
@@ -24,6 +28,7 @@ DOCKER_ENV := scripts/makefile/docker_env.sh
2428
LOCAL_ENV := scripts/makefile/local_env.sh
2529
DOCKER_PRUNE := scripts/makefile/docker_prune.sh
2630
PYCACHE_DEL := scripts/makefile/pycache_del.sh
31+
MIGRATION := scripts/makefile/migration.sh
2732
DISHKA_PLOT_DATA := scripts/dishka/plot_dependencies_data.py
2833

2934
# Test stack is isolated by project name
@@ -108,6 +113,14 @@ down:
108113
stop-all:
109114
docker ps -q | xargs -r docker stop
110115

116+
# Migrations
117+
.PHONY: migration
118+
migration: local-env
119+
PROJECT_NAME=$(PROJECT_NAME) \
120+
MIGRATION_DB_SERVICE=$(MIGRATION_DB_SERVICE) \
121+
STAIRWAY_TEST=$(STAIRWAY_TEST) \
122+
$(MIGRATION) "$(msg)"
123+
111124
# Tests (with infra)
112125
.PHONY: test-docker
113126
test-docker: docker-env

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Test (all paths)
4646
make test-docker
4747
```
4848

49+
Generate a migration
50+
```shell
51+
make migration msg=<msg>
52+
```
53+
4954
See [Makefile](Makefile) for more commands
5055

5156
Thanks for your patience and support

scripts/makefile/migration.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
# The script is called from Makefile
3+
set -eu -o pipefail
4+
5+
if [ "$#" -ne 1 ] || [ -z "$1" ]; then
6+
echo 'ERROR: msg is required, e.g. make migration msg="add users"' >&2
7+
exit 2
8+
fi
9+
slug="$1"
10+
11+
: "${PROJECT_NAME:?must be set in Makefile}"
12+
: "${MIGRATION_DB_SERVICE:?must be set in Makefile (transactional db service for alembic)}"
13+
14+
trap 'docker compose -p "$PROJECT_NAME" stop "$MIGRATION_DB_SERVICE" >/dev/null' EXIT
15+
16+
docker compose -p "$PROJECT_NAME" up -d --build --wait --wait-timeout 180 "$MIGRATION_DB_SERVICE"
17+
alembic upgrade head
18+
alembic revision --autogenerate -m "$slug"
19+
20+
if [ -n "${STAIRWAY_TEST:-}" ]; then
21+
ALLOW_DESTRUCTIVE_TEST_CLEANUP=1 pytest -s -vv "$STAIRWAY_TEST"
22+
fi

0 commit comments

Comments
 (0)