-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
116 lines (99 loc) · 5.47 KB
/
Makefile
File metadata and controls
116 lines (99 loc) · 5.47 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
SHELL := /bin/bash
ROOT := $(shell pwd)
RUST_TOOLCHAIN := 1.90.0
SQLX_FEATURES := postgres
DATABASE_URL ?= postgres://aether:postgres@localhost:5432/aether_dev
TEST_DATABASE_URL ?= postgres://aether:postgres@localhost:5432/aether_test
PG_CONTAINER_NAME ?= aether-pg-test
PG_IMAGE ?= postgres:15
SQLX ?= sqlx
.PHONY: all build fmt lint test clean sqlx-prepare crd db-start test-no-db test-db
all: build
build:
cargo build --workspace --all-targets
fmt:
cargo fmt --all
lint:
cargo clippy --workspace --all-targets --all-features -- -D warnings
test-no-db:
cargo test --workspace --all-features -- --nocapture
db-start: ensure-postgres ## Start (or ensure) local Postgres container based on DATABASE_URL
@echo "[db-start] Postgres ready at $(DATABASE_URL)"
test: ensure-postgres ## Run full test suite after ensuring Postgres is up
DATABASE_URL=$(DATABASE_URL) cargo test --workspace --all-features -- --nocapture
test-db: ensure-postgres ## Initialize dedicated test database and run migrations
@echo "[test-db] Using test database URL=$(TEST_DATABASE_URL)"; \
if ! command -v psql >/dev/null 2>&1; then echo "[test-db] ERROR: psql not found in PATH"; exit 1; fi; \
BASE_URL=$$(echo $(TEST_DATABASE_URL) | sed -E 's#/[^/]+$#/postgres#'); \
DB_NAME=$$(echo $(TEST_DATABASE_URL) | sed -E 's#.*/([^/?]+)(\?.*)?$#\1#'); \
USER_PART=$$(echo $(TEST_DATABASE_URL) | sed -E 's#postgres://([^:@/]+).*#\1#'); \
PASS_PART=$$(echo $(TEST_DATABASE_URL) | sed -nE 's#postgres://[^:]+:([^@]+)@.*#\1#p'); \
if [ -n "$$PASS_PART" ]; then export PGPASSWORD="$$PASS_PART"; fi; \
echo "[test-db] Ensuring database $$DB_NAME exists..."; \
psql "$$BASE_URL" -tc "SELECT 1 FROM pg_database WHERE datname='$$DB_NAME'" | grep -q 1 || psql "$$BASE_URL" -c "CREATE DATABASE $$DB_NAME"; \
echo "[test-db] Running migrations (control-plane)..."; \
if ! command -v sqlx >/dev/null 2>&1; then cargo install sqlx-cli --no-default-features --features native-tls,postgres >/dev/null; fi; \
(cd crates/control-plane && DATABASE_URL=$(TEST_DATABASE_URL) sqlx migrate run >/dev/null); \
echo "[test-db] Done. You can now run: DATABASE_URL=$(TEST_DATABASE_URL) cargo test -p control-plane"
clean:
cargo clean
sqlx-prepare:
DATABASE_URL=$(DATABASE_URL) cargo sqlx prepare --workspace -- --all-targets
.PHONY: test-full ensure-postgres schema-drift
ensure-postgres:
@echo "[ensure-postgres] Checking database connectivity..."; \
if ! PGPASSWORD=$$(echo $(DATABASE_URL) | sed -E 's#.*/([^:]+):([^@]+)@.*#\2#') psql "$(DATABASE_URL)" -c 'SELECT 1' >/dev/null 2>&1; then \
echo "[ensure-postgres] No reachable Postgres at $(DATABASE_URL). Attempting to start container $(PG_CONTAINER_NAME)..."; \
if command -v docker >/dev/null 2>&1; then \
if docker ps -a --format '{{.Names}}' | grep -q '^$(PG_CONTAINER_NAME)$$'; then \
docker start $(PG_CONTAINER_NAME) >/dev/null; \
else \
docker run -d --name $(PG_CONTAINER_NAME) -e POSTGRES_USER=aether -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=aether_dev -p 5432:5432 $(PG_IMAGE) >/dev/null; \
fi; \
elif command -v podman >/dev/null 2>&1; then \
if podman ps -a --format '{{.Names}}' | grep -q '^$(PG_CONTAINER_NAME)$$'; then \
podman start $(PG_CONTAINER_NAME) >/dev/null; \
else \
podman run -d --name $(PG_CONTAINER_NAME) -e POSTGRES_USER=aether -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=aether_dev -p 5432:5432 $(PG_IMAGE) >/dev/null; \
fi; \
else \
echo "[ensure-postgres] ERROR: docker or podman not found; please start Postgres manually."; exit 1; \
fi; \
echo "[ensure-postgres] Waiting for Postgres to become ready..."; \
for i in $$(seq 1 30); do \
if PGPASSWORD=postgres psql "$(DATABASE_URL)" -c 'SELECT 1' >/dev/null 2>&1; then echo "[ensure-postgres] Postgres is ready."; break; fi; \
sleep 1; \
if [ $$i -eq 30 ]; then echo "[ensure-postgres] Timed out waiting for Postgres."; exit 1; fi; \
done; \
else \
echo "[ensure-postgres] Existing Postgres reachable."; \
fi
test-full: ensure-postgres
@echo "[test-full] Ensuring sqlx-cli installed..."; \
if ! command -v $(SQLX) >/dev/null 2>&1; then cargo install sqlx-cli --no-default-features --features native-tls,postgres; fi; \
echo "[test-full] Running migrations..."; \
(cd crates/control-plane && DATABASE_URL=$(DATABASE_URL) $(SQLX) migrate run); \
echo "[test-full] Running full workspace tests..."; \
DATABASE_URL=$(DATABASE_URL) cargo test --workspace --all-features -- --nocapture
# Schema drift detection: regenerates sqlx-data.json and fails if it changes
schema-drift: ensure-postgres
@echo "[schema-drift] Checking for schema drift against live DB..."; \
if ! command -v $(SQLX) >/dev/null 2>&1; then cargo install sqlx-cli --no-default-features --features native-tls,postgres; fi; \
tmp=$$(mktemp); \
[ -f sqlx-data.json ] && cp sqlx-data.json $$tmp || true; \
DATABASE_URL=$(DATABASE_URL) cargo sqlx prepare --workspace -- --all-targets >/dev/null 2>&1 || { echo "[schema-drift] prepare failed"; exit 1; }; \
if [ -f $$tmp ]; then \
if diff -q $$tmp sqlx-data.json >/dev/null; then \
echo "[schema-drift] No drift detected."; \
rm -f $$tmp; \
else \
echo "[schema-drift] Drift detected. Updated sqlx-data.json differs from committed version."; \
echo "[schema-drift] Please review and commit the new sqlx-data.json."; \
exit 1; \
fi; \
else \
echo "[schema-drift] Baseline sqlx-data.json missing; generated a new one. Commit it."; \
exit 1; \
fi
crd:
cargo run -p aether-operator --bin crd-gen > k8s/aetherapp-crd.yaml