| @@ -1,5 +1,5 @@ |
| 1 | 1 | from enum import Enum |
| 2 | | -from typing import Annotated |
| 2 | +from typing import Annotated, Iterable |
| 3 | 3 | from pathlib import Path |
| 4 | 4 | from contextlib import nullcontext |
| 5 | 5 | import os |
| @@ -21,38 +21,39 @@ |
| 21 | 21 | |
| 22 | 22 | app = typer.Typer() |
| 23 | 23 | |
| 24 | | -@app.command() |
| 25 | | -def deploy( |
| 26 | | - migration_ids: list[str], |
| 27 | | - root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)], |
| 28 | | - database_url: Annotated[str | None, typer.Option("--database", "-d", help="Database connection URL.")] = None, |
| 29 | | - if_modified: Annotated[IfModifiedOption, typer.Option("--if-modified", "-m", help="Action to take if a migration has been modified since its last deployment.", case_sensitive=False, show_choices=True)] = IfModifiedOption.PROMPT, |
| 30 | | -) -> None: |
| 31 | | - """Deploy the specified migrations. |
| 32 | 24 | |
| 33 | | - Args: |
| 34 | | - migration_ids: The IDs of the migrations to deploy. |
| 35 | | - """ |
| 36 | | - |
| 37 | | - # Resolve database URL |
| 25 | +def _resolve_database_url(database_url: str | None) -> str: |
| 38 | 26 | resolved_database_url = database_url or os.getenv("MITCH_DATABASE_URL") or os.getenv("DATABASE_URL") |
| 39 | 27 | if not resolved_database_url: |
| 40 | | - typer.echo("Error: Database URL must be provided via --database option or MITCH_DATABASE_URL/DATABASE_URL environment variable.", err=True) |
| 28 | + typer.echo( |
| 29 | + "Error: Database URL must be provided via --database option or MITCH_DATABASE_URL/DATABASE_URL environment variable.", |
| 30 | + err=True, |
| 31 | + ) |
| 41 | 32 | raise typer.Exit(1) |
| 33 | + return resolved_database_url |
| 34 | + |
| 35 | + |
| 36 | +def _deploy_migrations( |
| 37 | + repo: Repository, |
| 38 | + migration_ids: Iterable[str], |
| 39 | + database_url: str | None, |
| 40 | + if_modified: IfModifiedOption, |
| 41 | +) -> None: |
| 42 | + migration_ids = list(migration_ids) |
| 43 | + resolved_database_url = _resolve_database_url(database_url) |
| 42 | 44 | engine = sa.create_engine(resolved_database_url) |
| 43 | | - |
| 45 | + |
| 44 | 46 | # Fetch due migrations |
| 45 | | - repo = Repository(root) |
| 46 | 47 | due_migrations = list(repo.by_ids_with_dependencies(migration_ids)) |
| 47 | | - |
| 48 | + |
| 48 | 49 | with engine.connect() as connection: |
| 49 | 50 | runner = Runner(connection) |
| 50 | | - |
| 51 | + |
| 51 | 52 | # Check for modified migrations |
| 52 | 53 | modified_migrations = [m for m in due_migrations if runner.deployment_status(m) == 'modified'] |
| 53 | 54 | migrations_to_redeploy = [] |
| 54 | 55 | migrations_to_ignore = set() |
| 55 | | - |
| 56 | + |
| 56 | 57 | match if_modified: |
| 57 | 58 | case IfModifiedOption.REDEPLOY: |
| 58 | 59 | migrations_to_redeploy = modified_migrations[:] |
| @@ -82,16 +83,16 @@ |
| 82 | 83 | rich.print(f" - Migration '{m.id}'") |
| 83 | 84 | if not Confirm.ask("Do you want to continue?", default=False): |
| 84 | 85 | raise typer.Exit(0) |
| 85 | | - |
| 86 | + |
| 86 | 87 | for m in migrations_to_revert: |
| 87 | 88 | rich.print(f"[blue]Reverting:[/blue] Migration '{m.id}'") |
| 88 | 89 | runner.revert_migration(m) |
| 89 | 90 | |
| 90 | | - # Prepare to redeploy modified migrations, together with the |
| 91 | + # Prepare to redeploy modified migrations, together with the |
| 91 | 92 | if migrations_to_revert: |
| 92 | 93 | due_ids = set(migration_ids) | {m.id for m in migrations_to_revert} |
| 93 | 94 | due_migrations = list(repo.by_ids_with_dependencies(due_ids)) |
| 94 | | - |
| 95 | + |
| 95 | 96 | # Deploy due and reverted migrations |
| 96 | 97 | for batch in batch_transactional_migrations(due_migrations): |
| 97 | 98 | runner.autocommit = not batch.transactional |
| @@ -102,5 +103,35 @@ |
| 102 | 103 | continue |
| 103 | 104 | rich.print(f"[green]Deploying:[/green] Migration '{m.id}'") |
| 104 | 105 | runner.apply_migration(m) |
| 105 | | - |
| 106 | + |
| 106 | 107 | engine.dispose() |
| 108 | + |
| 109 | + |
| 110 | +@app.command() |
| 111 | +def deploy( |
| 112 | + migration_ids: list[str], |
| 113 | + root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)], |
| 114 | + database_url: Annotated[str | None, typer.Option("--database", "-d", help="Database connection URL.")] = None, |
| 115 | + if_modified: Annotated[IfModifiedOption, typer.Option("--if-modified", "-m", help="Action to take if a migration has been modified since its last deployment.", case_sensitive=False, show_choices=True)] = IfModifiedOption.PROMPT, |
| 116 | +) -> None: |
| 117 | + """Deploy the specified migrations. |
| 118 | + |
| 119 | + Args: |
| 120 | + migration_ids: The IDs of the migrations to deploy. |
| 121 | + """ |
| 122 | + |
| 123 | + repo = Repository(root) |
| 124 | + _deploy_migrations(repo, migration_ids, database_url, if_modified) |
| 125 | + |
| 126 | + |
| 127 | +@app.command(name="up") |
| 128 | +@app.command(name="apply", help="Alias for up.") |
| 129 | +def up( |
| 130 | + root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)], |
| 131 | + database_url: Annotated[str | None, typer.Option("--database", "-d", help="Database connection URL.")] = None, |
| 132 | + if_modified: Annotated[IfModifiedOption, typer.Option("--if-modified", "-m", help="Action to take if a migration has been modified since its last deployment.", case_sensitive=False, show_choices=True)] = IfModifiedOption.PROMPT, |
| 133 | +) -> None: |
| 134 | + """Deploy all migrations in the repository.""" |
| 135 | + repo = Repository(root) |
| 136 | + migration_ids = [m.id for m in repo.migrations] |
| 137 | + _deploy_migrations(repo, migration_ids, database_url, if_modified) |