- Add schema/*.dbml covering all existing tables (001-019) - Wire relspecgo via make generate-migrations target - Add make check-schema-drift for CI drift detection - Add schema/README.md documenting the DBML-first workflow Closes #19
76 lines
3.0 KiB
Makefile
76 lines
3.0 KiB
Makefile
BIN_DIR := bin
|
|
GO_CACHE_DIR := $(CURDIR)/.cache/go-build
|
|
SERVER_BIN := $(BIN_DIR)/amcs-server
|
|
CMD_SERVER := ./cmd/amcs-server
|
|
BUILDINFO_PKG := git.warky.dev/wdevs/amcs/internal/buildinfo
|
|
PATCH_INCREMENT ?= 1
|
|
VERSION_TAG ?= $(shell git describe --tags --exact-match 2>/dev/null || echo dev)
|
|
COMMIT_SHA ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
|
|
BUILD_DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
|
|
RELSPEC ?= $(shell command -v relspec 2>/dev/null || echo $(HOME)/go/bin/relspec)
|
|
SCHEMA_FILES := $(sort $(wildcard schema/*.dbml))
|
|
GENERATED_SCHEMA_MIGRATION := migrations/020_generated_schema.sql
|
|
LDFLAGS := -s -w \
|
|
-X $(BUILDINFO_PKG).Version=$(VERSION_TAG) \
|
|
-X $(BUILDINFO_PKG).TagName=$(VERSION_TAG) \
|
|
-X $(BUILDINFO_PKG).Commit=$(COMMIT_SHA) \
|
|
-X $(BUILDINFO_PKG).BuildDate=$(BUILD_DATE)
|
|
|
|
.PHONY: all build clean migrate release-version test generate-migrations check-schema-drift
|
|
|
|
all: build
|
|
|
|
build:
|
|
@mkdir -p $(BIN_DIR)
|
|
go build -ldflags "$(LDFLAGS)" -o $(SERVER_BIN) $(CMD_SERVER)
|
|
|
|
test:
|
|
@mkdir -p $(GO_CACHE_DIR)
|
|
GOCACHE=$(GO_CACHE_DIR) go test ./...
|
|
|
|
release-version:
|
|
@case "$(PATCH_INCREMENT)" in \
|
|
''|*[!0-9]*|0) echo "PATCH_INCREMENT must be a positive integer" >&2; exit 1 ;; \
|
|
esac
|
|
@latest=$$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1); \
|
|
if [ -z "$$latest" ]; then latest="v0.0.0"; fi; \
|
|
version=$${latest#v}; \
|
|
major=$${version%%.*}; \
|
|
rest=$${version#*.}; \
|
|
minor=$${rest%%.*}; \
|
|
patch=$${rest##*.}; \
|
|
next_patch=$$((patch + $(PATCH_INCREMENT))); \
|
|
next_tag="v$$major.$$minor.$$next_patch"; \
|
|
if git rev-parse -q --verify "refs/tags/$$next_tag" >/dev/null; then \
|
|
echo "$$next_tag already exists" >&2; \
|
|
exit 1; \
|
|
fi; \
|
|
git tag -a "$$next_tag" -m "Release $$next_tag"; \
|
|
echo "$$next_tag"
|
|
|
|
migrate:
|
|
./scripts/migrate.sh
|
|
|
|
clean:
|
|
rm -rf $(BIN_DIR)
|
|
|
|
generate-migrations:
|
|
@test -n "$(SCHEMA_FILES)" || (echo "No DBML schema files found in schema/" >&2; exit 1)
|
|
@command -v $(RELSPEC) >/dev/null 2>&1 || (echo "relspec not found; install git.warky.dev/wdevs/relspecgo/cmd/relspec@latest" >&2; exit 1)
|
|
@schema_list=$$(printf '%s\n' $(SCHEMA_FILES) | paste -sd, -); \
|
|
$(RELSPEC) convert --from dbml --from-list "$$schema_list" --to pgsql --to-path $(GENERATED_SCHEMA_MIGRATION)
|
|
|
|
check-schema-drift:
|
|
@test -f $(GENERATED_SCHEMA_MIGRATION) || (echo "$(GENERATED_SCHEMA_MIGRATION) is missing; run make generate-migrations" >&2; exit 1)
|
|
@command -v $(RELSPEC) >/dev/null 2>&1 || (echo "relspec not found; install git.warky.dev/wdevs/relspecgo/cmd/relspec@latest" >&2; exit 1)
|
|
@tmpfile=$$(mktemp); \
|
|
schema_list=$$(printf '%s\n' $(SCHEMA_FILES) | paste -sd, -); \
|
|
$(RELSPEC) convert --from dbml --from-list "$$schema_list" --to pgsql --to-path $$tmpfile; \
|
|
if ! cmp -s $$tmpfile $(GENERATED_SCHEMA_MIGRATION); then \
|
|
echo "Schema drift detected between schema/*.dbml and $(GENERATED_SCHEMA_MIGRATION)" >&2; \
|
|
diff -u $(GENERATED_SCHEMA_MIGRATION) $$tmpfile || true; \
|
|
rm -f $$tmpfile; \
|
|
exit 1; \
|
|
fi; \
|
|
rm -f $$tmpfile
|