| @@ -199,34 +199,35 @@ |
| 199 | 199 | should_pop_stash = allow_dirty and git_manager.is_dirty() |
| 200 | 200 | |
| 201 | 201 | # Track where each changed migration will live on the target branch. |
| 202 | | - moved_up_scripts: set[Path] = set() |
| 203 | | - missing_ids: set[str] = set() |
| 204 | | - changed_ids: set[str] = set() |
| 202 | + apply_paths: set[Path] = set() |
| 203 | + revert_ids: set[str] = set() |
| 205 | 204 | |
| 206 | 205 | # Classify migrations affected by the branch switch: |
| 207 | 206 | # - missing_ids: up-script disappears on target branch |
| 208 | 207 | # - changed_ids: up/config content or path differs and should be redeployed |
| 209 | 208 | for entry in git_manager.changes(branch): |
| 210 | 209 | match entry: |
| 211 | | - case Deleted(path) if path.name.endswith(".up.sql"): |
| 210 | + case Deleted(path): |
| 212 | 211 | migration = repo.by_path(repo.path / path) |
| 213 | | - missing_ids.add(migration.id) |
| 214 | | - case Renamed(old_path, new_path) if old_path.name.endswith(".up.sql") and new_path.name.endswith(".up.sql"): |
| 212 | + revert_ids.add(migration.id) |
| 213 | + case Renamed(old_path, new_path): |
| 215 | 214 | migration = repo.by_path(repo.path / old_path) |
| 216 | | - missing_ids.add(migration.id) |
| 217 | | - moved_up_scripts.add(new_path) |
| 215 | + revert_ids.add(migration.id) |
| 216 | + apply_paths.add(new_path) |
| 218 | 217 | case Modified(path): |
| 219 | 218 | migration = repo.by_path(repo.path / path) |
| 220 | | - changed_ids.add(migration.id) |
| 221 | | - |
| 222 | | - # Revert all affected migrations (and dependants) before checkout to keep DB consistent. |
| 223 | | - ids_to_revert = missing_ids | changed_ids |
| 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) |
| 224 | 225 | |
| 225 | 226 | # Revert migrations on the current branch before switching branches. |
| 226 | 227 | with connection_for(database_url) as connection: |
| 227 | 228 | runner = Runner(connection) |
| 228 | 229 | runner.revert_migrations( |
| 229 | | - repo.by_ids_with_dependencies(ids_to_revert), |
| 230 | + repo.by_ids_with_dependants(revert_ids), |
| 230 | 231 | before_each=lambda migration, status: rich.print( |
| 231 | 232 | f"[blue]Reverting:[/blue] Migration '{migration.id}' ({status})" |
| 232 | 233 | ), |
| @@ -241,10 +242,10 @@ |
| 241 | 242 | |
| 242 | 243 | # Reload repository state from the target branch and identify migrations to reapply. |
| 243 | 244 | repo_after = Repository(root) |
| 244 | | - reapply_migrations = { |
| 245 | | - *repo_after.by_ids(changed_ids), |
| 246 | | - *repo_after.by_paths(repo_after.path.joinpath(up_script) for up_script in moved_up_scripts), |
| 247 | | - } |
| 245 | + reapply_migrations = repo_after.by_paths( |
| 246 | + repo_after.path.joinpath(up_script) |
| 247 | + for up_script in apply_paths |
| 248 | + ) |
| 248 | 249 | |
| 249 | 250 | # Reapply required migrations on the target branch, including dependencies. |
| 250 | 251 | with connection_for(database_url) as connection: |