| @@ -1,7 +1,9 @@ |
| 1 | 1 | from enum import Enum |
| 2 | | -from typing import Annotated, Iterable |
| 2 | +from typing import Annotated, Iterable, Callable |
| 3 | 3 | from pathlib import Path |
| 4 | 4 | from contextlib import nullcontext |
| 5 | +from threading import Event |
| 6 | +from time import monotonic, sleep |
| 5 | 7 | import os |
| 6 | 8 | |
| 7 | 9 | from rich.prompt import Confirm |
| @@ -31,6 +33,88 @@ |
| 31 | 33 | ) |
| 32 | 34 | raise typer.Exit(1) |
| 33 | 35 | return resolved_database_url |
| 36 | + |
| 37 | + |
| 38 | +def _is_migration_file(path: Path) -> bool: |
| 39 | + name = path.name |
| 40 | + return ( |
| 41 | + name.endswith(".up.sql") |
| 42 | + or name.endswith(".down.sql") |
| 43 | + or name.endswith(".config.toml") |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _deploy_all_migrations( |
| 48 | + root: Path, |
| 49 | + database_url: str | None, |
| 50 | + if_modified: IfModifiedOption, |
| 51 | +) -> None: |
| 52 | + repo = Repository(root) |
| 53 | + migration_ids = [m.id for m in repo.migrations] |
| 54 | + _deploy_migrations(repo, migration_ids, database_url, if_modified) |
| 55 | + |
| 56 | + |
| 57 | +def _watch_migrations( |
| 58 | + root: Path, |
| 59 | + on_change: Callable[[], None], |
| 60 | + keep_going: bool, |
| 61 | + debounce_seconds: float = 0.5, |
| 62 | +) -> None: |
| 63 | + try: |
| 64 | + from watchdog.events import FileSystemEventHandler |
| 65 | + from watchdog.observers import Observer |
| 66 | + except ModuleNotFoundError as exc: |
| 67 | + typer.echo("Error: --watch requires the 'watchdog' package.", err=True) |
| 68 | + raise typer.Exit(1) from exc |
| 69 | + |
| 70 | + change_event = Event() |
| 71 | + last_change = 0.0 |
| 72 | + |
| 73 | + def trigger_change() -> None: |
| 74 | + nonlocal last_change |
| 75 | + last_change = monotonic() |
| 76 | + change_event.set() |
| 77 | + |
| 78 | + class MigrationWatchHandler(FileSystemEventHandler): |
| 79 | + def on_any_event(self, event) -> None: |
| 80 | + if event.is_directory: |
| 81 | + return |
| 82 | + if _is_migration_file(Path(event.src_path)): |
| 83 | + trigger_change() |
| 84 | + return |
| 85 | + dest_path = getattr(event, "dest_path", None) |
| 86 | + if dest_path and _is_migration_file(Path(dest_path)): |
| 87 | + trigger_change() |
| 88 | + |
| 89 | + handler = MigrationWatchHandler() |
| 90 | + observer = Observer() |
| 91 | + observer.schedule(handler, str(root), recursive=True) |
| 92 | + observer.start() |
| 93 | + rich.print("[blue]Watching for migration changes...[/blue]") |
| 94 | + |
| 95 | + try: |
| 96 | + while True: |
| 97 | + change_event.wait() |
| 98 | + |
| 99 | + while True: |
| 100 | + change_event.clear() |
| 101 | + sleep(debounce_seconds) |
| 102 | + if monotonic() - last_change >= debounce_seconds: |
| 103 | + break |
| 104 | + |
| 105 | + try: |
| 106 | + on_change() |
| 107 | + except typer.Exit: |
| 108 | + raise |
| 109 | + except Exception as exc: |
| 110 | + rich.print(f"[red]Error:[/red] {exc}") |
| 111 | + if not keep_going: |
| 112 | + raise |
| 113 | + except KeyboardInterrupt: |
| 114 | + pass |
| 115 | + finally: |
| 116 | + observer.stop() |
| 117 | + observer.join() |
| 34 | 118 | |
| 35 | 119 | |
| 36 | 120 | def _deploy_migrations( |
| @@ -129,9 +213,36 @@ |
| 129 | 213 | def up( |
| 130 | 214 | root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)], |
| 131 | 215 | 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, |
| 216 | + if_modified: Annotated[ |
| 217 | + IfModifiedOption | None, |
| 218 | + typer.Option( |
| 219 | + "--if-modified", |
| 220 | + "-m", |
| 221 | + help="Action to take if a migration has been modified since its last deployment.", |
| 222 | + case_sensitive=False, |
| 223 | + show_choices=True, |
| 224 | + show_default=False, |
| 225 | + ), |
| 226 | + ] = None, |
| 227 | + watch: Annotated[ |
| 228 | + bool, |
| 229 | + typer.Option("--watch", help="Watch the repository and re-run deployments on changes."), |
| 230 | + ] = False, |
| 231 | + watch_keep_going: Annotated[ |
| 232 | + bool, |
| 233 | + typer.Option("--watch-keep-going", help="Keep watching even if a deployment fails."), |
| 234 | + ] = False, |
| 133 | 235 | ) -> None: |
| 134 | 236 | """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) |
| 237 | + resolved_if_modified = ( |
| 238 | + if_modified |
| 239 | + if if_modified is not None |
| 240 | + else (IfModifiedOption.REDEPLOY if watch else IfModifiedOption.PROMPT) |
| 241 | + ) |
| 242 | + _deploy_all_migrations(root, database_url, resolved_if_modified) |
| 243 | + if watch: |
| 244 | + _watch_migrations( |
| 245 | + root, |
| 246 | + on_change=lambda: _deploy_all_migrations(root, database_url, resolved_if_modified), |
| 247 | + keep_going=watch_keep_going, |
| 248 | + ) |