Public repository: timo / mitch

Reader

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

Improve descriptions for CLI commands. Refactor down/drop.

public

Improve descriptions for CLI commands. Refactor down/drop.
Changesetcae29b089ed025233df6cf155c79116fcbf1b9f0
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents78239ca0d1e6
Children
Pushlog
by timo via ssh

modified src/mitch/cli/create.py +1 −1

@@ -25,7 +25,7 @@
2525
         typer.Option("--skip-downscript", "-sd", help="Skip creating a downscript for the migration.")
2626
     ] = False,
2727
 ) -> None:
28
-    """Create a new migration in the repository.
28
+    """Create a new migration scaffold in the repository.
2929
 
3030
     Args:
3131
         migration_id: The ID of the new migration.

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

@@ -43,7 +43,7 @@
4343
         typer.Option("--watch", help="Watch the repository and re-run deployments on changes."),
4444
     ] = False,
4545
 ) -> None:
46
-    """Deploy the specified migrations in the repository."""
46
+    """Apply the specified migrations in the repository."""
4747
 
4848
     repo = Repository(root)
4949
     covered_migrations = set(repo.by_ids_with_dependencies(migration_ids))
@@ -177,7 +177,7 @@
177177
         typer.Option("--watch", help="Watch the repository and re-run deployments on changes."),
178178
     ] = False,
179179
 ) -> None:
180
-    """Deploy all migrations in the repository."""
180
+    """Apply all pending migrations in the repository."""
181181
 
182182
     repo = Repository(root)
183183
     add(ctx, [m.id for m in repo.migrations], root, database_url, if_modified, watch)

modified src/mitch/cli/revert.py +24 −11

@@ -1,4 +1,4 @@
1
-from typing import Annotated
1
+from typing import Annotated, Iterable
22
 from pathlib import Path
33
 
44
 import typer
@@ -6,30 +6,43 @@
66
 
77
 from ..repository import Repository
88
 from ..runner import Runner
9
-from ..helpers import create_engine
9
+from ..helpers import connection_for
1010
 
1111
 
1212
 app = typer.Typer(help="Revert migrations in the repository.")
1313
 
1414
 
15
-@app.command(name="down")
16
-@app.command(name="drop")
15
+@app.command(name="down", help="Revert all applied migrations in the repository.")
1716
 def down(
1817
     root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)],
1918
     database_url: Annotated[str | None, typer.Option("--database", "-d", help="Database URL. Can also be set via MITCH_DATABASE_URL or DATABASE_URL environment variable.")] = None,
20
-):
19
+) -> None:
20
+    """Revert all applied migrations in the repository."""
2121
     repo = Repository(root)
22
-    engine = create_engine(database_url)
22
+    drop([migration.id for migration in repo.migrations], root, database_url)
2323
 
24
-    with engine.connect() as connection:
24
+
25
+@app.command(
26
+    name="drop",
27
+    help="Revert the specified migrations and their dependants in the repository.",
28
+)
29
+def drop(
30
+    migration_ids: list[str],
31
+    root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)],
32
+    database_url: Annotated[str | None, typer.Option("--database", "-d", help="Database URL. Can also be set via MITCH_DATABASE_URL or DATABASE_URL environment variable.")] = None,
33
+) -> None:
34
+    """Revert the specified migrations and their dependants in the repository."""
35
+    repo = Repository(root)
36
+    with connection_for(database_url) as connection:
2537
         runner = Runner(connection)
26
-        ids = [m.id for m in repo.migrations]
27
-        applied_migrations = list(m for m in repo.by_ids_with_dependants(ids) if runner.deployment_status(m) in ('applied', 'modified'))
38
+        applied_migrations = list(
39
+            migration
40
+            for migration in repo.by_ids_with_dependants(migration_ids)
41
+            if runner.deployment_status(migration) in ("applied", "modified")
42
+        )
2843
         runner.revert_migrations(
2944
             applied_migrations,
3045
             before_each=lambda migration: rich.print(
3146
                 f"[blue]Reverting:[/blue] Migration '{migration.id}'"
3247
             ),
3348
         )
34
-
35
-    engine.dispose()

modified tests/test_cli.py +84 −1

@@ -15,7 +15,7 @@
1515
     result = runner.invoke(app, ["add", "--help"])
1616
 
1717
     assert result.exit_code == 0
18
-    assert "Deploy the specified migrations in the repository." in result.output
18
+    assert "Apply the specified migrations in the repository." in result.output
1919
 
2020
 
2121
 def test_up_help_includes_watch_options():
@@ -23,6 +23,17 @@
2323
 
2424
     assert result.exit_code == 0
2525
     assert "--watch" in result.output
26
+
27
+
28
+def test_down_and_drop_help_have_distinct_descriptions():
29
+    down_help = runner.invoke(app, ["down", "--help"])
30
+    drop_help = runner.invoke(app, ["drop", "--help"])
31
+
32
+    assert down_help.exit_code == 0
33
+    assert "Revert all applied migrations in the repository." in down_help.output
34
+
35
+    assert drop_help.exit_code == 0
36
+    assert "Revert the specified migrations and their dependants in the repository." in drop_help.output
2637
 
2738
 
2839
 def test_add_applies_migration_to_sqlite(tmp_path):
@@ -188,3 +199,75 @@
188199
         ).fetchall()
189200
 
190201
     assert [row[0] for row in rows] == ["001-init", "002-add-column"]
202
+
203
+
204
+def test_drop_reverts_only_selected_migration(tmp_path):
205
+    repo_path = tmp_path / "migrations"
206
+    repo_path.mkdir()
207
+
208
+    (repo_path / "001-init.up.sql").write_text(
209
+        "CREATE TABLE a (id int);",
210
+        encoding="utf-8",
211
+    )
212
+    (repo_path / "001-init.down.sql").write_text(
213
+        "DROP TABLE a;",
214
+        encoding="utf-8",
215
+    )
216
+
217
+    (repo_path / "002-next.up.sql").write_text(
218
+        "CREATE TABLE b (id int);",
219
+        encoding="utf-8",
220
+    )
221
+    (repo_path / "002-next.down.sql").write_text(
222
+        "DROP TABLE b;",
223
+        encoding="utf-8",
224
+    )
225
+    (repo_path / "002-next.config.toml").write_text(
226
+        'dependencies = ["001-init"]\n',
227
+        encoding="utf-8",
228
+    )
229
+
230
+    db_path = tmp_path / "test.db"
231
+    database_url = f"sqlite:///{db_path}"
232
+
233
+    up_result = runner.invoke(
234
+        app,
235
+        [
236
+            "up",
237
+            "--repository",
238
+            str(repo_path),
239
+            "--database",
240
+            database_url,
241
+            "--if-modified",
242
+            "ignore",
243
+        ],
244
+    )
245
+    assert up_result.exit_code == 0
246
+
247
+    drop_result = runner.invoke(
248
+        app,
249
+        [
250
+            "drop",
251
+            "002-next",
252
+            "--repository",
253
+            str(repo_path),
254
+            "--database",
255
+            database_url,
256
+        ],
257
+    )
258
+    assert drop_result.exit_code == 0
259
+
260
+    with sqlite3.connect(db_path) as connection:
261
+        rows = connection.execute(
262
+            "SELECT id FROM mitch_migrations ORDER BY id",
263
+        ).fetchall()
264
+        table_a = connection.execute(
265
+            "SELECT name FROM sqlite_schema WHERE type='table' AND name='a'",
266
+        ).fetchone()
267
+        table_b = connection.execute(
268
+            "SELECT name FROM sqlite_schema WHERE type='table' AND name='b'",
269
+        ).fetchone()
270
+
271
+    assert [row[0] for row in rows] == ["001-init"]
272
+    assert table_a is not None
273
+    assert table_b is None