BINDIR=${CURDIR}/../bin
PACKAGE=route256/comments

MIGRATIONS_FOLDER	:= ./db/migrations/
LOCAL_DB_NAME		:= route256
LOCAL_DB_DSN		:= postgresql://user:password@localhost:5433/route256?sslmode=disable

PROD_USER := comments-user
PROD_PASS := comments-password
PROD_DB   := postgres-comments-shard

PROD_MIGRATIONS := ./comments/db/migrations/

build: bindir
	echo "build comments"
	go build -o ${BINDIR}/comments cmd/server/main.go

bindir:
	mkdir -p ${BINDIR}

# Used for CI
run-migrations:
	$(GOOSE) -dir $(PROD_MIGRATIONS) postgres "postgresql://$(PROD_USER)-1:$(PROD_PASS)-1@$(PROD_DB)-1:5432/comments_db?sslmode=disable" up
	$(GOOSE) -dir $(PROD_MIGRATIONS) postgres "postgresql://$(PROD_USER)-2:$(PROD_PASS)-2@$(PROD_DB)-2:5432/comments_db?sslmode=disable" up

db-create-migration:
	$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) create -s $(n) sql

db-migrate:
	$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-1:$(PROD_PASS)-1@192.168.64.5:5438/comments_db?sslmode=disable" up
	$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-2:$(PROD_PASS)-2@192.168.64.5:5439/comments_db?sslmode=disable" up

db-migrate-down:
	$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-1:$(PROD_PASS)-1@192.168.64.5:5438/comments_db?sslmode=disable" down
	$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) postgres "postgresql://$(PROD_USER)-2:$(PROD_PASS)-2@192.168.64.5:5439/comments_db?sslmode=disable" down

db-reset-local:
	psql -c "drop database if exists \"$(LOCAL_DB_NAME)\""
	psql -c "create database \"$(LOCAL_DB_NAME)\""
	make db-migrate

.PHONY: generate-sqlc
generate-sqlc:
	$(BINDIR)/sqlc generate
