#!/bin/bash # Script to run Alembic migrations for AudiOhm backend # Usage: ./run_migration.sh [command] set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Change to backend directory cd "$(dirname "$0")" echo -e "${GREEN}=== AudiOhm Alembic Migration Tool ===${NC}\n" # Check if .env exists if [ ! -f .env ]; then echo -e "${RED}Error: .env file not found!${NC}" echo "Please copy .env.example to .env and configure it first." exit 1 fi # Function to show help show_help() { echo "Usage: $0 [command]" echo "" echo "Commands:" echo " current Show current migration version" echo " history Show migration history" echo " heads Show migration heads" echo " status Show current status" echo " upgrade Apply all pending migrations" echo " upgrade+1 Apply next migration only" echo " downgrade-1 Revert last migration" echo " downgrade Revert all migrations (to base)" echo " show [id] Show details of a migration" echo " create Create a new migration (requires -m message)" echo " sql-upgrade Show SQL for upgrade without executing" echo " sql-downgrade Show SQL for downgrade without executing" echo "" echo "Examples:" echo " $0 current" echo " $0 upgrade" echo " $0 downgrade-1" echo " $0 create -m 'Add new table'" echo " $0 show 001_add_library_tables" echo "" } # Function to check if PostgreSQL is running check_postgres() { if ! pg_isready -h localhost -p 5432 > /dev/null 2>&1; then echo -e "${YELLOW}Warning: PostgreSQL might not be running${NC}" echo "Please start PostgreSQL service first" return 1 fi return 0 } # Parse command case "$1" in current) echo "Showing current migration version..." check_postgres alembic current ;; history) echo "Showing migration history..." alembic history ;; heads) echo "Showing migration heads..." alembic heads ;; status) echo "Showing migration status..." check_postgres alembic current echo "" alembic heads ;; upgrade) echo -e "${YELLOW}Applying all pending migrations...${NC}" check_postgres alembic upgrade head echo -e "${GREEN}✓ Migrations applied successfully!${NC}" ;; upgrade+1) echo -e "${YELLOW}Applying next migration...${NC}" check_postgres alembic upgrade +1 echo -e "${GREEN}✓ Migration applied successfully!${NC}" ;; downgrade-1) echo -e "${YELLOW}Reverting last migration...${NC}" check_postgres alembic downgrade -1 echo -e "${GREEN}✓ Migration reverted successfully!${NC}" ;; downgrade) echo -e "${RED}WARNING: This will revert ALL migrations!${NC}" read -p "Are you sure? (yes/no): " confirm if [ "$confirm" = "yes" ]; then check_postgres alembic downgrade base echo -e "${GREEN}✓ All migrations reverted!${NC}" else echo "Aborted." fi ;; show) if [ -z "$2" ]; then echo "Error: Please provide a migration ID" echo "Usage: $0 show " exit 1 fi echo "Showing migration details for: $2" alembic show "$2" ;; create) shift echo "Creating new migration..." alembic revision "$@" echo -e "${GREEN}✓ New migration file created!${NC}" echo "Edit the file in alembic/versions/ and then run: $0 upgrade" ;; sql-upgrade) echo "Showing SQL for upgrade (not executing)..." check_postgres alembic upgrade head --sql ;; sql-downgrade) echo "Showing SQL for downgrade (not executing)..." check_postgres alembic downgrade -1 --sql ;; help|--help|-h) show_help ;; *) echo -e "${RED}Error: Unknown command '$1'${NC}\n" show_help exit 1 ;; esac echo ""