Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
89 lines (71 loc) · 2.74 KB
/
Copy pathMakefile
File metadata and controls
89 lines (71 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
.PHONY: db-up db-down db-dump
.PHONY: dvc dvc-build db-data-push db-data-pull
DB_PROJECT ?= rdr
DB_START_TIMEOUT ?= 120
DB_DUMP_DIR ?= /db
DB_COMPOSE = ./scripts/container.sh compose --project-name "$(DB_PROJECT)"
.PHONY: db-start db-wait db-info
# Separate recursive invocations preserve ordering even with make -j.
db-up: db-data-pull
@$(MAKE) --no-print-directory db-start
@$(MAKE) --no-print-directory db-wait
@$(MAKE) --no-print-directory db-info
db-start:
$(DB_COMPOSE) up --build --detach db
db-wait:
@timeout="$(DB_START_TIMEOUT)"; \
case "$$timeout" in \
''|*[!0-9]*|0) printf 'DB_START_TIMEOUT must be a positive number of seconds.\n' >&2; exit 2 ;; \
esac; \
printf 'Waiting for PostgreSQL initialization (up to %s seconds)...\n' "$$timeout"; \
elapsed=0; \
until $(DB_COMPOSE) exec -T db sh -c \
'pg_isready -h 127.0.0.1 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"' >/dev/null 2>&1; do \
if [ "$$elapsed" -ge "$$timeout" ]; then \
printf 'PostgreSQL did not become ready. Recent database logs:\n' >&2; \
$(DB_COMPOSE) logs --tail 50 db >&2 || true; \
exit 1; \
fi; \
sleep 1; \
elapsed=$$((elapsed + 1)); \
done
db-info:
@address=$$($(DB_COMPOSE) port db 5432) || exit $$?; \
case "$$address" in *:*) ;; *) address="127.0.0.1:$$address" ;; esac; \
printf '\nRDR is ready.\nAddress: %s\n' "$$address"
@$(DB_COMPOSE) exec -T db sh -c \
'printf "Database: %s\nUser: %s\nPassword: %s\n" "$$POSTGRES_DB" "$$POSTGRES_USER" "$$POSTGRES_PASSWORD"'
@printf '\nThis database is disposable. Run make db-dump before stopping it to save schema and data changes.\n'
db-down:
$(DB_COMPOSE) down --volumes --remove-orphans
# Export once, then extract matching schema and data SQL files.
db-dump:
@$(DB_COMPOSE) --profile tools run --rm --no-deps -T --entrypoint sh db-dump -ec '\
stage=$$(mktemp -d "$(DB_DUMP_DIR)/.dump-XXXXXX"); \
trap "rm -rf \"$$stage\"" EXIT; \
pg_dump --format=custom --file="$$stage/snapshot.dump"; \
pg_restore --schema-only --file="$$stage/schema.sql" "$$stage/snapshot.dump"; \
pg_restore --data-only --file="$$stage/data.sql" "$$stage/snapshot.dump"; \
mv "$$stage/schema.sql" "$$stage/data.sql" "$(DB_DUMP_DIR)/"; \
printf "Schema and data exported to %s\n" "$(DB_DUMP_DIR)"'
dvc:
./scripts/dvc.sh $(DVC_ARGS)
dvc-build:
./scripts/container.sh dvc-compose build --pull --no-cache dvc
db-data-push:
./scripts/dvc.sh \
push \
-r ensreg-git-lfs
db-data-pull:
./scripts/dvc.sh \
pull
.PHONY: schemaspy-docs docs-publish
schemaspy-docs: db-wait
DB_PROJECT="$(DB_PROJECT)" ./scripts/schemaspy.sh
docs-publish:
./scripts/publish-docs.sh
.PHONY: db-validate db-data-track
db-validate:
$(DB_COMPOSE) --profile tools run --build --rm --no-deps -T db-validate
db-data-track:
./scripts/dvc.sh add db/data.sql