Files
backend/Makefile
2025-12-13 22:34:01 +05:00

171 lines
5.0 KiB
Makefile

.PHONY: help db-up db-down db-restart db-logs db-ps migrate-up migrate-down migrate-status migrate-reset migrate-create db-shell db-reset build clean install-goose
# Load environment variables from .env file if it exists
ifneq (,$(wildcard .env))
include .env
export
endif
# Default values if not set in .env
DB_USER ?= volontery
DB_NAME ?= volontery_db
DB_HOST ?= localhost
DB_PORT ?= 5432
DB_SSLMODE ?= disable
# Database connection string for goose
DB_STRING := "postgres://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=$(DB_SSLMODE)"
help: ## Show this help message
@echo 'Usage: make [target]'
@echo ''
@echo 'Available targets:'
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
##@ Database Operations
db-up: ## Start PostgreSQL database container
@echo "Starting PostgreSQL with PostGIS..."
docker-compose up -d postgres
@echo "Waiting for database to be ready..."
@sleep 5
@echo "Database is ready!"
db-down: ## Stop PostgreSQL database container
@echo "Stopping PostgreSQL..."
docker-compose down
db-restart: db-down db-up ## Restart PostgreSQL database container
db-logs: ## Show PostgreSQL container logs
docker-compose logs -f postgres
db-ps: ## Show running database containers
docker-compose ps
db-shell: ## Connect to PostgreSQL shell
docker-compose exec postgres psql -U $(DB_USER) -d $(DB_NAME)
db-reset: ## Reset database (WARNING: destroys all data!)
@echo "WARNING: This will destroy all data in the database!"
@read -p "Are you sure? [y/N]: " confirm && [ "$$confirm" = "y" ] || exit 1
@echo "Resetting database..."
docker-compose down -v
docker-compose up -d postgres
@sleep 5
@$(MAKE) migrate-up
@echo "Database reset complete!"
##@ Migration Operations
install-goose: ## Install goose migration tool
@echo "Installing goose..."
go install github.com/pressly/goose/v3/cmd/goose@latest
@echo "goose installed successfully!"
migrate-up: ## Apply all pending migrations
@echo "Applying migrations..."
goose -dir migrations postgres $(DB_STRING) up
@echo "Migrations applied successfully!"
migrate-down: ## Rollback the last migration
@echo "Rolling back last migration..."
goose -dir migrations postgres $(DB_STRING) down
@echo "Migration rolled back successfully!"
migrate-status: ## Show migration status
@echo "Migration status:"
goose -dir migrations postgres $(DB_STRING) status
migrate-reset: ## Reset all migrations (WARNING: destroys all data!)
@echo "WARNING: This will destroy all data in the database!"
@read -p "Are you sure? [y/N]: " confirm && [ "$$confirm" = "y" ] || exit 1
@echo "Resetting migrations..."
goose -dir migrations postgres $(DB_STRING) reset
@echo "Migrations reset complete!"
migrate-create: ## Create a new migration file (usage: make migrate-create NAME=migration_name)
@if [ -z "$(NAME)" ]; then \
echo "Error: NAME is required. Usage: make migrate-create NAME=migration_name"; \
exit 1; \
fi
@echo "Creating migration: $(NAME)"
goose -dir migrations create $(NAME) sql
@echo "Migration file created!"
##@ sqlc Operations
install-sqlc: ## Install sqlc code generation tool
@echo "Installing sqlc..."
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
@echo "sqlc installed successfully!"
sqlc-generate: ## Generate Go code from SQL queries
@echo "Generating code from SQL..."
sqlc generate
@echo "Code generation complete!"
sqlc-vet: ## Validate SQL queries
@echo "Validating SQL queries..."
sqlc vet
@echo "SQL validation complete!"
##@ Build Operations
build: ## Build the Go application
@echo "Building application..."
go build -o bin/volontery-api ./cmd/api
@echo "Build complete! Binary: bin/volontery-api"
clean: ## Clean build artifacts
@echo "Cleaning build artifacts..."
rm -rf bin/
@echo "Clean complete!"
##@ Development
dev: db-up ## Start development environment
@echo "Development environment ready!"
@echo "Database: postgres://$(DB_USER):***@$(DB_HOST):$(DB_PORT)/$(DB_NAME)"
@echo ""
@echo "Next steps:"
@echo " 1. Copy .env.example to .env and configure"
@echo " 2. Run 'make migrate-up' to apply migrations"
@echo " 3. Run 'go run cmd/api/main.go' to start the API"
setup: ## Initial project setup
@echo "Setting up project..."
@if [ ! -f .env ]; then \
echo "Creating .env from .env.example..."; \
cp .env.example .env; \
echo "Please edit .env and set DB_PASSWORD!"; \
fi
@$(MAKE) install-goose
@mkdir -p bin
@echo "Setup complete!"
test-db: ## Run database tests
@echo "Testing database connection..."
@docker-compose exec postgres pg_isready -U $(DB_USER) -d $(DB_NAME) && \
echo "Database connection: OK" || \
echo "Database connection: FAILED"
##@ Quality
fmt: ## Format Go code
@echo "Formatting code..."
go fmt ./...
lint: ## Run linter
@echo "Running linter..."
golangci-lint run
test: ## Run tests
@echo "Running tests..."
go test -v -race -coverprofile=coverage.out ./...
coverage: test ## Show test coverage
@echo "Generating coverage report..."
go tool cover -html=coverage.out -o coverage.html
@echo "Coverage report: coverage.html"