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
modified
src/mitch/cli/deploy.py
+4 −4
| @@ -31,7 +31,7 @@ |
| 31 | 31 | """Deploy the specified migrations. |
| 32 | 32 | |
| 33 | 33 | Args: |
| 34 | | - *migration_ids: The IDs of the migrations to deploy. |
| 34 | + migration_ids: The IDs of the migrations to deploy. |
| 35 | 35 | """ |
| 36 | 36 | |
| 37 | 37 | # Resolve database URL |
| @@ -43,7 +43,7 @@ |
| 43 | 43 | |
| 44 | 44 | # Fetch due migrations |
| 45 | 45 | 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)) |
| 47 | 47 | |
| 48 | 48 | with engine.connect() as connection: |
| 49 | 49 | runner = Runner(connection) |
| @@ -72,7 +72,7 @@ |
| 72 | 72 | raise typer.Exit(1, f"Unknown if_modified option: {if_modified}") |
| 73 | 73 | |
| 74 | 74 | # 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])) |
| 76 | 76 | |
| 77 | 77 | # Ask for confirmation if there are dependants to revert. |
| 78 | 78 | dependants_to_revert = [m for m in migrations_to_revert if m not in migrations_to_redeploy] |
| @@ -90,7 +90,7 @@ |
| 90 | 90 | # Prepare to redeploy modified migrations, together with the |
| 91 | 91 | if migrations_to_revert: |
| 92 | 92 | 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)) |
| 94 | 94 | |
| 95 | 95 | # Deploy due and reverted migrations |
| 96 | 96 | for batch in batch_transactional_migrations(due_migrations): |
modified
src/mitch/repository.py
+7 −7
| @@ -50,7 +50,7 @@ |
| 50 | 50 | |
| 51 | 51 | # Resolve dependencies |
| 52 | 52 | for migration in self.migrations: |
| 53 | | - for dependency in self.by_ids(*migration.dependencies): |
| 53 | + for dependency in self.by_ids(migration.dependencies): |
| 54 | 54 | dependency.dependants.add(migration.id) |
| 55 | 55 | |
| 56 | 56 | |
| @@ -68,14 +68,14 @@ |
| 68 | 68 | except KeyError: |
| 69 | 69 | raise MissingMigrationError(migration_id) |
| 70 | 70 | |
| 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: |
| 73 | 73 | yield self.by_id(id_) |
| 74 | 74 | |
| 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) }) |
| 77 | 77 | yield from self._iter_sorter(sorter) |
| 78 | 78 | |
| 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) }) |
| 81 | 81 | yield from self._iter_sorter(sorter) |
modified
tests/test_repository.py
+2 −2
| @@ -17,13 +17,13 @@ |
| 17 | 17 | |
| 18 | 18 | def test_load_repository_with_dependencies(): |
| 19 | 19 | 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"])) |
| 21 | 21 | assert migrations[0].id == "001-init" |
| 22 | 22 | assert migrations[1].id == "002-add-column" |
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | def test_load_repository_with_dependants(): |
| 26 | 26 | repo = Repository("tests/migrations") |
| 27 | | - migrations = list(repo.by_ids_with_dependants("001-init")) |
| 27 | + migrations = list(repo.by_ids_with_dependants(["001-init"])) |
| 28 | 28 | assert migrations[0].id == "002-add-column" |
| 29 | 29 | assert migrations[1].id == "001-init" |
modified
tests/test_runner.py
+1 −1
| @@ -41,7 +41,7 @@ |
| 41 | 41 | |
| 42 | 42 | |
| 43 | 43 | 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"])) |
| 45 | 45 | |
| 46 | 46 | # Check that the migrations are marked as applied |
| 47 | 47 | for migration in due_migrations: |