Public repository: timo / mitch

Reader

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

Refactor migration ID handling to accept lists instead of variable arguments in repository methods

public

Refactor migration ID handling to accept lists instead of variable arguments in repository methods
Changesetfe6f12450197e69d7d390776a5e9ed412f895c9d
Author (claimed)Timo Stolz <timo.stolz@nullachtvierzehn.de>,
Parentsa4e197e3760e
Children
Pushlog
by timo via ssh

modified src/mitch/cli/deploy.py +4 −4

@@ -31,7 +31,7 @@
3131
     """Deploy the specified migrations.
3232
 
3333
     Args:
34
-        *migration_ids: The IDs of the migrations to deploy.
34
+        migration_ids: The IDs of the migrations to deploy.
3535
     """
3636
 
3737
     # Resolve database URL
@@ -43,7 +43,7 @@
4343
     
4444
     # Fetch due migrations
4545
     repo = Repository(root)
46
-    due_migrations = list(repo.by_ids_with_dependencies(*migration_ids))
46
+    due_migrations = list(repo.by_ids_with_dependencies(migration_ids))
4747
     
4848
     with engine.connect() as connection:
4949
         runner = Runner(connection)
@@ -72,7 +72,7 @@
7272
                 raise typer.Exit(1, f"Unknown if_modified option: {if_modified}")
7373
 
7474
         # Revert modified migrations if needed.
75
-        migrations_to_revert = list(repo.by_ids_with_dependants(*list(m.id for m in migrations_to_redeploy)))
75
+        migrations_to_revert = list(repo.by_ids_with_dependants([m.id for m in migrations_to_redeploy]))
7676
 
7777
         # Ask for confirmation if there are dependants to revert.
7878
         dependants_to_revert = [m for m in migrations_to_revert if m not in migrations_to_redeploy]
@@ -90,7 +90,7 @@
9090
         # Prepare to redeploy modified migrations, together with the 
9191
         if migrations_to_revert:
9292
             due_ids = set(migration_ids) | {m.id for m in migrations_to_revert}
93
-            due_migrations = list(repo.by_ids_with_dependencies(*due_ids))
93
+            due_migrations = list(repo.by_ids_with_dependencies(due_ids))
9494
         
9595
         # Deploy due and reverted migrations
9696
         for batch in batch_transactional_migrations(due_migrations):

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

@@ -50,7 +50,7 @@
5050
             
5151
         # Resolve dependencies
5252
         for migration in self.migrations:
53
-            for dependency in self.by_ids(*migration.dependencies):
53
+            for dependency in self.by_ids(migration.dependencies):
5454
                 dependency.dependants.add(migration.id)
5555
 
5656
     
@@ -68,14 +68,14 @@
6868
         except KeyError:
6969
             raise MissingMigrationError(migration_id)
7070
 
71
-    def by_ids(self, *migration_id: str) -> Iterable[Migration]:
72
-        for id_ in migration_id:
71
+    def by_ids(self, migration_ids: Iterable[str]) -> Iterable[Migration]:
72
+        for id_ in migration_ids:
7373
             yield self.by_id(id_)
7474
 
75
-    def by_ids_with_dependencies(self, *migration_id: str) -> Iterable[Migration]:
76
-        sorter = TopologicalSorter({ m.id: m.dependencies for m in self.by_ids(*migration_id) })
75
+    def by_ids_with_dependencies(self, migration_ids: Iterable[str]) -> Iterable[Migration]:
76
+        sorter = TopologicalSorter({ m.id: m.dependencies for m in self.by_ids(migration_ids) })
7777
         yield from self._iter_sorter(sorter)
7878
 
79
-    def by_ids_with_dependants(self, *migration_id: str) -> Iterable[Migration]:
80
-        sorter = TopologicalSorter({ m.id: m.dependants for m in self.by_ids(*migration_id) })
79
+    def by_ids_with_dependants(self, migration_ids: Iterable[str]) -> Iterable[Migration]:
80
+        sorter = TopologicalSorter({ m.id: m.dependants for m in self.by_ids(migration_ids) })
8181
         yield from self._iter_sorter(sorter)

modified tests/test_repository.py +2 −2

@@ -17,13 +17,13 @@
1717
 
1818
 def test_load_repository_with_dependencies():
1919
     repo = Repository("tests/migrations")
20
-    migrations = list(repo.by_ids_with_dependencies("002-add-column"))
20
+    migrations = list(repo.by_ids_with_dependencies(["002-add-column"]))
2121
     assert migrations[0].id == "001-init"
2222
     assert migrations[1].id == "002-add-column"
2323
 
2424
 
2525
 def test_load_repository_with_dependants():
2626
     repo = Repository("tests/migrations")
27
-    migrations = list(repo.by_ids_with_dependants("001-init"))
27
+    migrations = list(repo.by_ids_with_dependants(["001-init"]))
2828
     assert migrations[0].id == "002-add-column"
2929
     assert migrations[1].id == "001-init"

modified tests/test_runner.py +1 −1

@@ -41,7 +41,7 @@
4141
 
4242
 
4343
 def test_runner_apply_migration(repo, runner, connection):
44
-    due_migrations = list(repo.by_ids_with_dependencies("001-init"))
44
+    due_migrations = list(repo.by_ids_with_dependencies(["001-init"]))
4545
 
4646
     # Check that the migrations are marked as applied
4747
     for migration in due_migrations: