Spaces:
Paused
Paused
Download backend/cli/commands/migrate.py from data-llm-hub/tensorwall: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/spaces/data-llm-hub/tensorwall/resolve/main/backend/cli/commands/migrate.py
- Command line
-
hf download hf://spaces/data-llm-hub/tensorwall/backend/cli/commands/migrate.py
-
curl -L -o migrate.py https://huggingface.co/spaces/data-llm-hub/tensorwall/resolve/main/backend/cli/commands/migrate.py
2.93 kB
| """Database migration commands (Alembic wrapper).""" | |
| import os | |
| import typer | |
| from rich.console import Console | |
| console = Console() | |
| app = typer.Typer(help="Database migration management.") | |
| def get_alembic_config(): | |
| """Get Alembic configuration.""" | |
| from alembic.config import Config | |
| # Find alembic.ini | |
| backend_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | |
| ini_path = os.path.join(backend_dir, "alembic.ini") | |
| if not os.path.exists(ini_path): | |
| console.print(f"[red]Error:[/red] alembic.ini not found at {ini_path}") | |
| raise typer.Exit(1) | |
| config = Config(ini_path) | |
| return config | |
| def upgrade( | |
| revision: str = typer.Argument("head", help="Target revision"), | |
| sql: bool = typer.Option(False, "--sql", help="Show SQL without applying"), | |
| ): | |
| """Apply database migrations.""" | |
| from alembic import command | |
| config = get_alembic_config() | |
| if sql: | |
| console.print(f"[cyan]Generating SQL for upgrade to {revision}...[/cyan]") | |
| command.upgrade(config, revision, sql=True) | |
| else: | |
| console.print(f"[cyan]Upgrading database to {revision}...[/cyan]") | |
| command.upgrade(config, revision) | |
| console.print("[green]Migration complete.[/green]") | |
| def downgrade( | |
| revision: str = typer.Argument(..., help="Target revision (use -1 for previous)"), | |
| sql: bool = typer.Option(False, "--sql", help="Show SQL without applying"), | |
| ): | |
| """Rollback database migrations.""" | |
| from alembic import command | |
| config = get_alembic_config() | |
| if sql: | |
| console.print(f"[cyan]Generating SQL for downgrade to {revision}...[/cyan]") | |
| command.downgrade(config, revision, sql=True) | |
| else: | |
| console.print(f"[yellow]Downgrading database to {revision}...[/yellow]") | |
| command.downgrade(config, revision) | |
| console.print("[green]Downgrade complete.[/green]") | |
| def current(): | |
| """Show current migration revision.""" | |
| from alembic import command | |
| config = get_alembic_config() | |
| console.print("[cyan]Current revision:[/cyan]") | |
| command.current(config, verbose=True) | |
| def history(): | |
| """Show migration history.""" | |
| from alembic import command | |
| config = get_alembic_config() | |
| console.print("[cyan]Migration history:[/cyan]") | |
| command.history(config, verbose=True) | |
| def revision( | |
| message: str = typer.Option(..., "-m", "--message", help="Revision message"), | |
| autogenerate: bool = typer.Option( | |
| True, "--autogenerate/--no-autogenerate", help="Auto-detect model changes" | |
| ), | |
| ): | |
| """Create a new migration revision.""" | |
| from alembic import command | |
| config = get_alembic_config() | |
| console.print(f"[cyan]Creating revision: {message}[/cyan]") | |
| command.revision(config, message=message, autogenerate=autogenerate) | |
| console.print("[green]Revision created.[/green]") | |