63 lines
1.8 KiB
Bash
Executable File
63 lines
1.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# ============================================================================
|
|
# Database Migration Script
|
|
# Run a specific SQL migration file against the database
|
|
# ============================================================================
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Load environment variables
|
|
if [ -f ../.env ]; then
|
|
export $(cat ../.env | grep -v '^#' | xargs)
|
|
else
|
|
echo -e "${RED}Error: .env file not found${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if migration file is provided
|
|
if [ -z "$1" ]; then
|
|
echo -e "${YELLOW}Usage: ./run-migration.sh <migration-file.sql>${NC}"
|
|
echo ""
|
|
echo "Available migrations:"
|
|
ls -1 ../sql/*.sql | grep -v schema.sql
|
|
exit 1
|
|
fi
|
|
|
|
MIGRATION_FILE=$1
|
|
|
|
# Check if file exists
|
|
if [ ! -f "../sql/$MIGRATION_FILE" ]; then
|
|
echo -e "${RED}Error: Migration file not found: ../sql/$MIGRATION_FILE${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Construct database URL
|
|
DB_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
|
|
|
|
echo -e "${YELLOW}========================================${NC}"
|
|
echo -e "${YELLOW}Running migration: $MIGRATION_FILE${NC}"
|
|
echo -e "${YELLOW}Database: $DB_NAME${NC}"
|
|
echo -e "${YELLOW}========================================${NC}"
|
|
echo ""
|
|
|
|
# Run the migration
|
|
psql "$DB_URL" -f "../sql/$MIGRATION_FILE"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo ""
|
|
echo -e "${GREEN}========================================${NC}"
|
|
echo -e "${GREEN}✓ Migration completed successfully!${NC}"
|
|
echo -e "${GREEN}========================================${NC}"
|
|
else
|
|
echo ""
|
|
echo -e "${RED}========================================${NC}"
|
|
echo -e "${RED}✗ Migration failed!${NC}"
|
|
echo -e "${RED}========================================${NC}"
|
|
exit 1
|
|
fi
|