Improve descriptions for CLI commands. Refactor down/drop.
public
Improve descriptions for CLI commands. Refactor down/drop.
| Changeset | cae29b089ed025233df6cf155c79116fcbf1b9f0 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 78239ca0d1e6 |
| Children | |
| Pushlog | by timo via ssh |
modified src/mitch/cli/create.py +1 −1
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | typer.Option("--skip-downscript", "-sd", help="Skip creating a downscript for the migration.") |
| 26 | 26 | ] = False, |
| 27 | 27 | ) -> None: |
| 28 | - """Create a new migration in the repository. | |
| 28 | + """Create a new migration scaffold in the repository. | |
| 29 | 29 | |
| 30 | 30 | Args: |
| 31 | 31 | migration_id: The ID of the new migration. |
modified src/mitch/cli/deploy.py +2 −2
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | typer.Option("--watch", help="Watch the repository and re-run deployments on changes."), |
| 44 | 44 | ] = False, |
| 45 | 45 | ) -> None: |
| 46 | - """Deploy the specified migrations in the repository.""" | |
| 46 | + """Apply the specified migrations in the repository.""" | |
| 47 | 47 | |
| 48 | 48 | repo = Repository(root) |
| 49 | 49 | covered_migrations = set(repo.by_ids_with_dependencies(migration_ids)) |
| @@ -177,7 +177,7 @@ | ||
| 177 | 177 | typer.Option("--watch", help="Watch the repository and re-run deployments on changes."), |
| 178 | 178 | ] = False, |
| 179 | 179 | ) -> None: |
| 180 | - """Deploy all migrations in the repository.""" | |
| 180 | + """Apply all pending migrations in the repository.""" | |
| 181 | 181 | |
| 182 | 182 | repo = Repository(root) |
| 183 | 183 | 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 | |
| 2 | 2 | from pathlib import Path |
| 3 | 3 | |
| 4 | 4 | import typer |
| @@ -6,30 +6,43 @@ | ||
| 6 | 6 | |
| 7 | 7 | from ..repository import Repository |
| 8 | 8 | from ..runner import Runner |
| 9 | -from ..helpers import create_engine | |
| 9 | +from ..helpers import connection_for | |
| 10 | 10 | |
| 11 | 11 | |
| 12 | 12 | app = typer.Typer(help="Revert migrations in the repository.") |
| 13 | 13 | |
| 14 | 14 | |
| 15 | -@app.command(name="down") | |
| 16 | -@app.command(name="drop") | |
| 15 | +@app.command(name="down", help="Revert all applied migrations in the repository.") | |
| 17 | 16 | def down( |
| 18 | 17 | root: Annotated[Path, typer.Option("--repository", "-r", help="Repository directory.", default_factory=lambda: Path.cwd(), dir_okay=True, file_okay=False, exists=True)], |
| 19 | 18 | 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.""" | |
| 21 | 21 | repo = Repository(root) |
| 22 | - engine = create_engine(database_url) | |
| 22 | + drop([migration.id for migration in repo.migrations], root, database_url) | |
| 23 | 23 | |
| 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: | |
| 25 | 37 | 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 | + ) | |
| 28 | 43 | runner.revert_migrations( |
| 29 | 44 | applied_migrations, |
| 30 | 45 | before_each=lambda migration: rich.print( |
| 31 | 46 | f"[blue]Reverting:[/blue] Migration '{migration.id}'" |
| 32 | 47 | ), |
| 33 | 48 | ) |
| 34 | - | |
| 35 | - engine.dispose() | |
modified tests/test_cli.py +84 −1
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | result = runner.invoke(app, ["add", "--help"]) |
| 16 | 16 | |
| 17 | 17 | 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 | |
| 19 | 19 | |
| 20 | 20 | |
| 21 | 21 | def test_up_help_includes_watch_options(): |
| @@ -23,6 +23,17 @@ | ||
| 23 | 23 | |
| 24 | 24 | assert result.exit_code == 0 |
| 25 | 25 | 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 | |
| 26 | 37 | |
| 27 | 38 | |
| 28 | 39 | def test_add_applies_migration_to_sqlite(tmp_path): |
| @@ -188,3 +199,75 @@ | ||
| 188 | 199 | ).fetchall() |
| 189 | 200 | |
| 190 | 201 | 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 | |