Re-formulate algorithm for checkout
public
Re-formulate algorithm for checkout
| Changeset | d6d3e1781f9e39cb163ddea442da53ced9808bd4 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | e3e7e8770aeb |
| Children | |
| Pushlog | by timo via ssh |
modified src/mitch/cli/checkout.py +128 −61
| @@ -1,4 +1,4 @@ | ||
| 1 | -from collections import defaultdict | |
| 1 | +from contextlib import contextmanager | |
| 2 | 2 | from pathlib import Path |
| 3 | 3 | from typing import Annotated, Literal, NamedTuple, Protocol, Generator |
| 4 | 4 | from dataclasses import dataclass |
| @@ -7,9 +7,10 @@ | ||
| 7 | 7 | import typer |
| 8 | 8 | import rich |
| 9 | 9 | |
| 10 | +from mitch.migration import Migration | |
| 11 | + | |
| 10 | 12 | from ..errors import MissingMigrationError |
| 11 | 13 | from ..helpers import connection_for |
| 12 | -from ..migration import Migration | |
| 13 | 14 | from ..repository import Repository |
| 14 | 15 | from ..runner import Runner |
| 15 | 16 | |
| @@ -42,12 +43,12 @@ | ||
| 42 | 43 | |
| 43 | 44 | @dataclass(frozen=True) |
| 44 | 45 | class Renamed: |
| 45 | - old_path: Path | |
| 46 | + path: Path | |
| 46 | 47 | new_path: Path |
| 47 | 48 | |
| 48 | 49 | @dataclass(frozen=True) |
| 49 | 50 | class Copied: |
| 50 | - old_path: Path | |
| 51 | + path: Path | |
| 51 | 52 | new_path: Path |
| 52 | 53 | |
| 53 | 54 | |
| @@ -122,20 +123,20 @@ | ||
| 122 | 123 | status_token, *paths = line.split("\t") |
| 123 | 124 | match (status_token, paths): |
| 124 | 125 | case ("A", [path]): |
| 125 | - yield Add(Path(path)) | |
| 126 | + yield Add(self.root / path) | |
| 126 | 127 | case ("M", [path]): |
| 127 | - yield Modified(Path(path)) | |
| 128 | + yield Modified(self.root / path) | |
| 128 | 129 | case ("T", [path]): |
| 129 | 130 | # Type changes include mode changes (e.g., file to symlink). |
| 130 | - yield Modified(Path(path)) | |
| 131 | + yield Modified(self.root / path) | |
| 131 | 132 | case ("D", [path]): |
| 132 | - yield Deleted(Path(path)) | |
| 133 | + yield Deleted(self.root / path) | |
| 133 | 134 | case (token, [old_path, new_path]) if token.startswith("R"): |
| 134 | 135 | # The numeric suffix indicates similarity: higher means less content changed. |
| 135 | - yield Renamed(Path(old_path), Path(new_path)) | |
| 136 | + yield Renamed(self.root / old_path, self.root / new_path) | |
| 136 | 137 | case (token, [old_path, new_path]) if token.startswith("C"): |
| 137 | 138 | # Copy scores use the same similarity convention as rename scores. |
| 138 | - yield Copied(Path(old_path), Path(new_path)) | |
| 139 | + yield Copied(self.root / old_path, self.root / new_path) | |
| 139 | 140 | case _: |
| 140 | 141 | # Ignore unexpected records to keep checkout robust across git versions. |
| 141 | 142 | continue |
| @@ -157,6 +158,42 @@ | ||
| 157 | 158 | def restore_changes(self) -> None: |
| 158 | 159 | # Restore local changes after checkout and migration re-application. |
| 159 | 160 | self._run("stash", "pop", "--index") |
| 161 | + | |
| 162 | + | |
| 163 | +def _migration_up_path(path: Path) -> Path | None: | |
| 164 | + if path.name.endswith(".up.sql"): | |
| 165 | + return path | |
| 166 | + if path.name.endswith(".config.toml"): | |
| 167 | + stem = path.name.removesuffix(".config.toml") | |
| 168 | + return path.with_name(f"{stem}.up.sql") | |
| 169 | + return None | |
| 170 | + | |
| 171 | + | |
| 172 | +@contextmanager | |
| 173 | +def _stashed_worktree( | |
| 174 | + git_manager: VersionControlManagerProtocol, | |
| 175 | + enabled: bool, | |
| 176 | + message: str, | |
| 177 | +): | |
| 178 | + if not enabled: | |
| 179 | + yield | |
| 180 | + return | |
| 181 | + | |
| 182 | + git_manager.stash_changes(message) | |
| 183 | + try: | |
| 184 | + yield | |
| 185 | + except Exception: | |
| 186 | + try: | |
| 187 | + git_manager.restore_changes() | |
| 188 | + except Exception as restore_error: | |
| 189 | + typer.echo( | |
| 190 | + "Error: checkout failed and stashed changes could not be restored automatically.", | |
| 191 | + err=True, | |
| 192 | + ) | |
| 193 | + raise RuntimeError("Failed to restore stashed changes.") from restore_error | |
| 194 | + raise | |
| 195 | + else: | |
| 196 | + git_manager.restore_changes() | |
| 160 | 197 | |
| 161 | 198 | |
| 162 | 199 | @app.command(name="checkout") |
| @@ -198,65 +235,95 @@ | ||
| 198 | 235 | git_manager.can_checkout(branch, allow_dirty) |
| 199 | 236 | should_pop_stash = allow_dirty and git_manager.is_dirty() |
| 200 | 237 | |
| 201 | - # Track where each changed migration will live on the target branch. | |
| 202 | - apply_paths: set[Path] = set() | |
| 203 | - revert_ids: set[str] = set() | |
| 204 | - | |
| 205 | - # Classify migrations affected by the branch switch: | |
| 206 | - # - missing_ids: up-script disappears on target branch | |
| 207 | - # - changed_ids: up/config content or path differs and should be redeployed | |
| 238 | + # Keep paths relative to repository root; this avoids cwd-dependent resolution later. | |
| 239 | + up_modified: set[Migration] = set() | |
| 240 | + up_deleted: set[Migration] = set() | |
| 241 | + | |
| 242 | + config_added: set[Migration] = set() | |
| 243 | + config_modified: set[Migration] = set() | |
| 244 | + config_deleted: set[Migration] = set() | |
| 245 | + | |
| 246 | + moved_paths: dict[Path, Path] = {} | |
| 247 | + redeploy_by_path: set[Path] = set() | |
| 248 | + new_paths: set[Path] = set() | |
| 249 | + | |
| 208 | 250 | for entry in git_manager.changes(branch): |
| 209 | 251 | match entry: |
| 210 | - case Deleted(path): | |
| 211 | - migration = repo.by_path(repo.path / path) | |
| 212 | - revert_ids.add(migration.id) | |
| 213 | - case Renamed(old_path, new_path): | |
| 214 | - migration = repo.by_path(repo.path / old_path) | |
| 215 | - revert_ids.add(migration.id) | |
| 216 | - apply_paths.add(new_path) | |
| 217 | - case Modified(path): | |
| 218 | - migration = repo.by_path(repo.path / path) | |
| 219 | - revert_ids.add(migration.id) | |
| 220 | - apply_paths.add(path) | |
| 221 | - case Add(path): | |
| 222 | - apply_paths.add(path) | |
| 223 | - case Copied(old_path, new_path) if new_path.name.endswith(".up.sql"): | |
| 224 | - apply_paths.add(new_path) | |
| 252 | + case Deleted(path) if path.suffix == '.sql': | |
| 253 | + up_deleted.add(repo.by_path(path)) | |
| 254 | + case Deleted(path) if path.suffix == '.toml': | |
| 255 | + config_deleted.add(repo.by_path(path)) | |
| 256 | + case Modified(path) if path.suffix == '.sql': | |
| 257 | + up_modified.add(repo.by_path(path)) | |
| 258 | + case Modified(path) if path.suffix == '.toml': | |
| 259 | + config_modified.add(repo.by_path(path)) | |
| 260 | + case Renamed(old_path, new_path) if old_path.suffix == '.sql' and new_path.suffix == '.sql': | |
| 261 | + up_deleted.add(repo.by_path(old_path)) | |
| 262 | + moved_paths[old_path] = new_path | |
| 263 | + redeploy_by_path.add(new_path) | |
| 264 | + case Renamed(old_path, new_path) if old_path.suffix == '.toml' and new_path.suffix == '.toml': | |
| 265 | + config_deleted.add(repo.by_path(old_path)) | |
| 266 | + moved_paths[old_path] = new_path | |
| 267 | + # Redeploys should not be triggered here, but by the up script path above. | |
| 268 | + # FIXME: Added migrations should only be applied if requested. | |
| 269 | + # For now, we assume all adds are intended to be applied, basically matching the behavior of `mitch up` on new files. | |
| 270 | + # But maybe, we should be closer to a `mitch pick` and only apply what the user explicitly wanted to add. | |
| 271 | + case Add(path) if path.suffix == '.sql': | |
| 272 | + new_paths.add(path) | |
| 273 | + case Add(path) if path.suffix == '.toml': | |
| 274 | + try: | |
| 275 | + # Maybe, we already have a migration with an up script, but no config yet. | |
| 276 | + # If so, we want to make sure to reapply that migration after checkout since the new config may introduce new dependencies or change IDs. | |
| 277 | + config_added.add(repo.by_path(path.with_suffix('').with_suffix('.up.sql'))) | |
| 278 | + except MissingMigrationError: | |
| 279 | + pass | |
| 280 | + case Copied(_, new_path) if new_path.suffix == '.sql': | |
| 281 | + # Copies are treated as additions on the target branch, | |
| 282 | + # but we don't need to track them here since they don't cause reverts on the source branch. | |
| 283 | + new_paths.add(new_path) | |
| 225 | 284 | |
| 226 | 285 | # Revert migrations on the current branch before switching branches. |
| 227 | 286 | with connection_for(database_url) as connection: |
| 228 | 287 | runner = Runner(connection) |
| 229 | - runner.revert_migrations( | |
| 230 | - repo.by_ids_with_dependants(revert_ids), | |
| 288 | + reverted = set(runner.revert_migrations( | |
| 289 | + repo.with_dependants(up_deleted | up_modified | config_deleted | config_modified | config_added), | |
| 231 | 290 | before_each=lambda migration, status: rich.print( |
| 232 | 291 | f"[blue]Reverting:[/blue] Migration '{migration.id}' ({status})" |
| 233 | 292 | ), |
| 234 | - ) | |
| 293 | + )) | |
| 294 | + | |
| 295 | + for migration in reverted: | |
| 296 | + old_path = migration.up_script.relative_to(repo.path) | |
| 297 | + redeploy_by_path.add(moved_paths.get(old_path, old_path)) | |
| 298 | + | |
| 235 | 299 | |
| 236 | 300 | # For dirty worktrees, stash only after DB reverts so reverts reflect current local files. |
| 237 | - if should_pop_stash: | |
| 238 | - git_manager.stash_changes(f"mitch checkout {branch}") | |
| 239 | - | |
| 240 | - # Switch working tree to the target branch once pre-checkout DB reverts are complete. | |
| 241 | - git_manager.checkout(branch) | |
| 242 | - | |
| 243 | - # Reload repository state from the target branch and identify migrations to reapply. | |
| 244 | - repo_after = Repository(root) | |
| 245 | - reapply_migrations = repo_after.by_paths( | |
| 246 | - repo_after.path.joinpath(up_script) | |
| 247 | - for up_script in apply_paths | |
| 248 | - ) | |
| 249 | - | |
| 250 | - # Reapply required migrations on the target branch, including dependencies. | |
| 251 | - with connection_for(database_url) as connection: | |
| 252 | - runner = Runner(connection) | |
| 253 | - runner.deploy_migrations( | |
| 254 | - repo_after.with_dependencies(reapply_migrations), | |
| 255 | - before_each=lambda migration, status: rich.print( | |
| 256 | - f"[green]Deploying:[/green] Migration '{migration.id}' ({status})" | |
| 257 | - ), | |
| 258 | - ) | |
| 259 | - | |
| 260 | - # Restore local worktree changes after migration state matches the checked-in target branch. | |
| 261 | - if should_pop_stash: | |
| 262 | - git_manager.restore_changes() | |
| 301 | + with _stashed_worktree(git_manager, should_pop_stash, f"mitch checkout {branch}"): | |
| 302 | + # Switch working tree to the target branch once pre-checkout DB reverts are complete. | |
| 303 | + git_manager.checkout(branch) | |
| 304 | + | |
| 305 | + # Reload repository state from the target branch and identify migrations to reapply. | |
| 306 | + repo_after = Repository(root) | |
| 307 | + apply_migrations = { repo_after.by_path(path) for path in redeploy_by_path } | |
| 308 | + apply_migrations.update({ repo_after.by_path(path) for path in new_paths }) | |
| 309 | + | |
| 310 | + # Important: this path-based lookup also handles ID changes after checkout. | |
| 311 | + # If the target config renames a migration ID, we still resolve and reapply | |
| 312 | + # by file path from `apply_paths`, even though the old ID no longer exists. | |
| 313 | + #for migration in reverted: | |
| 314 | + # try: | |
| 315 | + # apply_migrations.add(repo_after.by_id(migration.id)) | |
| 316 | + # except MissingMigrationError: | |
| 317 | + # # Old IDs may disappear on target after config changes. That's expected; | |
| 318 | + # # the path-based reapply above already covers those migrations. | |
| 319 | + # continue | |
| 320 | + | |
| 321 | + # Reapply required migrations on the target branch, including dependencies. | |
| 322 | + with connection_for(database_url) as connection: | |
| 323 | + runner = Runner(connection) | |
| 324 | + runner.deploy_migrations( | |
| 325 | + repo_after.with_dependencies(apply_migrations), | |
| 326 | + before_each=lambda migration, status: rich.print( | |
| 327 | + f"[green]Deploying:[/green] Migration '{migration.id}' ({status})" | |
| 328 | + ), | |
| 329 | + ) | |
modified src/mitch/repository.py +29 −3
| @@ -140,10 +140,36 @@ | ||
| 140 | 140 | raise MissingMigrationError(f"No migration found for path: {migration_path}") |
| 141 | 141 | |
| 142 | 142 | def by_paths(self, migration_paths: Iterable[Path | str]) -> Iterable[Migration]: |
| 143 | - migration_paths = set(Path(p).resolve() for p in migration_paths) | |
| 143 | + # FIXME: This is heavy and should be moved to _load_migrations | |
| 144 | + path_to_migration: dict[Path, Migration] = {} | |
| 144 | 145 | for migration in self.migrations: |
| 145 | - if { migration.up_script, migration.down_script, migration.config_file } & migration_paths: | |
| 146 | - yield migration | |
| 146 | + path_to_migration[migration.up_script] = migration | |
| 147 | + if migration.down_script is not None: | |
| 148 | + path_to_migration[migration.down_script] = migration | |
| 149 | + if migration.config_file is not None: | |
| 150 | + path_to_migration[migration.config_file] = migration | |
| 151 | + | |
| 152 | + selected: list[Migration] = [] | |
| 153 | + selected_ids: set[str] = set() | |
| 154 | + missing_paths: list[Path] = [] | |
| 155 | + | |
| 156 | + for path in migration_paths: | |
| 157 | + resolved = Path(path).resolve() | |
| 158 | + migration = path_to_migration.get(resolved) | |
| 159 | + if migration is None: | |
| 160 | + missing_paths.append(resolved) | |
| 161 | + continue | |
| 162 | + if migration.id not in selected_ids: | |
| 163 | + selected_ids.add(migration.id) | |
| 164 | + selected.append(migration) | |
| 165 | + | |
| 166 | + if missing_paths: | |
| 167 | + if len(missing_paths) == 1: | |
| 168 | + raise MissingMigrationError(f"No migration found for path: {missing_paths[0]}") | |
| 169 | + joined = ", ".join(str(path) for path in missing_paths) | |
| 170 | + raise MissingMigrationError(f"No migration found for paths: {joined}") | |
| 171 | + | |
| 172 | + yield from selected | |
| 147 | 173 | |
| 148 | 174 | def watch_for_changes(self): |
| 149 | 175 | from .watcher import wait_for_changes |
modified tests/test_cli.py +505 −0
| @@ -735,3 +735,508 @@ | ||
| 735 | 735 | assert [row[0] for row in applied_ids] == ["001-init"] |
| 736 | 736 | assert any(column[1] == "name" for column in table_columns) |
| 737 | 737 | assert marker_table is not None |
| 738 | + | |
| 739 | + | |
| 740 | +def test_checkout_deploys_migrations_added_on_target_branch(tmp_path): | |
| 741 | + repo_path = tmp_path / "repo" | |
| 742 | + repo_path.mkdir() | |
| 743 | + _init_git_repo(repo_path) | |
| 744 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 745 | + | |
| 746 | + (repo_path / "001-init.up.sql").write_text( | |
| 747 | + "CREATE TABLE table_one (id int);", | |
| 748 | + encoding="utf-8", | |
| 749 | + ) | |
| 750 | + (repo_path / "001-init.down.sql").write_text( | |
| 751 | + "DROP TABLE table_one;", | |
| 752 | + encoding="utf-8", | |
| 753 | + ) | |
| 754 | + _run_git(repo_path, "add", ".") | |
| 755 | + _run_git(repo_path, "commit", "-m", "base migration") | |
| 756 | + | |
| 757 | + db_path = tmp_path / "test.db" | |
| 758 | + database_url = f"sqlite:///{db_path}" | |
| 759 | + | |
| 760 | + up_result = runner.invoke( | |
| 761 | + app, | |
| 762 | + [ | |
| 763 | + "up", | |
| 764 | + "--repository", | |
| 765 | + str(repo_path), | |
| 766 | + "--database", | |
| 767 | + database_url, | |
| 768 | + "--if-modified", | |
| 769 | + "ignore", | |
| 770 | + ], | |
| 771 | + ) | |
| 772 | + assert up_result.exit_code == 0 | |
| 773 | + | |
| 774 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 775 | + (repo_path / "002-add.up.sql").write_text( | |
| 776 | + "CREATE TABLE table_two (id int);", | |
| 777 | + encoding="utf-8", | |
| 778 | + ) | |
| 779 | + (repo_path / "002-add.down.sql").write_text( | |
| 780 | + "DROP TABLE table_two;", | |
| 781 | + encoding="utf-8", | |
| 782 | + ) | |
| 783 | + _run_git(repo_path, "add", ".") | |
| 784 | + _run_git(repo_path, "commit", "-m", "add migration") | |
| 785 | + _run_git(repo_path, "checkout", base_branch) | |
| 786 | + | |
| 787 | + checkout_result = runner.invoke( | |
| 788 | + app, | |
| 789 | + [ | |
| 790 | + "checkout", | |
| 791 | + "target", | |
| 792 | + "--repository", | |
| 793 | + str(repo_path), | |
| 794 | + "--database", | |
| 795 | + database_url, | |
| 796 | + ], | |
| 797 | + ) | |
| 798 | + assert checkout_result.exit_code == 0 | |
| 799 | + | |
| 800 | + with sqlite3.connect(db_path) as connection: | |
| 801 | + applied_ids = connection.execute( | |
| 802 | + "SELECT id FROM mitch_migrations ORDER BY id", | |
| 803 | + ).fetchall() | |
| 804 | + second_table = connection.execute( | |
| 805 | + "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_two'", | |
| 806 | + ).fetchone() | |
| 807 | + | |
| 808 | + assert [row[0] for row in applied_ids] == ["001-init", "002-add"] | |
| 809 | + assert second_table is not None | |
| 810 | + | |
| 811 | + | |
| 812 | +def test_checkout_redeploys_dependants_of_changed_migration(tmp_path): | |
| 813 | + repo_path = tmp_path / "repo" | |
| 814 | + repo_path.mkdir() | |
| 815 | + _init_git_repo(repo_path) | |
| 816 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 817 | + | |
| 818 | + (repo_path / "001-init.up.sql").write_text( | |
| 819 | + "CREATE TABLE table_one (id int);", | |
| 820 | + encoding="utf-8", | |
| 821 | + ) | |
| 822 | + (repo_path / "001-init.down.sql").write_text( | |
| 823 | + "DROP TABLE table_one;", | |
| 824 | + encoding="utf-8", | |
| 825 | + ) | |
| 826 | + (repo_path / "002-child.up.sql").write_text( | |
| 827 | + "CREATE TABLE table_two (id int);", | |
| 828 | + encoding="utf-8", | |
| 829 | + ) | |
| 830 | + (repo_path / "002-child.down.sql").write_text( | |
| 831 | + "DROP TABLE table_two;", | |
| 832 | + encoding="utf-8", | |
| 833 | + ) | |
| 834 | + (repo_path / "002-child.config.toml").write_text( | |
| 835 | + 'dependencies = ["001-init"]', | |
| 836 | + encoding="utf-8", | |
| 837 | + ) | |
| 838 | + _run_git(repo_path, "add", ".") | |
| 839 | + _run_git(repo_path, "commit", "-m", "base chain") | |
| 840 | + | |
| 841 | + db_path = tmp_path / "test.db" | |
| 842 | + database_url = f"sqlite:///{db_path}" | |
| 843 | + | |
| 844 | + up_result = runner.invoke( | |
| 845 | + app, | |
| 846 | + [ | |
| 847 | + "up", | |
| 848 | + "--repository", | |
| 849 | + str(repo_path), | |
| 850 | + "--database", | |
| 851 | + database_url, | |
| 852 | + "--if-modified", | |
| 853 | + "ignore", | |
| 854 | + ], | |
| 855 | + ) | |
| 856 | + assert up_result.exit_code == 0 | |
| 857 | + | |
| 858 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 859 | + (repo_path / "001-init.up.sql").write_text( | |
| 860 | + "CREATE TABLE table_one (id int, name text);", | |
| 861 | + encoding="utf-8", | |
| 862 | + ) | |
| 863 | + _run_git(repo_path, "add", ".") | |
| 864 | + _run_git(repo_path, "commit", "-m", "change parent migration") | |
| 865 | + _run_git(repo_path, "checkout", base_branch) | |
| 866 | + | |
| 867 | + checkout_result = runner.invoke( | |
| 868 | + app, | |
| 869 | + [ | |
| 870 | + "checkout", | |
| 871 | + "target", | |
| 872 | + "--repository", | |
| 873 | + str(repo_path), | |
| 874 | + "--database", | |
| 875 | + database_url, | |
| 876 | + ], | |
| 877 | + ) | |
| 878 | + assert checkout_result.exit_code == 0 | |
| 879 | + | |
| 880 | + with sqlite3.connect(db_path) as connection: | |
| 881 | + applied_ids = connection.execute( | |
| 882 | + "SELECT id FROM mitch_migrations ORDER BY id", | |
| 883 | + ).fetchall() | |
| 884 | + parent_columns = connection.execute( | |
| 885 | + "PRAGMA table_info(table_one)", | |
| 886 | + ).fetchall() | |
| 887 | + child_table = connection.execute( | |
| 888 | + "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_two'", | |
| 889 | + ).fetchone() | |
| 890 | + | |
| 891 | + assert [row[0] for row in applied_ids] == ["001-init", "002-child"] | |
| 892 | + assert any(column[1] == "name" for column in parent_columns) | |
| 893 | + assert child_table is not None | |
| 894 | + | |
| 895 | + | |
| 896 | +def test_checkout_handles_migration_id_changed_in_config(tmp_path): | |
| 897 | + repo_path = tmp_path / "repo" | |
| 898 | + repo_path.mkdir() | |
| 899 | + _init_git_repo(repo_path) | |
| 900 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 901 | + | |
| 902 | + (repo_path / "001-init.up.sql").write_text( | |
| 903 | + "CREATE TABLE table_one (id int);", | |
| 904 | + encoding="utf-8", | |
| 905 | + ) | |
| 906 | + (repo_path / "001-init.down.sql").write_text( | |
| 907 | + "DROP TABLE table_one;", | |
| 908 | + encoding="utf-8", | |
| 909 | + ) | |
| 910 | + (repo_path / "001-init.config.toml").write_text( | |
| 911 | + 'id = "001-init"', | |
| 912 | + encoding="utf-8", | |
| 913 | + ) | |
| 914 | + _run_git(repo_path, "add", ".") | |
| 915 | + _run_git(repo_path, "commit", "-m", "base id") | |
| 916 | + | |
| 917 | + db_path = tmp_path / "test.db" | |
| 918 | + database_url = f"sqlite:///{db_path}" | |
| 919 | + | |
| 920 | + up_result = runner.invoke( | |
| 921 | + app, | |
| 922 | + [ | |
| 923 | + "up", | |
| 924 | + "--repository", | |
| 925 | + str(repo_path), | |
| 926 | + "--database", | |
| 927 | + database_url, | |
| 928 | + "--if-modified", | |
| 929 | + "ignore", | |
| 930 | + ], | |
| 931 | + ) | |
| 932 | + assert up_result.exit_code == 0 | |
| 933 | + | |
| 934 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 935 | + (repo_path / "001-init.config.toml").write_text( | |
| 936 | + 'id = "001-renamed"', | |
| 937 | + encoding="utf-8", | |
| 938 | + ) | |
| 939 | + _run_git(repo_path, "add", ".") | |
| 940 | + _run_git(repo_path, "commit", "-m", "rename migration id") | |
| 941 | + _run_git(repo_path, "checkout", base_branch) | |
| 942 | + | |
| 943 | + checkout_result = runner.invoke( | |
| 944 | + app, | |
| 945 | + [ | |
| 946 | + "checkout", | |
| 947 | + "target", | |
| 948 | + "--repository", | |
| 949 | + str(repo_path), | |
| 950 | + "--database", | |
| 951 | + database_url, | |
| 952 | + ], | |
| 953 | + ) | |
| 954 | + assert checkout_result.exit_code == 0 | |
| 955 | + assert checkout_result.exception is None | |
| 956 | + | |
| 957 | + with sqlite3.connect(db_path) as connection: | |
| 958 | + applied_ids = connection.execute( | |
| 959 | + "SELECT id FROM mitch_migrations ORDER BY id", | |
| 960 | + ).fetchall() | |
| 961 | + | |
| 962 | + assert [row[0] for row in applied_ids] == ["001-renamed"] | |
| 963 | + | |
| 964 | + | |
| 965 | +def test_checkout_handles_config_added_for_existing_migration(tmp_path): | |
| 966 | + repo_path = tmp_path / "repo" | |
| 967 | + repo_path.mkdir() | |
| 968 | + _init_git_repo(repo_path) | |
| 969 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 970 | + | |
| 971 | + (repo_path / "001-init.up.sql").write_text( | |
| 972 | + "CREATE TABLE table_one (id int);", | |
| 973 | + encoding="utf-8", | |
| 974 | + ) | |
| 975 | + (repo_path / "001-init.down.sql").write_text( | |
| 976 | + "DROP TABLE table_one;", | |
| 977 | + encoding="utf-8", | |
| 978 | + ) | |
| 979 | + _run_git(repo_path, "add", ".") | |
| 980 | + _run_git(repo_path, "commit", "-m", "base without config") | |
| 981 | + | |
| 982 | + db_path = tmp_path / "test.db" | |
| 983 | + database_url = f"sqlite:///{db_path}" | |
| 984 | + | |
| 985 | + up_result = runner.invoke( | |
| 986 | + app, | |
| 987 | + [ | |
| 988 | + "up", | |
| 989 | + "--repository", | |
| 990 | + str(repo_path), | |
| 991 | + "--database", | |
| 992 | + database_url, | |
| 993 | + "--if-modified", | |
| 994 | + "ignore", | |
| 995 | + ], | |
| 996 | + ) | |
| 997 | + assert up_result.exit_code == 0 | |
| 998 | + | |
| 999 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 1000 | + (repo_path / "001-init.config.toml").write_text( | |
| 1001 | + 'id = "001-renamed"', | |
| 1002 | + encoding="utf-8", | |
| 1003 | + ) | |
| 1004 | + _run_git(repo_path, "add", ".") | |
| 1005 | + _run_git(repo_path, "commit", "-m", "add config and rename id") | |
| 1006 | + _run_git(repo_path, "checkout", base_branch) | |
| 1007 | + | |
| 1008 | + checkout_result = runner.invoke( | |
| 1009 | + app, | |
| 1010 | + [ | |
| 1011 | + "checkout", | |
| 1012 | + "target", | |
| 1013 | + "--repository", | |
| 1014 | + str(repo_path), | |
| 1015 | + "--database", | |
| 1016 | + database_url, | |
| 1017 | + ], | |
| 1018 | + ) | |
| 1019 | + assert checkout_result.exit_code == 0 | |
| 1020 | + assert checkout_result.exception is None | |
| 1021 | + | |
| 1022 | + with sqlite3.connect(db_path) as connection: | |
| 1023 | + applied_ids = connection.execute( | |
| 1024 | + "SELECT id FROM mitch_migrations ORDER BY id", | |
| 1025 | + ).fetchall() | |
| 1026 | + | |
| 1027 | + assert [row[0] for row in applied_ids] == ["001-renamed"] | |
| 1028 | + | |
| 1029 | + | |
| 1030 | +def test_checkout_handles_config_removed_for_existing_migration(tmp_path): | |
| 1031 | + repo_path = tmp_path / "repo" | |
| 1032 | + repo_path.mkdir() | |
| 1033 | + _init_git_repo(repo_path) | |
| 1034 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 1035 | + | |
| 1036 | + (repo_path / "001-init.up.sql").write_text( | |
| 1037 | + "CREATE TABLE table_one (id int);", | |
| 1038 | + encoding="utf-8", | |
| 1039 | + ) | |
| 1040 | + (repo_path / "001-init.down.sql").write_text( | |
| 1041 | + "DROP TABLE table_one;", | |
| 1042 | + encoding="utf-8", | |
| 1043 | + ) | |
| 1044 | + (repo_path / "001-init.config.toml").write_text( | |
| 1045 | + 'id = "001-custom"', | |
| 1046 | + encoding="utf-8", | |
| 1047 | + ) | |
| 1048 | + _run_git(repo_path, "add", ".") | |
| 1049 | + _run_git(repo_path, "commit", "-m", "base with config") | |
| 1050 | + | |
| 1051 | + db_path = tmp_path / "test.db" | |
| 1052 | + database_url = f"sqlite:///{db_path}" | |
| 1053 | + | |
| 1054 | + up_result = runner.invoke( | |
| 1055 | + app, | |
| 1056 | + [ | |
| 1057 | + "up", | |
| 1058 | + "--repository", | |
| 1059 | + str(repo_path), | |
| 1060 | + "--database", | |
| 1061 | + database_url, | |
| 1062 | + "--if-modified", | |
| 1063 | + "ignore", | |
| 1064 | + ], | |
| 1065 | + ) | |
| 1066 | + assert up_result.exit_code == 0 | |
| 1067 | + | |
| 1068 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 1069 | + _run_git(repo_path, "rm", "001-init.config.toml") | |
| 1070 | + _run_git(repo_path, "commit", "-m", "remove config") | |
| 1071 | + _run_git(repo_path, "checkout", base_branch) | |
| 1072 | + | |
| 1073 | + checkout_result = runner.invoke( | |
| 1074 | + app, | |
| 1075 | + [ | |
| 1076 | + "checkout", | |
| 1077 | + "target", | |
| 1078 | + "--repository", | |
| 1079 | + str(repo_path), | |
| 1080 | + "--database", | |
| 1081 | + database_url, | |
| 1082 | + ], | |
| 1083 | + ) | |
| 1084 | + assert checkout_result.exit_code == 0 | |
| 1085 | + assert checkout_result.exception is None | |
| 1086 | + | |
| 1087 | + with sqlite3.connect(db_path) as connection: | |
| 1088 | + applied_ids = connection.execute( | |
| 1089 | + "SELECT id FROM mitch_migrations ORDER BY id", | |
| 1090 | + ).fetchall() | |
| 1091 | + table_exists = connection.execute( | |
| 1092 | + "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", | |
| 1093 | + ).fetchone() | |
| 1094 | + | |
| 1095 | + assert [row[0] for row in applied_ids] == ["001-init"] | |
| 1096 | + assert table_exists is not None | |
| 1097 | + | |
| 1098 | + | |
| 1099 | +def test_checkout_handles_config_and_scripts_deleted_together(tmp_path): | |
| 1100 | + repo_path = tmp_path / "repo" | |
| 1101 | + repo_path.mkdir() | |
| 1102 | + _init_git_repo(repo_path) | |
| 1103 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 1104 | + | |
| 1105 | + (repo_path / "001-init.up.sql").write_text( | |
| 1106 | + "CREATE TABLE table_one (id int);", | |
| 1107 | + encoding="utf-8", | |
| 1108 | + ) | |
| 1109 | + (repo_path / "001-init.down.sql").write_text( | |
| 1110 | + "DROP TABLE table_one;", | |
| 1111 | + encoding="utf-8", | |
| 1112 | + ) | |
| 1113 | + (repo_path / "001-init.config.toml").write_text( | |
| 1114 | + 'id = "001-custom"', | |
| 1115 | + encoding="utf-8", | |
| 1116 | + ) | |
| 1117 | + _run_git(repo_path, "add", ".") | |
| 1118 | + _run_git(repo_path, "commit", "-m", "base with migration config") | |
| 1119 | + | |
| 1120 | + db_path = tmp_path / "test.db" | |
| 1121 | + database_url = f"sqlite:///{db_path}" | |
| 1122 | + | |
| 1123 | + up_result = runner.invoke( | |
| 1124 | + app, | |
| 1125 | + [ | |
| 1126 | + "up", | |
| 1127 | + "--repository", | |
| 1128 | + str(repo_path), | |
| 1129 | + "--database", | |
| 1130 | + database_url, | |
| 1131 | + "--if-modified", | |
| 1132 | + "ignore", | |
| 1133 | + ], | |
| 1134 | + ) | |
| 1135 | + assert up_result.exit_code == 0 | |
| 1136 | + | |
| 1137 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 1138 | + _run_git( | |
| 1139 | + repo_path, | |
| 1140 | + "rm", | |
| 1141 | + "001-init.config.toml", | |
| 1142 | + "001-init.up.sql", | |
| 1143 | + "001-init.down.sql", | |
| 1144 | + ) | |
| 1145 | + _run_git(repo_path, "commit", "-m", "remove migration and config") | |
| 1146 | + _run_git(repo_path, "checkout", base_branch) | |
| 1147 | + | |
| 1148 | + checkout_result = runner.invoke( | |
| 1149 | + app, | |
| 1150 | + [ | |
| 1151 | + "checkout", | |
| 1152 | + "target", | |
| 1153 | + "--repository", | |
| 1154 | + str(repo_path), | |
| 1155 | + "--database", | |
| 1156 | + database_url, | |
| 1157 | + ], | |
| 1158 | + ) | |
| 1159 | + assert checkout_result.exit_code == 0 | |
| 1160 | + assert checkout_result.exception is None | |
| 1161 | + | |
| 1162 | + with sqlite3.connect(db_path) as connection: | |
| 1163 | + applied_ids = connection.execute( | |
| 1164 | + "SELECT id FROM mitch_migrations ORDER BY id", | |
| 1165 | + ).fetchall() | |
| 1166 | + table_exists = connection.execute( | |
| 1167 | + "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", | |
| 1168 | + ).fetchone() | |
| 1169 | + | |
| 1170 | + assert applied_ids == [] | |
| 1171 | + assert table_exists is None | |
| 1172 | + | |
| 1173 | + | |
| 1174 | +def test_checkout_allow_dirty_restores_stashed_changes_after_failure(tmp_path): | |
| 1175 | + repo_path = tmp_path / "repo" | |
| 1176 | + repo_path.mkdir() | |
| 1177 | + _init_git_repo(repo_path) | |
| 1178 | + base_branch = _run_git(repo_path, "branch", "--show-current") | |
| 1179 | + | |
| 1180 | + (repo_path / "README.md").write_text("base\n", encoding="utf-8") | |
| 1181 | + (repo_path / "001-init.up.sql").write_text( | |
| 1182 | + "CREATE TABLE table_one (id int);", | |
| 1183 | + encoding="utf-8", | |
| 1184 | + ) | |
| 1185 | + (repo_path / "001-init.down.sql").write_text( | |
| 1186 | + "DROP TABLE table_one;", | |
| 1187 | + encoding="utf-8", | |
| 1188 | + ) | |
| 1189 | + _run_git(repo_path, "add", ".") | |
| 1190 | + _run_git(repo_path, "commit", "-m", "base migration") | |
| 1191 | + | |
| 1192 | + db_path = tmp_path / "test.db" | |
| 1193 | + database_url = f"sqlite:///{db_path}" | |
| 1194 | + | |
| 1195 | + up_result = runner.invoke( | |
| 1196 | + app, | |
| 1197 | + [ | |
| 1198 | + "up", | |
| 1199 | + "--repository", | |
| 1200 | + str(repo_path), | |
| 1201 | + "--database", | |
| 1202 | + database_url, | |
| 1203 | + "--if-modified", | |
| 1204 | + "ignore", | |
| 1205 | + ], | |
| 1206 | + ) | |
| 1207 | + assert up_result.exit_code == 0 | |
| 1208 | + | |
| 1209 | + _run_git(repo_path, "checkout", "-b", "target") | |
| 1210 | + (repo_path / "002-broken.up.sql").write_text( | |
| 1211 | + "THIS IS NOT VALID SQL;", | |
| 1212 | + encoding="utf-8", | |
| 1213 | + ) | |
| 1214 | + (repo_path / "002-broken.down.sql").write_text( | |
| 1215 | + "-- noop", | |
| 1216 | + encoding="utf-8", | |
| 1217 | + ) | |
| 1218 | + _run_git(repo_path, "add", ".") | |
| 1219 | + _run_git(repo_path, "commit", "-m", "broken migration") | |
| 1220 | + _run_git(repo_path, "checkout", base_branch) | |
| 1221 | + | |
| 1222 | + (repo_path / "README.md").write_text("dirty local change\n", encoding="utf-8") | |
| 1223 | + | |
| 1224 | + checkout_result = runner.invoke( | |
| 1225 | + app, | |
| 1226 | + [ | |
| 1227 | + "checkout", | |
| 1228 | + "target", | |
| 1229 | + "--repository", | |
| 1230 | + str(repo_path), | |
| 1231 | + "--database", | |
| 1232 | + database_url, | |
| 1233 | + "--allow-dirty", | |
| 1234 | + ], | |
| 1235 | + ) | |
| 1236 | + assert checkout_result.exit_code == 1 | |
| 1237 | + | |
| 1238 | + restored_readme = (repo_path / "README.md").read_text(encoding="utf-8") | |
| 1239 | + stash_list = _run_git(repo_path, "stash", "list") | |
| 1240 | + | |
| 1241 | + assert restored_readme == "dirty local change\n" | |
| 1242 | + assert stash_list == "" | |
modified tests/test_repository.py +16 −0
| @@ -1,4 +1,6 @@ | ||
| 1 | 1 | from mitch.repository import Repository |
| 2 | +from mitch.errors import MissingMigrationError | |
| 3 | +import pytest | |
| 2 | 4 | |
| 3 | 5 | def test_load_repository(): |
| 4 | 6 | repo = Repository("tests/migrations") |
| @@ -43,3 +45,17 @@ | ||
| 43 | 45 | migrations = list(repo.by_ids_with_dependencies(["001-init", "002-second", "003-old", "004-new"])) |
| 44 | 46 | |
| 45 | 47 | assert [m.id for m in migrations] == ["003-old", "004-new", "001-init", "002-second"] |
| 48 | + | |
| 49 | + | |
| 50 | +def test_by_paths_raises_if_any_path_is_missing(): | |
| 51 | + repo = Repository("tests/migrations") | |
| 52 | + | |
| 53 | + with pytest.raises(MissingMigrationError): | |
| 54 | + list( | |
| 55 | + repo.by_paths( | |
| 56 | + [ | |
| 57 | + "tests/migrations/001-init.up.sql", | |
| 58 | + "tests/migrations/does-not-exist.up.sql", | |
| 59 | + ] | |
| 60 | + ) | |
| 61 | + ) | |