Files
pos-system/scripts/db/migrate.sh
Ho Ngoc Hai 510e2306cd feat(docs): Update getting started and local development guides with new prerequisites and improved flow
- Enhanced Node.js and PNPM version recommendations in the getting started guide.
- Added a color palette section with Mermaid diagrams for better visual representation.
- Improved the workflow steps in the local development guide for clarity and consistency.
- Updated Vietnamese documentation to reflect changes in the English version.
- Refactored database migration and seeding scripts for better error handling and user feedback.
2026-01-08 15:37:25 +07:00

76 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# VI: Script migration database cho từng service riêng lẻ
set -e
SERVICE=$1
MODE=$2
# VI: Xác thực tham số tên service
if [ -z "$SERVICE" ]; then
echo "Cách dùng: $0 <tên-service> [dev|deploy|reset]"
echo "Ví dụ: $0 iam-service dev"
echo "Ví dụ: $0 iam-service deploy"
echo "Ví dụ: $0 iam-service reset"
exit 1
fi
# VI: Kiểm tra thư mục service có tồn tại không
if [ ! -d "services/$SERVICE" ]; then
echo "❌ Không tìm thấy service $SERVICE"
exit 1
fi
echo "🔄 Chạy migrations cho $SERVICE..."
cd "services/$SERVICE"
# VI: Load biến môi trường (hybrid pattern)
# VI: 1. Load shared env (JWT secrets, Redis config)
if [ -f "../../deployments/local/.env.local" ]; then
set -a
source ../../deployments/local/.env.local
set +a
fi
# VI: 2. Load service-specific env (DATABASE_URL, PORT, etc.)
if [ -f ".env.local" ]; then
set -a
source .env.local
set +a
fi
# VI: Kiểm tra DATABASE_URL có được thiết lập không
if [ -z "$DATABASE_URL" ]; then
echo "⚠️ DATABASE_URL chưa được thiết lập. Vui lòng kiểm tra file .env.local:"
echo " - Shared config: ../../deployments/local/.env.local"
echo " - Service config: .env.local"
echo " Ví dụ Neon: postgresql://user:pass@ep-xxx.region.neon.tech/dbname?sslmode=require"
exit 1
fi
# VI: Thực hiện migration dựa trên mode
if [ "$MODE" = "dev" ]; then
echo "📝 Chạy development migration (tạo migration mới)..."
pnpm prisma migrate dev
elif [ "$MODE" = "deploy" ]; then
echo "🚀 Chạy production migration (áp dụng migrations có sẵn)..."
pnpm prisma migrate deploy
pnpm prisma generate
elif [ "$MODE" = "reset" ]; then
echo "⚠️ CẢNH BÁO: Sẽ xóa toàn bộ database và chạy lại migrations!"
read -p "Bạn có chắc chắn? (yes/no): " confirm
if [ "$confirm" = "yes" ]; then
echo "🔄 Reset database..."
pnpm prisma migrate reset --force
else
echo "❌ Hủy reset database"
exit 0
fi
else
echo "⚠️ Mode không hợp lệ. Sử dụng: dev, deploy, hoặc reset"
exit 1
fi
echo "✅ Migrations hoàn tất!"