-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
87 lines (78 loc) · 3.16 KB
/
Makefile
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
# migration name
NAME ?= name
# default database configuration
TEST_DB_USER ?= postgres
TEST_DB_PASSWORD ?= postgres
TEST_DB_NAME ?= postgres
TEST_DB_HOST_PORT ?= 5432
TEST_DB_URL ?= postgres://$(TEST_DB_USER):$(TEST_DB_PASSWORD)@localhost:$(TEST_DB_HOST_PORT)/$(TEST_DB_NAME)?sslmode=disable
POSTGRESQL_VERSION ?= 16
.PHONY: query
dev:
@VERSION=dev APP_ENV=development air
up:
@goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" up
down:
@goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" down-to 0
migration:
@goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" create $(NAME) sql
sql:
@sqlc generate
setup:
@go install github.com/air-verse/[email protected] && \
go install github.com/sqlc-dev/sqlc/cmd/[email protected] && \
go install github.com/pressly/goose/v3/cmd/[email protected]
init-dev-db:
@docker run --name nokap-postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DATABASE=postgres -p 5432:5432 -d postgres:$(POSTGRESQL_VERSION) && \
timeout 90s bash -c "until docker exec nokap-postgres pg_isready ; do sleep 5 ; done" && echo "Postgres is ready! Run migrations with 'make up'"
start-dev-db:
@docker start nokap-postgres && \
timeout 90s bash -c \
"until docker exec nokap-postgres pg_isready ; do sleep 5 ; done" && \
echo "Postgres is ready! Run migrations with 'make up'"
stop-dev-db:
@docker stop nokap-postgres
check-dev-db:
@docker exec nokap-postgres pg_isready
clean-dev-db:
@docker stop nokap-postgres && docker rm nokap-postgres
health-dev-db:
@echo "Checking server health..."
@curl -s localhost:8080/api/v1/health | jq || echo "Failed to connect to health endpoint"
query:
@docker exec nokap-postgres psql -U postgres -d postgres -c "$(filter-out $@,$(MAKECMDGOALS))"
# Prevent make from treating the query string as a target
%:
@:
test: start-testdb
@trap 'make stop-testdb' EXIT; \
if ! make run-tests; then \
exit 1; \
fi
start-testdb:
@echo "Setting up PostgreSQL database..."
@if ! docker ps -a --filter "name=test-postgres" --format '{{.Names}}' | grep -q '^test-postgres$$'; then \
docker run --name test-postgres \
-e POSTGRES_USER=$(TEST_DB_USER) \
-e POSTGRES_PASSWORD=$(TEST_DB_PASSWORD) \
-e POSTGRES_DB=$(TEST_DB_NAME) \
-d -p "$(TEST_DB_HOST_PORT):5432" \
postgres:$(POSTGRESQL_VERSION); \
elif ! docker ps --filter "name=test-postgres" --format '{{.Names}}' | grep -q '^test-postgres$$'; then \
docker start test-postgres; \
fi
@echo "Waiting for PostgreSQL to be ready..."
@timeout 30s bash -c 'until docker exec test-postgres pg_isready -U $(TEST_DB_USER); do sleep 1; done'
@echo "Running migrations..."
@goose -dir .sqlc/migrations postgres "$(TEST_DB_URL)" up
run-tests:
@go test -v ./...
stop-testdb:
@echo "Stopping test db..."
@docker container stop test-postgres || true
@echo "Removing test db..."
@docker container rm -f test-postgres || true
# helper target to check database connection
check-testdb:
@echo "Testing database connection..."
@docker exec test-postgres psql -U $(TEST_DB_USER) -d $(TEST_DB_NAME) -c "SELECT 1"