| @@ -1,13 +1,14 @@ |
| 1 | 1 | from enum import Enum |
| 2 | | -from typing import Annotated |
| 2 | +from typing import Annotated, Callable, Iterable |
| 3 | 3 | from pathlib import Path |
| 4 | 4 | |
| 5 | 5 | from rich.prompt import Confirm |
| 6 | 6 | import typer |
| 7 | 7 | import rich |
| 8 | 8 | |
| 9 | +from ..migration import Migration |
| 9 | 10 | from ..repository import Repository |
| 10 | | -from ..runner import Runner |
| 11 | +from ..runner import Runner, DeploymentStatus |
| 11 | 12 | from ..helpers import connection_for |
| 12 | 13 | from ..watcher import EventType |
| 13 | 14 | |
| @@ -19,6 +20,163 @@ |
| 19 | 20 | |
| 20 | 21 | |
| 21 | 22 | app = typer.Typer() |
| 23 | + |
| 24 | + |
| 25 | +def _resolve_if_modified_policy( |
| 26 | + if_modified: IfModifiedOption | None, |
| 27 | + watch: bool, |
| 28 | +) -> IfModifiedOption: |
| 29 | + if if_modified is not None: |
| 30 | + return if_modified |
| 31 | + if watch: |
| 32 | + return IfModifiedOption.REDEPLOY |
| 33 | + |
| 34 | + console = rich.get_console() |
| 35 | + if console.is_interactive and console.is_terminal: |
| 36 | + return IfModifiedOption.PROMPT |
| 37 | + return IfModifiedOption.ABORT |
| 38 | + |
| 39 | + |
| 40 | +def _collect_redeploy_migrations( |
| 41 | + runner: Runner, |
| 42 | + covered_migrations: set[Migration], |
| 43 | + if_modified: IfModifiedOption, |
| 44 | +) -> set[Migration]: |
| 45 | + if if_modified == IfModifiedOption.IGNORE: |
| 46 | + return set() |
| 47 | + |
| 48 | + redeploy = set( |
| 49 | + migration |
| 50 | + for migration in covered_migrations |
| 51 | + if runner.deployment_status(migration) == "modified" |
| 52 | + ) |
| 53 | + |
| 54 | + if redeploy and if_modified == IfModifiedOption.ABORT: |
| 55 | + rich.print("[red]Error:[/red] The following migrations have been modified since their last deployment:") |
| 56 | + for migration in redeploy: |
| 57 | + rich.print(f" - Migration '{migration.id}'") |
| 58 | + raise typer.Abort("Aborting due to modified migrations.") |
| 59 | + |
| 60 | + if redeploy and if_modified == IfModifiedOption.PROMPT: |
| 61 | + for migration in redeploy.copy(): |
| 62 | + rich.print( |
| 63 | + f"[yellow]Warning:[/yellow] Migration '{migration.id}' has been modified since it was last deployed." |
| 64 | + ) |
| 65 | + match Confirm.ask( |
| 66 | + "(r)evert and redeploy, (i)gnore, or (e)exit?", |
| 67 | + choices=["r", "i", "e"], |
| 68 | + default="e", |
| 69 | + ): |
| 70 | + case "r": |
| 71 | + continue |
| 72 | + case "i": |
| 73 | + redeploy.remove(migration) |
| 74 | + case "e": |
| 75 | + raise typer.Abort() |
| 76 | + |
| 77 | + return redeploy |
| 78 | + |
| 79 | + |
| 80 | +def _revert_with_feedback( |
| 81 | + runner: Runner, |
| 82 | + migrations: list[Migration], |
| 83 | + message_builder: Callable[[Migration, DeploymentStatus], str], |
| 84 | +) -> None: |
| 85 | + runner.revert_migrations( |
| 86 | + migrations, |
| 87 | + before_each=lambda migration, status: rich.print( |
| 88 | + message_builder(migration, status) |
| 89 | + ), |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def _deploy_with_feedback( |
| 94 | + runner: Runner, |
| 95 | + migrations: list[Migration], |
| 96 | + message_builder: Callable[[Migration, DeploymentStatus], str], |
| 97 | +) -> None: |
| 98 | + runner.deploy_migrations( |
| 99 | + migrations, |
| 100 | + before_each=lambda migration, status: rich.print( |
| 101 | + message_builder(migration, status) |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def _apply_planned_migrations( |
| 107 | + repo: Repository, |
| 108 | + runner: Runner, |
| 109 | + covered_migrations: set[Migration], |
| 110 | + redeploy: set[Migration], |
| 111 | +) -> None: |
| 112 | + to_revert = list(repo.with_dependants(redeploy)) |
| 113 | + _revert_with_feedback( |
| 114 | + runner, |
| 115 | + to_revert, |
| 116 | + lambda migration, status: ( |
| 117 | + f"[blue]Reverting:[/blue] {status} migration '{migration.id}' due to modifications after deployment." |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + pending = { |
| 122 | + migration |
| 123 | + for migration in covered_migrations |
| 124 | + if runner.deployment_status(migration) == "pending" |
| 125 | + } |
| 126 | + to_apply = list(repo.with_dependencies(pending | redeploy)) |
| 127 | + _deploy_with_feedback( |
| 128 | + runner, |
| 129 | + to_apply, |
| 130 | + lambda migration, status: ( |
| 131 | + f"[green]Deploying:[/green] Migration '{migration.id}' ({status})" |
| 132 | + ), |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +def _process_watch_changes( |
| 137 | + repo: Repository, |
| 138 | + runner: Runner, |
| 139 | + covered_migrations: set[Migration], |
| 140 | + changes: Iterable[tuple[EventType, Path]], |
| 141 | + allow_new_migrations: bool, |
| 142 | +) -> None: |
| 143 | + # Reload repository after receiving events so created files are visible in the index. |
| 144 | + repo.reload() |
| 145 | + |
| 146 | + applied_and_modified = set() |
| 147 | + added_and_pending = set() |
| 148 | + for action, path in changes: |
| 149 | + if action == EventType.MODIFIED and ( |
| 150 | + path.name.endswith(".up.sql") or path.name.endswith(".config.toml") |
| 151 | + ): |
| 152 | + migration = repo.by_path(path) |
| 153 | + if migration not in covered_migrations: |
| 154 | + continue |
| 155 | + if runner.deployment_status(migration) in ("modified", "applied"): |
| 156 | + applied_and_modified.add(migration) |
| 157 | + |
| 158 | + if allow_new_migrations and action == EventType.CREATED and path.name.endswith(".up.sql"): |
| 159 | + migration = repo.by_path(path) |
| 160 | + if runner.deployment_status(migration) == "pending": |
| 161 | + added_and_pending.add(migration) |
| 162 | + |
| 163 | + to_revert = list(repo.with_dependants(applied_and_modified)) |
| 164 | + _revert_with_feedback( |
| 165 | + runner, |
| 166 | + to_revert, |
| 167 | + lambda migration, status: ( |
| 168 | + f"[blue]Reverted:[/blue] Migration '{migration.id}' ({status})" |
| 169 | + ), |
| 170 | + ) |
| 171 | + |
| 172 | + to_apply = list(repo.with_dependencies(applied_and_modified | added_and_pending)) |
| 173 | + _deploy_with_feedback( |
| 174 | + runner, |
| 175 | + to_apply, |
| 176 | + lambda migration, status: ( |
| 177 | + f"[green]Deployed:[/green] Migration '{migration.id}' ({status})" |
| 178 | + ), |
| 179 | + ) |
| 22 | 180 | |
| 23 | 181 | |
| 24 | 182 | @app.command(name="add") |
| @@ -47,112 +205,30 @@ |
| 47 | 205 | |
| 48 | 206 | repo = Repository(root) |
| 49 | 207 | covered_migrations = set(repo.by_ids_with_dependencies(migration_ids)) |
| 50 | | - |
| 51 | | - # Choose default behavior for modified migrations based on whether watch mode is enabled... |
| 52 | | - if if_modified is None and watch: |
| 53 | | - if_modified = IfModifiedOption.REDEPLOY |
| 54 | | - |
| 55 | | - # ...and whether we're in an interactive terminal. |
| 56 | | - if if_modified is None: |
| 57 | | - console = rich.get_console() |
| 58 | | - if console.is_interactive and console.is_terminal: |
| 59 | | - if_modified = IfModifiedOption.PROMPT |
| 60 | | - else: |
| 61 | | - if_modified = IfModifiedOption.ABORT |
| 208 | + allow_new_migrations = ctx.info_name == "up" |
| 209 | + |
| 210 | + if_modified = _resolve_if_modified_policy(if_modified, watch) |
| 62 | 211 | |
| 63 | 212 | with connection_for(database_url) as connection: |
| 64 | 213 | runner = Runner(connection) |
| 65 | | - |
| 66 | | - # If the option is to ignore modifications, we won't redeploy any modified migrations. |
| 67 | | - if if_modified == IfModifiedOption.IGNORE: |
| 68 | | - redeploy = set() |
| 69 | | - |
| 70 | | - # In other cases, we prepare to redeploy all modified migrations, |
| 71 | | - # but for the prompt case we will confirm with the user before actually doing it. |
| 72 | | - else: |
| 73 | | - redeploy = set(m for m in covered_migrations if runner.deployment_status(m) == 'modified') |
| 74 | | - |
| 75 | | - # Abort if there are modified migrations and the option is set to abort. Especially on CI where prompting is not possible. |
| 76 | | - if redeploy and if_modified == IfModifiedOption.ABORT: |
| 77 | | - rich.print("[red]Error:[/red] The following migrations have been modified since their last deployment:") |
| 78 | | - for m in redeploy: |
| 79 | | - rich.print(f" - Migration '{m.id}'") |
| 80 | | - raise typer.Abort("Aborting due to modified migrations.") |
| 81 | | - |
| 82 | | - # If prompting is enabled, ask the user what to do with each modified migration. |
| 83 | | - if redeploy and if_modified == IfModifiedOption.PROMPT: |
| 84 | | - for migration in redeploy.copy(): |
| 85 | | - rich.print(f"[yellow]Warning:[/yellow] Migration '{migration.id}' has been modified since it was last deployed.") |
| 86 | | - match Confirm.ask(f"(r)evert and redeploy, (i)gnore, or (e)exit?", choices=["r", "i", "e"], default="e"): |
| 87 | | - case "r": |
| 88 | | - continue |
| 89 | | - case "i": |
| 90 | | - redeploy.remove(migration) |
| 91 | | - case "e": |
| 92 | | - raise typer.Abort() |
| 93 | | - |
| 94 | | - # Revert modified migrations and their dependants before redeploying. |
| 95 | | - to_revert = list(repo.with_dependants(redeploy)) |
| 96 | | - runner.revert_migrations( |
| 97 | | - to_revert, |
| 98 | | - before_each=lambda migration, status: rich.print( |
| 99 | | - f"[blue]Reverting:[/blue] {status} migration '{migration.id}' due to modifications after deployment." |
| 100 | | - ), |
| 101 | | - ) |
| 102 | | - |
| 103 | | - # Deploy pending migrations, including redeployments for modified ones. |
| 104 | | - pending = set(m for m in covered_migrations if runner.deployment_status(m) == 'pending') |
| 105 | | - to_apply = list(repo.with_dependencies(pending | redeploy)) |
| 106 | | - runner.deploy_migrations( |
| 107 | | - to_apply, |
| 108 | | - before_each=lambda migration, status: rich.print( |
| 109 | | - f"[green]Deploying:[/green] Migration '{migration.id}' ({status})" |
| 110 | | - ), |
| 111 | | - ) |
| 112 | | - |
| 113 | | - if watch: |
| 114 | | - incoming_changes = repo.watch_for_changes() |
| 115 | | - |
| 116 | | - while watch: |
| 214 | + redeploy = _collect_redeploy_migrations(runner, covered_migrations, if_modified) |
| 215 | + _apply_planned_migrations(repo, runner, covered_migrations, redeploy) |
| 216 | + |
| 217 | + if not watch: |
| 218 | + return |
| 219 | + |
| 220 | + incoming_changes = repo.watch_for_changes() |
| 221 | + while True: |
| 117 | 222 | try: |
| 118 | | - changes = next(incoming_changes) # pyright: ignore[reportPossiblyUnboundVariable] |
| 223 | + changes = next(incoming_changes) |
| 119 | 224 | except StopIteration: |
| 120 | 225 | break |
| 121 | | - |
| 122 | | - # Reload repository after receiving events so created files are visible in the index. |
| 123 | | - repo.reload() |
| 124 | | - |
| 125 | | - # Collect modified and added migrations from the incoming changes. |
| 126 | | - applied_and_modified = set() |
| 127 | | - added_and_pending = set() |
| 128 | | - for action, path in changes: |
| 129 | | - if action == EventType.MODIFIED and (path.name.endswith(".up.sql") or path.name.endswith(".config.toml")): |
| 130 | | - migration = repo.by_path(path) |
| 131 | | - if migration not in covered_migrations: |
| 132 | | - continue |
| 133 | | - |
| 134 | | - if runner.deployment_status(migration) in ('modified', 'applied'): |
| 135 | | - applied_and_modified.add(migration) |
| 136 | | - |
| 137 | | - if ctx.info_name in ('up',) and action == EventType.CREATED and path.name.endswith(".up.sql"): |
| 138 | | - migration = repo.by_path(path) |
| 139 | | - if runner.deployment_status(migration) == 'pending': |
| 140 | | - added_and_pending.add(migration) |
| 141 | | - |
| 142 | | - to_revert = list(repo.with_dependants(applied_and_modified)) |
| 143 | | - runner.revert_migrations( |
| 144 | | - to_revert, |
| 145 | | - before_each=lambda migration, status: rich.print( |
| 146 | | - f"[blue]Reverted:[/blue] Migration '{migration.id}' ({status})" |
| 147 | | - ), |
| 148 | | - ) |
| 149 | | - |
| 150 | | - to_be_applied = list(repo.with_dependencies(applied_and_modified | added_and_pending)) |
| 151 | | - runner.deploy_migrations( |
| 152 | | - to_be_applied, |
| 153 | | - before_each=lambda migration, status: rich.print( |
| 154 | | - f"[green]Deployed:[/green] Migration '{migration.id}' ({status})" |
| 155 | | - ), |
| 226 | + _process_watch_changes( |
| 227 | + repo, |
| 228 | + runner, |
| 229 | + covered_migrations, |
| 230 | + changes, |
| 231 | + allow_new_migrations, |
| 156 | 232 | ) |
| 157 | 233 | |
| 158 | 234 | |