.DEFAULT_GOAL := dev

-include .env
export

MIGRATE := migrate -path database_migrations -database "$(DATABASE_URL)"
MIGRATE_TEST := migrate -path database_migrations -database "$(TEST_DATABASE_URL)"

.PHONY: dev migrate-up migrate-down migrate-version tailwind run \
	test test-against-dev-db migrate-test-up migrate-test-down migrate-test-version

# applies pending migrations, starts the tailwind watcher in the background,
# then runs the server in the foreground. Ctrl-C stops both.
dev: migrate-up
	@./tailwind.sh & \
	TW_PID=$$!; \
	trap "kill $$TW_PID 2>/dev/null" EXIT INT TERM; \
	go run .

migrate-up:
	$(MIGRATE) up

migrate-down:
	$(MIGRATE) down 1

migrate-version:
	$(MIGRATE) version

tailwind:
	./tailwind.sh

run:
	go run .

# runs the integration test suite against TEST_DATABASE_URL (the dedicated
# inventory_2_test database by default). Applies pending migrations there
# first.
test: migrate-test-up
	go test ./...

# same as `test`, but points TEST_DATABASE_URL at the real dev database for
# this run instead of the dedicated test database - useful for checking
# behavior against real data. Leaves the dev DB in whatever state the tests'
# own cleanup produces; it's still your dev data, treat it accordingly.
test-against-dev-db: TEST_DATABASE_URL := $(DATABASE_URL)
test-against-dev-db: test

migrate-test-up:
	$(MIGRATE_TEST) up

migrate-test-down:
	$(MIGRATE_TEST) down 1

migrate-test-version:
	$(MIGRATE_TEST) version
