| @@ -167,6 +167,12 @@ |
| 167 | 167 | stem = path.name.removesuffix(".config.toml") |
| 168 | 168 | return path.with_name(f"{stem}.up.sql") |
| 169 | 169 | return None |
| 170 | + |
| 171 | + |
| 172 | +def _resolve_repo_path(path: Path, repo_root: Path) -> Path: |
| 173 | + if path.is_absolute(): |
| 174 | + return path.resolve() |
| 175 | + return (repo_root / path).resolve() |
| 170 | 176 | |
| 171 | 177 | |
| 172 | 178 | @contextmanager |
| @@ -229,13 +235,14 @@ |
| 229 | 235 | ) -> None: |
| 230 | 236 | """Checkout a branch while reverting/deploying migrations as needed.""" |
| 231 | 237 | repo = Repository(root) |
| 238 | + repo_root = repo.path |
| 232 | 239 | git_manager: VersionControlManagerProtocol = GitManager(repo.path) |
| 233 | 240 | |
| 234 | 241 | # Ensure branch checkout is feasible before touching the database state. |
| 235 | 242 | git_manager.can_checkout(branch, allow_dirty) |
| 236 | 243 | should_pop_stash = allow_dirty and git_manager.is_dirty() |
| 237 | 244 | |
| 238 | | - # Keep paths relative to repository root; this avoids cwd-dependent resolution later. |
| 245 | + # Keep all paths absolute and rooted at the repository path to avoid cwd-dependent lookups. |
| 239 | 246 | up_modified: set[Migration] = set() |
| 240 | 247 | up_deleted: set[Migration] = set() |
| 241 | 248 | |
| @@ -250,37 +257,41 @@ |
| 250 | 257 | for entry in git_manager.changes(branch): |
| 251 | 258 | match entry: |
| 252 | 259 | case Deleted(path) if path.suffix == '.sql': |
| 253 | | - up_deleted.add(repo.by_path(path)) |
| 260 | + up_deleted.add(repo.by_path(_resolve_repo_path(path, repo_root))) |
| 254 | 261 | case Deleted(path) if path.suffix == '.toml': |
| 255 | | - config_deleted.add(repo.by_path(path)) |
| 262 | + config_deleted.add(repo.by_path(_resolve_repo_path(path, repo_root))) |
| 256 | 263 | case Modified(path) if path.suffix == '.sql': |
| 257 | | - up_modified.add(repo.by_path(path)) |
| 264 | + up_modified.add(repo.by_path(_resolve_repo_path(path, repo_root))) |
| 258 | 265 | case Modified(path) if path.suffix == '.toml': |
| 259 | | - config_modified.add(repo.by_path(path)) |
| 266 | + config_modified.add(repo.by_path(_resolve_repo_path(path, repo_root))) |
| 260 | 267 | 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) |
| 268 | + old_abs = _resolve_repo_path(old_path, repo_root) |
| 269 | + new_abs = _resolve_repo_path(new_path, repo_root) |
| 270 | + up_deleted.add(repo.by_path(old_abs)) |
| 271 | + moved_paths[old_abs] = new_abs |
| 272 | + redeploy_by_path.add(new_abs) |
| 264 | 273 | 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 |
| 274 | + old_abs = _resolve_repo_path(old_path, repo_root) |
| 275 | + new_abs = _resolve_repo_path(new_path, repo_root) |
| 276 | + config_deleted.add(repo.by_path(old_abs)) |
| 277 | + moved_paths[old_abs] = new_abs |
| 267 | 278 | # Redeploys should not be triggered here, but by the up script path above. |
| 268 | 279 | # FIXME: Added migrations should only be applied if requested. |
| 269 | 280 | # For now, we assume all adds are intended to be applied, basically matching the behavior of `mitch up` on new files. |
| 270 | 281 | # But maybe, we should be closer to a `mitch pick` and only apply what the user explicitly wanted to add. |
| 271 | 282 | case Add(path) if path.suffix == '.sql': |
| 272 | | - new_paths.add(path) |
| 283 | + new_paths.add(_resolve_repo_path(path, repo_root)) |
| 273 | 284 | case Add(path) if path.suffix == '.toml': |
| 274 | 285 | try: |
| 275 | 286 | # Maybe, we already have a migration with an up script, but no config yet. |
| 276 | 287 | # 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'))) |
| 288 | + config_added.add(repo.by_path(_resolve_repo_path(path.with_suffix('').with_suffix('.up.sql'), repo_root))) |
| 278 | 289 | except MissingMigrationError: |
| 279 | 290 | pass |
| 280 | 291 | case Copied(_, new_path) if new_path.suffix == '.sql': |
| 281 | 292 | # Copies are treated as additions on the target branch, |
| 282 | 293 | # 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) |
| 294 | + new_paths.add(_resolve_repo_path(new_path, repo_root)) |
| 284 | 295 | |
| 285 | 296 | # Revert migrations on the current branch before switching branches. |
| 286 | 297 | with connection_for(database_url) as connection: |
| @@ -293,8 +304,9 @@ |
| 293 | 304 | )) |
| 294 | 305 | |
| 295 | 306 | 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)) |
| 307 | + if not migration in up_deleted: |
| 308 | + old_path = _resolve_repo_path(migration.up_script, repo_root) |
| 309 | + redeploy_by_path.add(moved_paths.get(old_path, old_path)) |
| 298 | 310 | |
| 299 | 311 | |
| 300 | 312 | # For dirty worktrees, stash only after DB reverts so reverts reflect current local files. |
| @@ -306,17 +318,6 @@ |
| 306 | 318 | repo_after = Repository(root) |
| 307 | 319 | apply_migrations = { repo_after.by_path(path) for path in redeploy_by_path } |
| 308 | 320 | 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 | |
| 321 | 322 | # Reapply required migrations on the target branch, including dependencies. |
| 322 | 323 | with connection_for(database_url) as connection: |