-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
106 lines (92 loc) · 3.26 KB
/
Makefile
File metadata and controls
106 lines (92 loc) · 3.26 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
.PHONY: run build test migrate-up migrate-down clean install-migrate seed migrate-force migrate-fix-dirty migrate-version migrate-reset
GOPATH := $(shell go env GOPATH)
MIGRATE := $(GOPATH)/bin/migrate
run:
go run cmd/server/main.go
build:
go build -o bin/server cmd/server/main.go
test:
go test -v ./...
install-migrate:
@echo "Installing migrate tool..."
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
migrate-up: install-migrate
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" up
migrate-down: install-migrate
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" down
migrate-force: install-migrate
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
if [ -z "$$VERSION" ]; then \
echo "Error: VERSION is required."; \
echo "Usage: make migrate-force VERSION=<version_number>"; \
echo "Example: make migrate-force VERSION=4"; \
exit 1; \
fi; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" force $$VERSION
migrate-fix-dirty: install-migrate
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
@echo "Fixing dirty database state..."; \
@echo "This will force the version to the current dirty version to mark it as clean."; \
@echo "If the error says 'Dirty database version X', run: make migrate-force VERSION=X"; \
@echo ""; \
@echo "Current migration version:"; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" version || true
migrate-version: install-migrate
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" version
migrate-reset: install-migrate
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
@echo "WARNING: This will reset migrations to version 0 and reapply all migrations."; \
@echo "Press Ctrl+C to cancel, or Enter to continue..."; \
read confirm; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" drop -f; \
$(MIGRATE) -path migrations -database "$$DATABASE_URL" up
clean:
rm -rf bin/
seed:
@if [ -f .env ]; then \
export $$(grep -v '^#' .env | xargs); \
fi; \
if [ -z "$$DATABASE_URL" ]; then \
echo "Error: DATABASE_URL is not set. Please set it in your .env file or export it."; \
exit 1; \
fi; \
go run scripts/seed/main.go