Public repository: timo / mitch

Reader

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

Improve checkout.py after review

public

Improve checkout.py after review
Changesete3e7e8770aebcd64d51aa029983303524e739544
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentse81b1f23e1ab
Children
Pushlog
by timo via ssh

modified src/mitch/cli/checkout.py +18 −17

@@ -199,34 +199,35 @@
199199
     should_pop_stash = allow_dirty and git_manager.is_dirty()
200200
 
201201
     # 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()
205204
 
206205
     # Classify migrations affected by the branch switch:
207206
     # - missing_ids: up-script disappears on target branch
208207
     # - changed_ids: up/config content or path differs and should be redeployed
209208
     for entry in git_manager.changes(branch):
210209
         match entry:
211
-            case Deleted(path) if path.name.endswith(".up.sql"):
210
+            case Deleted(path):
212211
                 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):
215214
                 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)
218217
             case Modified(path):
219218
                 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)
224225
     
225226
     # Revert migrations on the current branch before switching branches.
226227
     with connection_for(database_url) as connection:
227228
         runner = Runner(connection)
228229
         runner.revert_migrations(
229
-            repo.by_ids_with_dependencies(ids_to_revert),
230
+            repo.by_ids_with_dependants(revert_ids),
230231
             before_each=lambda migration, status: rich.print(
231232
                 f"[blue]Reverting:[/blue] Migration '{migration.id}' ({status})"
232233
             ),
@@ -241,10 +242,10 @@
241242
 
242243
     # Reload repository state from the target branch and identify migrations to reapply.
243244
     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
+    )
248249
     
249250
     # Reapply required migrations on the target branch, including dependencies.
250251
     with connection_for(database_url) as connection:

modified src/mitch/helpers.py +1 −0

@@ -45,5 +45,6 @@
4545
     try:
4646
         with engine.connect() as connection:
4747
             yield connection
48
+        connection.close()
4849
     finally:
4950
         engine.dispose()