feat: add database migrations and enhance Makefile with environment loading
This commit is contained in:
parent
23ec4d7bd2
commit
e8aeef6013
15
Makefile
15
Makefile
@ -1,6 +1,12 @@
|
|||||||
# ULFlow Golang Starter Kit Makefile
|
# ULFlow Golang Starter Kit Makefile
|
||||||
# Provides common commands for development, testing, and deployment
|
# 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
|
.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.
|
# Default target executed when no arguments are given to make.
|
||||||
@ -164,7 +170,9 @@ migrate-create:
|
|||||||
|
|
||||||
# Run migrations up
|
# Run migrations up
|
||||||
m-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
|
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" up
|
||||||
|
|
||||||
# Run migrations down
|
# Run migrations down
|
||||||
@ -180,6 +188,11 @@ m-reset: m-down m-up
|
|||||||
m-status:
|
m-status:
|
||||||
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" version
|
@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 application (default: without hot reload)
|
||||||
run:
|
run:
|
||||||
go run ./cmd/app/main.go
|
go run ./cmd/app/main.go
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
-- +goose StatementBegin
|
-- +goose StatementBegin
|
||||||
|
-- Enable UUID extension
|
||||||
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
|
|
||||||
CREATE TABLE roles (
|
CREATE TABLE roles (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(50) UNIQUE NOT NULL,
|
name VARCHAR(50) UNIQUE NOT NULL,
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
-- +goose StatementBegin
|
-- +goose StatementBegin
|
||||||
|
-- Ensure UUID extension is available
|
||||||
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
|
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
username VARCHAR(50) UNIQUE NOT NULL,
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
|
|||||||
@ -1,17 +1,32 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
-- +goose StatementBegin
|
-- +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,
|
user_id UUID NOT NULL,
|
||||||
role_id INTEGER NOT NULL,
|
role_id INTEGER NOT NULL,
|
||||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (user_id, role_id),
|
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
|
|
||||||
);
|
);
|
||||||
|
|
||||||
-- 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_user_id ON user_roles(user_id);
|
||||||
CREATE INDEX idx_user_roles_role_id ON user_roles(role_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 StatementEnd
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user