# PowerShell script to run database migrations using Docker Compose # # Usage: # .\scripts\run_migrations.ps1 [command] # # Commands: # upgrade - Run all pending migrations (default) # downgrade - Rollback one migration # current - Show current migration version # history - Show migration history # create - Create a new migration (requires description) # param( [Parameter(Position=0)] [string]$Command = "upgrade", [Parameter(Position=1)] [string]$Description = "" ) # Check if docker-compose is available if (-not (Get-Command docker-compose -ErrorAction SilentlyContinue)) { Write-Host "Error: docker-compose is not installed" -ForegroundColor Red exit 1 } Write-Host "===========================================================" -ForegroundColor Blue Write-Host "Database Migration Tool" -ForegroundColor Blue Write-Host "===========================================================" -ForegroundColor Blue Write-Host "" switch ($Command) { "upgrade" { Write-Host "Running migrations..." -ForegroundColor Green docker-compose run --rm migrations if ($LASTEXITCODE -eq 0) { Write-Host "`n✓ Migrations completed successfully!" -ForegroundColor Green } else { Write-Host "`n✗ Migration failed!" -ForegroundColor Red exit 1 } } "downgrade" { Write-Host "Rolling back one migration..." -ForegroundColor Yellow docker-compose run --rm migrations sh -c "uv run alembic downgrade -1" if ($LASTEXITCODE -eq 0) { Write-Host "`n✓ Rollback completed!" -ForegroundColor Green } else { Write-Host "`n✗ Rollback failed!" -ForegroundColor Red exit 1 } } "current" { Write-Host "Current migration version:" -ForegroundColor Blue docker-compose run --rm migrations sh -c "uv run alembic current" } "history" { Write-Host "Migration history:" -ForegroundColor Blue docker-compose run --rm migrations sh -c "uv run alembic history" } "create" { if ([string]::IsNullOrWhiteSpace($Description)) { Write-Host "Error: Migration description required" -ForegroundColor Red Write-Host "Usage: .\scripts\run_migrations.ps1 create `"your migration description`"" exit 1 } Write-Host "Creating new migration: $Description" -ForegroundColor Green docker-compose run --rm migrations sh -c "uv run alembic revision --autogenerate -m `"$Description`"" if ($LASTEXITCODE -eq 0) { Write-Host "`n✓ Migration created!" -ForegroundColor Green } else { Write-Host "`n✗ Migration creation failed!" -ForegroundColor Red exit 1 } } { $_ -in "help", "--help", "-h" } { Write-Host "Database Migration Tool" -ForegroundColor Cyan Write-Host "" Write-Host "Usage: .\scripts\run_migrations.ps1 [command] [args]" Write-Host "" Write-Host "Commands:" Write-Host " upgrade Run all pending migrations (default)" Write-Host " downgrade Rollback one migration" Write-Host " current Show current migration version" Write-Host " history Show migration history" Write-Host " create Create a new migration" Write-Host " help Show this help message" Write-Host "" Write-Host "Examples:" Write-Host " .\scripts\run_migrations.ps1 # Run migrations" Write-Host " .\scripts\run_migrations.ps1 upgrade # Run migrations" Write-Host " .\scripts\run_migrations.ps1 downgrade # Rollback" Write-Host " .\scripts\run_migrations.ps1 current # Show version" Write-Host " .\scripts\run_migrations.ps1 history # Show history" Write-Host " .\scripts\run_migrations.ps1 create `"add user table`" # Create migration" } default { Write-Host "Error: Unknown command '$Command'" -ForegroundColor Red Write-Host "Run '.\scripts\run_migrations.ps1 help' for usage information" exit 1 } } Write-Host ""