Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

checkout now working with _resolve_repo_path

public

checkout now working with _resolve_repo_path
Changeset9b8402aad90e54f486ecf43598be43779c777283
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsd6d3e1781f9e
Children
Pushlog
by timo via ssh

modified src/mitch/cli/checkout.py +27 −26

@@ -167,6 +167,12 @@
167167
         stem = path.name.removesuffix(".config.toml")
168168
         return path.with_name(f"{stem}.up.sql")
169169
     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()
170176
 
171177
 
172178
 @contextmanager
@@ -229,13 +235,14 @@
229235
 ) -> None:
230236
     """Checkout a branch while reverting/deploying migrations as needed."""
231237
     repo = Repository(root)
238
+    repo_root = repo.path
232239
     git_manager: VersionControlManagerProtocol = GitManager(repo.path)
233240
     
234241
     # Ensure branch checkout is feasible before touching the database state.
235242
     git_manager.can_checkout(branch, allow_dirty)
236243
     should_pop_stash = allow_dirty and git_manager.is_dirty()
237244
 
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.
239246
     up_modified: set[Migration] = set()
240247
     up_deleted: set[Migration] = set()
241248
 
@@ -250,37 +257,41 @@
250257
     for entry in git_manager.changes(branch):
251258
         match entry:
252259
             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)))
254261
             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)))
256263
             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)))
258265
             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)))
260267
             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)
264273
             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
267278
                 # Redeploys should not be triggered here, but by the up script path above.
268279
             # FIXME: Added migrations should only be applied if requested. 
269280
             # For now, we assume all adds are intended to be applied, basically matching the behavior of `mitch up` on new files.
270281
             # But maybe, we should be closer to a `mitch pick` and only apply what the user explicitly wanted to add.
271282
             case Add(path) if path.suffix == '.sql':
272
-                new_paths.add(path)
283
+                new_paths.add(_resolve_repo_path(path, repo_root))
273284
             case Add(path) if path.suffix == '.toml':
274285
                 try:
275286
                     # Maybe, we already have a migration with an up script, but no config yet.
276287
                     # 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)))
278289
                 except MissingMigrationError:
279290
                     pass
280291
             case Copied(_, new_path) if new_path.suffix == '.sql':
281292
                 # Copies are treated as additions on the target branch, 
282293
                 # 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))
284295
     
285296
     # Revert migrations on the current branch before switching branches.
286297
     with connection_for(database_url) as connection:
@@ -293,8 +304,9 @@
293304
         ))
294305
         
295306
         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))
298310
 
299311
 
300312
     # For dirty worktrees, stash only after DB reverts so reverts reflect current local files.
@@ -306,17 +318,6 @@
306318
         repo_after = Repository(root)
307319
         apply_migrations = { repo_after.by_path(path) for path in redeploy_by_path }
308320
         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
320321
 
321322
         # Reapply required migrations on the target branch, including dependencies.
322323
         with connection_for(database_url) as connection:

modified src/mitch/repository.py +7 −1

@@ -133,10 +133,16 @@
133133
             raise TypeError(f"Item must be a string, Path, or Migration, got {type(item)!r}")
134134
 
135135
     def by_path(self, migration_path: Path | str) -> Migration:
136
-        migration_path = Path(migration_path).resolve()
136
+        migration_path = Path(migration_path)
137
+        if migration_path.is_absolute():
138
+            migration_path = migration_path.resolve()
139
+        else:
140
+            migration_path = (self.path / migration_path).resolve()
141
+        
137142
         for migration in self.migrations:
138143
             if migration_path in (migration.up_script, migration.down_script, migration.config_file):
139144
                 return migration
145
+        
140146
         raise MissingMigrationError(f"No migration found for path: {migration_path}")
141147
 
142148
     def by_paths(self, migration_paths: Iterable[Path | str]) -> Iterable[Migration]: