diff --git a/Makefile b/Makefile index 077f2f29..02876da1 100644 --- a/Makefile +++ b/Makefile @@ -14,92 +14,40 @@ MIGRATIONS_DIR := ./migrations MIGRATE_IMAGE := migrate/migrate:v4.15.2 DB_URL := postgres://$(DB_USER):$(DB_PASSWORD)@lti-postgres:5432/$(DB_NAME)?sslmode=disable -# Tunggu DB ready memakai pg_isready dari image postgres -WAIT_DB := docker run --rm --network $(NETWORK) postgres:alpine \ - sh -c 'until pg_isready -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -d $(DB_NAME); do echo "waiting for postgres..."; sleep 1; done' - -# Default target -.DEFAULT_GOAL := start - -# --- Daftar phony targets --- -.PHONY: start build test lint gen \ - db-up wait-db \ - migration-% migrate-up migrate-down migrate-fresh \ - seed \ - docker-local docker-down docker-nuke docker-cache psql - -# --- Go workflow --- -start: - @go run cmd/api/main.go - -build: - @go build -o tmp/app ./cmd/api - -test: - @go test ./test/... - -lint: - @golangci-lint run - -# --- Compose / DB helpers --- -db-up: - @$(COMPOSE) up -d postgresdb - -wait-db: - @$(WAIT_DB) - -# --- Migration (pembuatan file) --- -# Contoh: make migration-create_users_table -# ":" akan diubah ke "_" (biar aman untuk nama file) -migration-%: - @migrate create -ext sql -dir $(MIGRATIONS_DIR) $(subst :,_,$*) - -# --- Migration (apply via docker image 'migrate') --- -migrate-up: db-up wait-db - @docker run --rm -v $(MIGRATIONS_DIR):/migrations --network $(NETWORK) \ - $(MIGRATE_IMAGE) -path=/migrations/ -database "$(DB_URL)" up - -# Contoh: -# make migrate-down step=2 → rollback 2 step -# make migrate-down → rollback semua - -migrate-down: db-up wait-db - @if [ -n "$(step)" ]; then \ - echo "⬇️ Migrating down $(step) step(s)..."; \ - docker run --rm -v $(MIGRATIONS_DIR):/migrations --network $(NETWORK) \ - $(MIGRATE_IMAGE) -path=/migrations/ -database "$(DB_URL)" down $(step); \ - else \ - echo "⬇️ Migrating down ALL steps..."; \ - docker run --rm -v $(MIGRATIONS_DIR):/migrations --network $(NETWORK) \ - $(MIGRATE_IMAGE) -path=/migrations/ -database "$(DB_URL)" down -all; \ - fi - -migrate-fresh: migrate-down migrate-up - @true - -# Pakai: make migrate-force v=20250917120000 -migrate-force: - @docker run --rm -v $(MIGRATIONS_DIR):/migrations --network $(NETWORK) \ - $(MIGRATE_IMAGE) -path=/migrations/ -database "$(DB_URL)" force $(v) - - -# --- Seeder --- -seed: db-up wait-db - @$(COMPOSE) run --rm app go run cmd/seed/main.go - -# --- Docker orchestration convenience --- +# --- Docker --- docker-local: + @echo "🚀 Starting $(APP_NAME) with local PostgreSQL & Redis..." @$(COMPOSE) up --build -d docker-down: @$(COMPOSE) down --remove-orphans -# ⚠️ Akan menghapus container, images dan volumes. docker-nuke: + @echo "💣 Removing all containers, images, and volumes..." @$(COMPOSE) down --rmi all --volumes --remove-orphans -docker-cache: - @docker builder prune -f +# --- Database / Migration --- + +wait-db: + @echo "⏳ Waiting for database lti-postgres to be ready (inside Docker network)..." + @$(COMPOSE) run --rm app sh -c 'until nc -z lti-postgres 5432; do echo "Waiting for DB..."; sleep 2; done; echo "✅ Database is ready!"' + +migrate-up: wait-db + @echo "⬆️ Running migrations..." + @docker run --rm -v $(MIGRATIONS_DIR):/migrations --network $(NETWORK) \ + $(MIGRATE_IMAGE) -path=/migrations/ -database "$(DB_URL)" up + +migrate-down: wait-db + @echo "⬇️ Rolling back all migrations..." + @docker run --rm -v $(MIGRATIONS_DIR):/migrations --network $(NETWORK) \ + $(MIGRATE_IMAGE) -path=/migrations/ -database "$(DB_URL)" down -all + +seed: + @echo "🌱 Running seed script..." + @$(COMPOSE) run --rm app go run cmd/seed/main.go + +psql: + @docker exec -it lti-postgres psql -U $(DB_USER) -d $(DB_NAME) logs: @$(COMPOSE) logs -f app