This commit is contained in:
Ho Ngoc Hai
2025-12-27 01:31:10 +07:00
commit 4da46b5b8e
205 changed files with 21063 additions and 0 deletions

61
scripts/db/backup.sh Executable file
View File

@@ -0,0 +1,61 @@
#!/bin/bash
set -e
SERVICE=$1
BACKUP_DIR="${2:-./backups}"
if [ -z "$SERVICE" ]; then
echo "Usage: $0 <service-name> [backup-dir]"
echo "Example: $0 auth-service"
exit 1
fi
mkdir -p "$BACKUP_DIR"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/${SERVICE}_${TIMESTAMP}.sql"
echo "💾 Backing up database for $SERVICE..."
# Extract database URL from .env or environment
cd "services/$SERVICE"
if [ -z "$DATABASE_URL" ]; then
if [ -f ".env" ]; then
DATABASE_URL=$(grep DATABASE_URL .env | cut -d '=' -f2- | tr -d '"' | tr -d "'")
elif [ -f "../../deployments/local/.env.local" ]; then
DATABASE_URL=$(grep DATABASE_URL ../../deployments/local/.env.local | cut -d '=' -f2- | tr -d '"' | tr -d "'")
fi
fi
if [ -z "$DATABASE_URL" ]; then
echo "❌ DATABASE_URL not found. Please set it in:"
echo " - services/$SERVICE/.env"
echo " - deployments/local/.env.local"
echo " - Or as environment variable"
exit 1
fi
# Use pg_dump directly with DATABASE_URL (works with Neon and standard PostgreSQL)
# Neon URLs format: postgresql://user:pass@ep-xxx.region.neon.tech/dbname?sslmode=require
# Standard format: postgresql://user:pass@host:port/dbname
# Check if pg_dump supports connection string (PostgreSQL 11+)
if pg_dump --version | grep -qE "1[1-9]|2[0-9]"; then
# Use connection string directly (supports Neon)
pg_dump "$DATABASE_URL" > "../$BACKUP_FILE"
else
# Fallback: Parse connection string for older pg_dump
# Remove query parameters for parsing
CLEAN_URL=$(echo $DATABASE_URL | sed 's/?.*//')
DB_USER=$(echo $CLEAN_URL | sed -n 's/.*:\/\/\([^:]*\):.*/\1/p')
DB_PASS=$(echo $CLEAN_URL | sed -n 's/.*:\/\/[^:]*:\([^@]*\)@.*/\1/p')
DB_HOST=$(echo $CLEAN_URL | sed -n 's/.*@\([^:]*\):.*/\1/p')
DB_PORT=$(echo $CLEAN_URL | sed -n 's/.*:\([0-9]*\)\/.*/\1/p')
DB_NAME=$(echo $CLEAN_URL | sed -n 's/.*\/\([^?]*\).*/\1/p')
PGPASSWORD=$DB_PASS pg_dump -h $DB_HOST -p ${DB_PORT:-5432} -U $DB_USER -d $DB_NAME > "../$BACKUP_FILE"
fi
echo "✅ Backup created: $BACKUP_FILE"

51
scripts/db/migrate.sh Executable file
View File

@@ -0,0 +1,51 @@
#!/bin/bash
set -e
SERVICE=$1
if [ -z "$SERVICE" ]; then
echo "Usage: $0 <service-name> [dev|deploy]"
echo "Example: $0 auth-service dev"
echo "Example: $0 auth-service deploy"
exit 1
fi
if [ ! -d "services/$SERVICE" ]; then
echo "❌ Service $SERVICE not found"
exit 1
fi
echo "🔄 Running migrations for $SERVICE..."
cd "services/$SERVICE"
# Load environment variables (hybrid pattern)
# 1. Load shared env (JWT secrets, Redis config)
if [ -f "../../deployments/local/.env.local" ]; then
export $(grep -v '^#' ../../deployments/local/.env.local | xargs)
fi
# 2. Load service-specific env (DATABASE_URL, PORT, etc.)
if [ -f ".env.local" ]; then
export $(grep -v '^#' .env.local | xargs)
fi
# Check if DATABASE_URL is set
if [ -z "$DATABASE_URL" ]; then
echo "⚠️ DATABASE_URL not set. Please check your .env.local files:"
echo " - Shared config: ../../deployments/local/.env.local"
echo " - Service config: .env.local"
echo " For Neon: postgresql://user:pass@ep-xxx.region.neon.tech/dbname?sslmode=require&pgbouncer=true"
exit 1
fi
if [ "$2" = "dev" ]; then
echo "📝 Running development migration (creates new migration)..."
pnpm prisma migrate dev
else
echo "🚀 Running production migration (applies existing migrations)..."
pnpm prisma migrate deploy
fi
echo "✅ Migrations completed!"

36
scripts/db/seed.sh Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/bash
set -e
SERVICE=$1
if [ -z "$SERVICE" ]; then
echo "Usage: $0 <service-name>"
echo "Example: $0 auth-service"
exit 1
fi
if [ ! -d "services/$SERVICE" ]; then
echo "❌ Service $SERVICE not found"
exit 1
fi
echo "🌱 Seeding database for $SERVICE..."
cd "services/$SERVICE"
# Check if DATABASE_URL is set
if [ -z "$DATABASE_URL" ]; then
if [ -f ".env" ]; then
export $(grep -v '^#' .env | xargs)
elif [ -f "../../deployments/local/.env.local" ]; then
export $(grep -v '^#' ../../deployments/local/.env.local | xargs)
else
echo "⚠️ DATABASE_URL not set. Please set it in .env or environment variable."
exit 1
fi
fi
pnpm prisma:seed
echo "✅ Database seeded!"

69
scripts/db/setup-neon.sh Executable file
View File

@@ -0,0 +1,69 @@
#!/bin/bash
set -e
echo "🚀 Neon Database Setup Script"
echo ""
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}This script helps you set up Neon database for all environments.${NC}"
echo ""
# Check if .env.local exists
ENV_LOCAL="deployments/local/.env.local"
if [ ! -f "$ENV_LOCAL" ]; then
echo "📝 Creating $ENV_LOCAL from example..."
cp deployments/local/env.local.example "$ENV_LOCAL"
echo -e "${GREEN}✅ Created $ENV_LOCAL${NC}"
echo ""
fi
echo "📋 Setup Steps:"
echo ""
echo "1. Go to https://console.neon.tech"
echo "2. Create a project (or use existing): goodgo-platform"
echo "3. Create branches:"
echo " - staging (from main)"
echo " - production (from main)"
echo ""
echo "4. Get connection strings for each branch from Neon Console"
echo " Format: postgresql://user:password@ep-xxx.region.neon.tech/dbname?sslmode=require&pgbouncer=true"
echo ""
read -p "Enter development DATABASE_URL (main branch): " dev_url
if [ -n "$dev_url" ]; then
# Update .env.local
if grep -q "DATABASE_URL=" "$ENV_LOCAL"; then
sed -i.bak "s|DATABASE_URL=.*|DATABASE_URL=$dev_url|" "$ENV_LOCAL"
rm -f "${ENV_LOCAL}.bak"
else
echo "DATABASE_URL=$dev_url" >> "$ENV_LOCAL"
fi
echo -e "${GREEN}✅ Updated $ENV_LOCAL${NC}"
fi
echo ""
echo "📝 Next Steps:"
echo ""
echo "1. Add to GitHub Secrets (for CI/CD):"
echo " - NEON_DATABASE_URL_STAGING"
echo " - NEON_DATABASE_URL_PRODUCTION"
echo " - NEON_DATABASE_URL_TEST (optional, for CI tests)"
echo ""
echo "2. Create Kubernetes secrets for staging/production:"
echo " See: deployments/staging/kubernetes/secrets.yaml.example"
echo " See: deployments/production/kubernetes/secrets.yaml.example"
echo ""
echo "3. Run initial migration:"
echo " ./scripts/db/migrate.sh auth-service dev"
echo ""
echo "4. Seed database (optional):"
echo " ./scripts/db/seed.sh auth-service"
echo ""
echo -e "${GREEN}✅ Setup instructions complete!${NC}"
echo ""
echo "For more details, see: infra/databases/neon/README.md"