feat: add database migrations and enhance Makefile with environment loading
Some checks failed
CI Pipeline / Security Scan (push) Successful in 2m53s
CI Pipeline / Lint (push) Failing after 2m43s
CI Pipeline / Build (push) Has been skipped
CI Pipeline / Test (push) Has been skipped
CI Pipeline / Notification (push) Successful in 2s

This commit is contained in:
ulflow_phattt2901 2025-06-04 07:32:51 +07:00
parent 23ec4d7bd2
commit e8aeef6013
4 changed files with 40 additions and 6 deletions

View File

@ -1,6 +1,12 @@
# ULFlow Golang Starter Kit Makefile
# Provides common commands for development, testing, and deployment
# Load environment variables from .env file
ifneq (,$(wildcard ./.env))
include .env
export
endif
.PHONY: help init dev test lint build clean docker-build docker-run docker-clean docker-prune docker-compose-up docker-compose-down docker-compose-prod-up docker-compose-prod-down ci setup-git all
# Default target executed when no arguments are given to make.
@ -164,7 +170,9 @@ migrate-create:
# Run migrations up
m-up:
@echo "Running migrations..."
@echo "Running migrations with user: $(DATABASE_USERNAME)"
@echo "Database: $(DATABASE_NAME) on $(DATABASE_HOST):$(DATABASE_PORT)"
@echo "Using connection string: postgres://$(DATABASE_USERNAME):*****@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable"
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" up
# Run migrations down
@ -180,6 +188,11 @@ m-reset: m-down m-up
m-status:
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" version
# Force migration to specific version (fix dirty state)
m-force:
@echo "Forcing migration to version $(version)..."
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" force $(version)
# Run application (default: without hot reload)
run:
go run ./cmd/app/main.go

View File

@ -1,5 +1,8 @@
-- +goose Up
-- +goose StatementBegin
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE roles (
id SERIAL PRIMARY KEY,
name VARCHAR(50) UNIQUE NOT NULL,

View File

@ -1,5 +1,8 @@
-- +goose Up
-- +goose StatementBegin
-- Ensure UUID extension is available
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
username VARCHAR(50) UNIQUE NOT NULL,

View File

@ -1,17 +1,32 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE user_roles (
-- Tạo bảng mà không có ràng buộc
CREATE TABLE IF NOT EXISTS user_roles (
user_id UUID NOT NULL,
role_id INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, role_id),
CONSTRAINT fk_user_roles_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
CONSTRAINT fk_user_roles_role FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
PRIMARY KEY (user_id, role_id)
);
-- Create index for better query performance
-- Tạo index cho hiệu suất truy vấn tốt hơn
CREATE INDEX idx_user_roles_user_id ON user_roles(user_id);
CREATE INDEX idx_user_roles_role_id ON user_roles(role_id);
-- Thêm ràng buộc khóa ngoại nếu bảng tồn tại
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
ALTER TABLE user_roles ADD CONSTRAINT fk_user_roles_user
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'roles') THEN
ALTER TABLE user_roles ADD CONSTRAINT fk_user_roles_role
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE;
END IF;
END
$$;
-- +goose StatementEnd
-- +goose Down