refactor(cli): enhance checkout migration handling logic
public
refactor(cli): enhance checkout migration handling logic Update checkout ADR to clarify competing principles and transition planning. Add regression test for migration redeployment when local edits match target branch.
| Changeset | 17c5620c37079c91919a7ff0a18a9d53177f2458 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | daf392811a00 |
| Children | |
| Pushlog | by timo via ssh |
modified docs/adr/cli/checkout/restore-applied-migrations-but-do-not-up.md +23 −2
| @@ -5,6 +5,27 @@ | ||
| 5 | 5 | |
| 6 | 6 | When switching branches with uncommitted migration changes, `checkout` reverts what the transition requires, switches the branch, restores the stash, and then re-applies the migrations it reverted, including local-only ones that were applied before. It does not apply migrations that are new on the target branch unless `--and-up` is given. |
| 7 | 7 | |
| 8 | -## Considered options | |
| 8 | +## Two competing principles | |
| 9 | 9 | |
| 10 | -A strict rule ("the target branch rules") would leave a local-only migration reverted after checkout. We rejected it because the developer's intent is to continue where they left off: restore what was rolled out, but do not pull in extra work they did not ask for. The full reasoning is in the design notes on checkout. | |
| 10 | +Two intuitions pull in different directions when a local-only migration was applied before checkout: | |
| 11 | + | |
| 12 | +- **"The target branch rules."** After checkout, the deployment should match the target branch. A local-only migration is an addition and should stay reverted without `--and-up`. | |
| 13 | +- **"What I had, I want back."** A migration that was applied and is still present after the stash is restored should be applied again. | |
| 14 | + | |
| 15 | +We chose the second. The developer's intent is to continue where they left off: restore what was rolled out, but do not pull in extra work they did not ask for. A local-only migration is technically an addition, but from the developer's view it was reverted by checkout and is still there, so it comes back. | |
| 16 | + | |
| 17 | +## The transition is planned from commit to commit | |
| 18 | + | |
| 19 | +The plan compares the current commit with the target commit, never the working tree with the target. Local changes are reconciled separately before and after. The sequence with `--allow-dirty` is: | |
| 20 | + | |
| 21 | +1. Revert migrations whose local files differ from the current commit. | |
| 22 | +2. Stash local changes. | |
| 23 | +3. Re-apply the committed versions, so the deployment matches the current commit. | |
| 24 | +4. Revert what the transition from the current to the target commit requires. | |
| 25 | +5. Check out the target. | |
| 26 | +6. Restore the stash. | |
| 27 | +7. Re-apply everything reverted, from the final files. With `--and-up`, also apply new migrations. | |
| 28 | + | |
| 29 | +Without `--allow-dirty`, a dirty working tree is an error and only steps 4, 5 and 7 run. | |
| 30 | + | |
| 31 | +Steps 1 to 3 look redundant, but a working-tree-based plan breaks in this case: the current branch has v2 of a migration applied, the target branch has v1, and the developer has edited the file back to v1 locally. The working tree then equals the target, so no change is detected and the deployment stays at v2 while the file says v1. Going through the current commit first makes the v2-to-v1 transition visible. A regression test covers this case. | |
modified tests/test_cli.py +60 −0
| @@ -1410,6 +1410,66 @@ | ||
| 1410 | 1410 | assert marker_table is not None |
| 1411 | 1411 | |
| 1412 | 1412 | |
| 1413 | +def test_checkout_allow_dirty_redeploys_migration_locally_edited_to_match_target(tmp_path): | |
| 1414 | + # The current branch has v2 of a migration applied, the target branch has v1, and the file is | |
| 1415 | + # edited locally back to v1. A diff of the target against the working tree shows no migration | |
| 1416 | + # change, so a working-tree-based plan would skip the redeploy and leave the DB at v2. | |
| 1417 | + repo_path = tmp_path / "repo" | |
| 1418 | + repo_path.mkdir() | |
| 1419 | + _write_project_config(repo_path) | |
| 1420 | + _init_git_repo(repo_path) | |
| 1421 | + | |
| 1422 | + v1 = "CREATE TABLE table_one (id int);" | |
| 1423 | + v2 = "CREATE TABLE table_one (id int, name text);" | |
| 1424 | + | |
| 1425 | + (repo_path / "001-init.up.sql").write_text(v1, encoding="utf-8") | |
| 1426 | + (repo_path / "001-init.down.sql").write_text("DROP TABLE table_one;", encoding="utf-8") | |
| 1427 | + _run_git(repo_path, "add", ".") | |
| 1428 | + _run_git(repo_path, "commit", "-m", "add 001 v1") | |
| 1429 | + _run_git(repo_path, "branch", "target") | |
| 1430 | + | |
| 1431 | + (repo_path / "001-init.up.sql").write_text(v2, encoding="utf-8") | |
| 1432 | + _run_git(repo_path, "add", ".") | |
| 1433 | + _run_git(repo_path, "commit", "-m", "change 001 to v2") | |
| 1434 | + | |
| 1435 | + db_path = tmp_path / "test.db" | |
| 1436 | + database_url = f"sqlite:///{db_path}" | |
| 1437 | + | |
| 1438 | + up_result = runner.invoke( | |
| 1439 | + app, | |
| 1440 | + ["up", "--project", str(repo_path), "--database", database_url, "--if-modified", "ignore"], | |
| 1441 | + ) | |
| 1442 | + assert up_result.exit_code == 0 | |
| 1443 | + | |
| 1444 | + # Edit the migration locally back to what the target branch has. | |
| 1445 | + (repo_path / "001-init.up.sql").write_text(v1, encoding="utf-8") | |
| 1446 | + assert _run_git(repo_path, "diff", "--name-only", "target", "--", "001-init.up.sql") == "" | |
| 1447 | + | |
| 1448 | + checkout_result = runner.invoke( | |
| 1449 | + app, | |
| 1450 | + [ | |
| 1451 | + "checkout", | |
| 1452 | + "target", | |
| 1453 | + "--project", | |
| 1454 | + str(repo_path), | |
| 1455 | + "--database", | |
| 1456 | + database_url, | |
| 1457 | + "--allow-dirty", | |
| 1458 | + ], | |
| 1459 | + ) | |
| 1460 | + assert checkout_result.exit_code == 0, checkout_result.output | |
| 1461 | + | |
| 1462 | + assert _run_git(repo_path, "branch", "--show-current") == "target" | |
| 1463 | + assert (repo_path / "001-init.up.sql").read_text(encoding="utf-8") == v1 | |
| 1464 | + | |
| 1465 | + with sqlite3.connect(db_path) as connection: | |
| 1466 | + applied_ids = connection.execute("SELECT id FROM mitch_migrations").fetchall() | |
| 1467 | + columns = connection.execute("PRAGMA table_info(table_one)").fetchall() | |
| 1468 | + | |
| 1469 | + assert [row[0] for row in applied_ids] == ["001-init"] | |
| 1470 | + assert [column[1] for column in columns] == ["id"] | |
| 1471 | + | |
| 1472 | + | |
| 1413 | 1473 | def test_checkout_allow_dirty_restores_locally_added_applied_migration_without_and_up(tmp_path): |
| 1414 | 1474 | repo_path = tmp_path / "repo" |
| 1415 | 1475 | repo_path.mkdir() |