| 1 | import sqlite3 |
| 2 | import subprocess |
| 3 | import tomllib |
| 4 | from datetime import datetime |
| 5 | from pathlib import Path |
| 6 | |
| 7 | from typer.testing import CliRunner |
| 8 | |
| 9 | from mitch.cli import app |
| 10 | from mitch.project import Project |
| 11 | from mitch.watcher import EventType |
| 12 | |
| 13 | runner = CliRunner() |
| 14 | |
| 15 | |
| 16 | def _write_project_config(project_root, project_id="tests-cli-project"): |
| 17 | (project_root / "mitch.toml").write_text(f'[project]\nid = "{project_id}"\n', encoding="utf-8") |
| 18 | |
| 19 | |
| 20 | def _run_git(cwd, *args): |
| 21 | result = subprocess.run( |
| 22 | ["git", *args], |
| 23 | cwd=cwd, |
| 24 | capture_output=True, |
| 25 | text=True, |
| 26 | check=False, |
| 27 | ) |
| 28 | assert result.returncode == 0, result.stderr or result.stdout |
| 29 | return result.stdout.strip() |
| 30 | |
| 31 | |
| 32 | def _init_git_repo(path): |
| 33 | _run_git(path, "init") |
| 34 | _run_git(path, "config", "user.email", "test@example.com") |
| 35 | _run_git(path, "config", "user.name", "Test User") |
| 36 | |
| 37 | |
| 38 | def _write_status_fixture_migrations(repo_path): |
| 39 | _write_project_config(repo_path, "status-fixture") |
| 40 | (repo_path / "001-init.up.sql").write_text( |
| 41 | "CREATE TABLE table_one (id int);", |
| 42 | encoding="utf-8", |
| 43 | ) |
| 44 | (repo_path / "001-init.down.sql").write_text( |
| 45 | "DROP TABLE table_one;", |
| 46 | encoding="utf-8", |
| 47 | ) |
| 48 | (repo_path / "002-next.up.sql").write_text( |
| 49 | "CREATE TABLE table_two (id int);", |
| 50 | encoding="utf-8", |
| 51 | ) |
| 52 | (repo_path / "002-next.down.sql").write_text( |
| 53 | "DROP TABLE table_two;", |
| 54 | encoding="utf-8", |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | def test_add_help(): |
| 59 | result = runner.invoke(app, ["add", "--help"]) |
| 60 | |
| 61 | assert result.exit_code == 0 |
| 62 | assert "Apply the specified migrations in the project." in result.output |
| 63 | |
| 64 | |
| 65 | def test_up_help_includes_watch_options(): |
| 66 | result = runner.invoke(app, ["up", "--help"]) |
| 67 | |
| 68 | assert result.exit_code == 0 |
| 69 | assert "--watch" in result.output |
| 70 | |
| 71 | |
| 72 | def test_down_and_drop_help_have_distinct_descriptions(): |
| 73 | down_help = runner.invoke(app, ["down", "--help"]) |
| 74 | drop_help = runner.invoke(app, ["drop", "--help"]) |
| 75 | |
| 76 | assert down_help.exit_code == 0 |
| 77 | assert "Revert all applied migrations in the project." in down_help.output |
| 78 | |
| 79 | assert drop_help.exit_code == 0 |
| 80 | assert "Revert the specified migrations and their dependants in the project." in drop_help.output |
| 81 | |
| 82 | |
| 83 | def test_stash_help_mentions_stashing_behavior(): |
| 84 | result = runner.invoke(app, ["stash", "--help"]) |
| 85 | |
| 86 | assert result.exit_code == 0 |
| 87 | assert "--vcs" in result.output |
| 88 | |
| 89 | |
| 90 | def test_status_help_mentions_ci_and_strict_options(): |
| 91 | result = runner.invoke(app, ["status", "--help"]) |
| 92 | |
| 93 | assert result.exit_code == 0 |
| 94 | assert "--ci" in result.output |
| 95 | assert "--strict" in result.output |
| 96 | |
| 97 | |
| 98 | def test_init_creates_mitch_toml_in_current_working_directory(): |
| 99 | with runner.isolated_filesystem(): |
| 100 | result = runner.invoke(app, ["init", "my-project"]) |
| 101 | |
| 102 | assert result.exit_code == 0 |
| 103 | config = tomllib.loads(Path("mitch.toml").read_text(encoding="utf-8")) |
| 104 | assert config["project"]["id"] == "my-project" |
| 105 | |
| 106 | |
| 107 | def test_init_refuses_to_overwrite_existing_mitch_toml(): |
| 108 | with runner.isolated_filesystem(): |
| 109 | Path("mitch.toml").write_text('[project]\nid = "existing"\n', encoding="utf-8") |
| 110 | |
| 111 | result = runner.invoke(app, ["init", "new-id"]) |
| 112 | |
| 113 | assert result.exit_code == 1 |
| 114 | assert "already exists" in result.output |
| 115 | |
| 116 | |
| 117 | def test_add_applies_migration_to_sqlite(tmp_path): |
| 118 | db_path = tmp_path / "test.db" |
| 119 | database_url = f"sqlite:///{db_path}" |
| 120 | |
| 121 | result = runner.invoke( |
| 122 | app, |
| 123 | [ |
| 124 | "add", |
| 125 | "001-init", |
| 126 | "--project", |
| 127 | "tests/migrations", |
| 128 | "--database", |
| 129 | database_url, |
| 130 | "--if-modified", |
| 131 | "ignore", |
| 132 | ], |
| 133 | ) |
| 134 | |
| 135 | assert result.exit_code == 0 |
| 136 | assert "Deploying:" in result.output |
| 137 | |
| 138 | with sqlite3.connect(db_path) as connection: |
| 139 | row = connection.execute( |
| 140 | "SELECT id FROM mitch_migrations WHERE id = ?", |
| 141 | ("001-init",), |
| 142 | ).fetchone() |
| 143 | |
| 144 | assert row is not None |
| 145 | |
| 146 | |
| 147 | def test_create_generates_migration_files(tmp_path): |
| 148 | _write_project_config(tmp_path, "create-generated") |
| 149 | result = runner.invoke( |
| 150 | app, |
| 151 | [ |
| 152 | "create", |
| 153 | "001-init", |
| 154 | "--project", |
| 155 | str(tmp_path), |
| 156 | ], |
| 157 | ) |
| 158 | |
| 159 | assert result.exit_code == 0 |
| 160 | |
| 161 | up_script = tmp_path / "001-init.up.sql" |
| 162 | down_script = tmp_path / "001-init.down.sql" |
| 163 | config_file = tmp_path / "001-init.config.toml" |
| 164 | |
| 165 | assert up_script.exists() |
| 166 | assert down_script.exists() |
| 167 | assert config_file.exists() |
| 168 | |
| 169 | config = tomllib.loads(config_file.read_text(encoding="utf-8")) |
| 170 | assert config["id"] == "001-init" |
| 171 | assert isinstance(config["created_at"], datetime) |
| 172 | assert config["created_at"].tzinfo is not None |
| 173 | |
| 174 | |
| 175 | def test_create_populates_up_script_from_stdin(tmp_path): |
| 176 | _write_project_config(tmp_path, "create-stdin-up") |
| 177 | up_sql = "CREATE TABLE stdin_only (id int);\n" |
| 178 | result = runner.invoke( |
| 179 | app, |
| 180 | [ |
| 181 | "create", |
| 182 | "001-init", |
| 183 | "--project", |
| 184 | str(tmp_path), |
| 185 | ], |
| 186 | input=up_sql, |
| 187 | ) |
| 188 | |
| 189 | assert result.exit_code == 0 |
| 190 | assert (tmp_path / "001-init.up.sql").read_text(encoding="utf-8") == up_sql |
| 191 | assert (tmp_path / "001-init.down.sql").read_text(encoding="utf-8") == "" |
| 192 | |
| 193 | |
| 194 | def test_create_populates_up_and_down_scripts_from_stdin_sections(tmp_path): |
| 195 | _write_project_config(tmp_path, "create-stdin-sections") |
| 196 | payload = ( |
| 197 | "-- up.sql\n" |
| 198 | "CREATE TABLE stdin_split (id int);\n" |
| 199 | "-- down.sql\n" |
| 200 | "DROP TABLE stdin_split;\n" |
| 201 | ) |
| 202 | result = runner.invoke( |
| 203 | app, |
| 204 | [ |
| 205 | "create", |
| 206 | "001-init", |
| 207 | "--project", |
| 208 | str(tmp_path), |
| 209 | ], |
| 210 | input=payload, |
| 211 | ) |
| 212 | |
| 213 | assert result.exit_code == 0 |
| 214 | assert (tmp_path / "001-init.up.sql").read_text(encoding="utf-8") == "CREATE TABLE stdin_split (id int);\n" |
| 215 | assert (tmp_path / "001-init.down.sql").read_text(encoding="utf-8") == "DROP TABLE stdin_split;\n" |
| 216 | |
| 217 | |
| 218 | def test_create_rejects_missing_dependencies(tmp_path): |
| 219 | _write_project_config(tmp_path, "create-missing-deps") |
| 220 | result = runner.invoke( |
| 221 | app, |
| 222 | [ |
| 223 | "create", |
| 224 | "001-init", |
| 225 | "--project", |
| 226 | str(tmp_path), |
| 227 | "--dependency", |
| 228 | "missing-migration", |
| 229 | ], |
| 230 | ) |
| 231 | |
| 232 | assert result.exit_code == 1 |
| 233 | assert "Missing dependencies" in result.output |
| 234 | |
| 235 | |
| 236 | def test_create_respects_skip_flags(tmp_path): |
| 237 | _write_project_config(tmp_path, "create-skip") |
| 238 | result = runner.invoke( |
| 239 | app, |
| 240 | [ |
| 241 | "create", |
| 242 | "001-init", |
| 243 | "--project", |
| 244 | str(tmp_path), |
| 245 | "--skip-config", |
| 246 | "--skip-downscript", |
| 247 | ], |
| 248 | ) |
| 249 | |
| 250 | assert result.exit_code == 0 |
| 251 | assert (tmp_path / "001-init.up.sql").exists() |
| 252 | assert not (tmp_path / "001-init.down.sql").exists() |
| 253 | assert not (tmp_path / "001-init.config.toml").exists() |
| 254 | |
| 255 | |
| 256 | def test_up_watch_applies_new_migration(tmp_path, monkeypatch): |
| 257 | repo_path = tmp_path / "migrations" |
| 258 | repo_path.mkdir() |
| 259 | _write_project_config(repo_path) |
| 260 | (repo_path / "001-init.up.sql").write_text( |
| 261 | "CREATE TABLE some_table (id int);", |
| 262 | encoding="utf-8", |
| 263 | ) |
| 264 | |
| 265 | db_path = tmp_path / "test.db" |
| 266 | database_url = f"sqlite:///{db_path}" |
| 267 | |
| 268 | def fake_watch_for_changes(self): |
| 269 | (repo_path / "002-add.up.sql").write_text( |
| 270 | "ALTER TABLE some_table ADD COLUMN extra int;", |
| 271 | encoding="utf-8", |
| 272 | ) |
| 273 | yield [(EventType.CREATED, repo_path / "002-add.up.sql")] |
| 274 | |
| 275 | monkeypatch.setattr(Project, "watch_for_changes", fake_watch_for_changes) |
| 276 | |
| 277 | result = runner.invoke( |
| 278 | app, |
| 279 | [ |
| 280 | "up", |
| 281 | "--watch", |
| 282 | "--project", |
| 283 | str(repo_path), |
| 284 | "--database", |
| 285 | database_url, |
| 286 | "--if-modified", |
| 287 | "ignore", |
| 288 | ], |
| 289 | ) |
| 290 | |
| 291 | assert result.exit_code == 0 |
| 292 | |
| 293 | with sqlite3.connect(db_path) as connection: |
| 294 | rows = connection.execute( |
| 295 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 296 | ).fetchall() |
| 297 | |
| 298 | assert [row[0] for row in rows] == ["001-init", "002-add"] |
| 299 | |
| 300 | |
| 301 | def test_up_applies_all_migrations_to_sqlite(tmp_path): |
| 302 | db_path = tmp_path / "test.db" |
| 303 | database_url = f"sqlite:///{db_path}" |
| 304 | |
| 305 | result = runner.invoke( |
| 306 | app, |
| 307 | [ |
| 308 | "up", |
| 309 | "--project", |
| 310 | "tests/migrations", |
| 311 | "--database", |
| 312 | database_url, |
| 313 | "--if-modified", |
| 314 | "ignore", |
| 315 | ], |
| 316 | ) |
| 317 | |
| 318 | assert result.exit_code == 0 |
| 319 | assert "Deploying:" in result.output |
| 320 | |
| 321 | with sqlite3.connect(db_path) as connection: |
| 322 | rows = connection.execute( |
| 323 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 324 | ).fetchall() |
| 325 | |
| 326 | assert [row[0] for row in rows] == ["001-init", "002-add-column"] |
| 327 | |
| 328 | |
| 329 | def test_drop_reverts_only_selected_migration(tmp_path): |
| 330 | repo_path = tmp_path / "migrations" |
| 331 | repo_path.mkdir() |
| 332 | _write_project_config(repo_path) |
| 333 | |
| 334 | (repo_path / "001-init.up.sql").write_text( |
| 335 | "CREATE TABLE a (id int);", |
| 336 | encoding="utf-8", |
| 337 | ) |
| 338 | (repo_path / "001-init.down.sql").write_text( |
| 339 | "DROP TABLE a;", |
| 340 | encoding="utf-8", |
| 341 | ) |
| 342 | |
| 343 | (repo_path / "002-next.up.sql").write_text( |
| 344 | "CREATE TABLE b (id int);", |
| 345 | encoding="utf-8", |
| 346 | ) |
| 347 | (repo_path / "002-next.down.sql").write_text( |
| 348 | "DROP TABLE b;", |
| 349 | encoding="utf-8", |
| 350 | ) |
| 351 | (repo_path / "002-next.config.toml").write_text( |
| 352 | 'dependencies = ["001-init"]\n', |
| 353 | encoding="utf-8", |
| 354 | ) |
| 355 | |
| 356 | db_path = tmp_path / "test.db" |
| 357 | database_url = f"sqlite:///{db_path}" |
| 358 | |
| 359 | up_result = runner.invoke( |
| 360 | app, |
| 361 | [ |
| 362 | "up", |
| 363 | "--project", |
| 364 | str(repo_path), |
| 365 | "--database", |
| 366 | database_url, |
| 367 | "--if-modified", |
| 368 | "ignore", |
| 369 | ], |
| 370 | ) |
| 371 | assert up_result.exit_code == 0 |
| 372 | |
| 373 | drop_result = runner.invoke( |
| 374 | app, |
| 375 | [ |
| 376 | "drop", |
| 377 | "002-next", |
| 378 | "--project", |
| 379 | str(repo_path), |
| 380 | "--database", |
| 381 | database_url, |
| 382 | ], |
| 383 | ) |
| 384 | assert drop_result.exit_code == 0 |
| 385 | |
| 386 | with sqlite3.connect(db_path) as connection: |
| 387 | rows = connection.execute( |
| 388 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 389 | ).fetchall() |
| 390 | table_a = connection.execute( |
| 391 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='a'", |
| 392 | ).fetchone() |
| 393 | table_b = connection.execute( |
| 394 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='b'", |
| 395 | ).fetchone() |
| 396 | |
| 397 | assert [row[0] for row in rows] == ["001-init"] |
| 398 | assert table_a is not None |
| 399 | assert table_b is None |
| 400 | |
| 401 | |
| 402 | def test_log_lists_applied_migrations_with_timestamp_and_status(tmp_path): |
| 403 | db_path = tmp_path / "test.db" |
| 404 | database_url = f"sqlite:///{db_path}" |
| 405 | |
| 406 | up_result = runner.invoke( |
| 407 | app, |
| 408 | [ |
| 409 | "up", |
| 410 | "--project", |
| 411 | "tests/migrations", |
| 412 | "--database", |
| 413 | database_url, |
| 414 | "--if-modified", |
| 415 | "ignore", |
| 416 | ], |
| 417 | ) |
| 418 | assert up_result.exit_code == 0 |
| 419 | |
| 420 | log_result = runner.invoke( |
| 421 | app, |
| 422 | [ |
| 423 | "log", |
| 424 | "--project", |
| 425 | "tests/migrations", |
| 426 | "--database", |
| 427 | database_url, |
| 428 | ], |
| 429 | ) |
| 430 | |
| 431 | assert log_result.exit_code == 0 |
| 432 | assert "MIGRATION_ID" in log_result.output |
| 433 | assert "APPLIED_AT" in log_result.output |
| 434 | assert "STATUS" in log_result.output |
| 435 | assert "001-init" in log_result.output |
| 436 | assert "002-add-column" in log_result.output |
| 437 | assert "applied" in log_result.output |
| 438 | |
| 439 | |
| 440 | def test_log_marks_modified_migrations(tmp_path): |
| 441 | repo_path = tmp_path / "migrations" |
| 442 | repo_path.mkdir() |
| 443 | _write_project_config(repo_path) |
| 444 | |
| 445 | (repo_path / "001-init.up.sql").write_text( |
| 446 | "CREATE TABLE some_table (id int primary key, name text not null);", |
| 447 | encoding="utf-8", |
| 448 | ) |
| 449 | |
| 450 | db_path = tmp_path / "test.db" |
| 451 | database_url = f"sqlite:///{db_path}" |
| 452 | |
| 453 | up_result = runner.invoke( |
| 454 | app, |
| 455 | [ |
| 456 | "up", |
| 457 | "--project", |
| 458 | str(repo_path), |
| 459 | "--database", |
| 460 | database_url, |
| 461 | "--if-modified", |
| 462 | "ignore", |
| 463 | ], |
| 464 | ) |
| 465 | assert up_result.exit_code == 0 |
| 466 | |
| 467 | (repo_path / "001-init.up.sql").write_text( |
| 468 | "CREATE TABLE some_table (id int primary key, name text not null);\nALTER TABLE some_table ADD COLUMN extra int;", |
| 469 | encoding="utf-8", |
| 470 | ) |
| 471 | |
| 472 | log_result = runner.invoke( |
| 473 | app, |
| 474 | [ |
| 475 | "log", |
| 476 | "--project", |
| 477 | str(repo_path), |
| 478 | "--database", |
| 479 | database_url, |
| 480 | ], |
| 481 | ) |
| 482 | |
| 483 | assert log_result.exit_code == 0 |
| 484 | assert "001-init" in log_result.output |
| 485 | assert "modified" in log_result.output |
| 486 | |
| 487 | |
| 488 | def test_stash_reconciles_database_and_stashes_local_changes(tmp_path): |
| 489 | repo_path = tmp_path / "repo" |
| 490 | repo_path.mkdir() |
| 491 | _write_project_config(repo_path) |
| 492 | _init_git_repo(repo_path) |
| 493 | |
| 494 | original_down = "DROP TABLE table_one;" |
| 495 | dirty_down = "DROP TABLE table_one; -- local edit" |
| 496 | |
| 497 | (repo_path / "001-init.up.sql").write_text( |
| 498 | "CREATE TABLE table_one (id int);", |
| 499 | encoding="utf-8", |
| 500 | ) |
| 501 | (repo_path / "001-init.down.sql").write_text( |
| 502 | original_down, |
| 503 | encoding="utf-8", |
| 504 | ) |
| 505 | _run_git(repo_path, "add", ".") |
| 506 | _run_git(repo_path, "commit", "-m", "add migration") |
| 507 | |
| 508 | db_path = tmp_path / "test.db" |
| 509 | database_url = f"sqlite:///{db_path}" |
| 510 | |
| 511 | up_result = runner.invoke( |
| 512 | app, |
| 513 | [ |
| 514 | "up", |
| 515 | "--project", |
| 516 | str(repo_path), |
| 517 | "--database", |
| 518 | database_url, |
| 519 | "--if-modified", |
| 520 | "ignore", |
| 521 | ], |
| 522 | ) |
| 523 | assert up_result.exit_code == 0 |
| 524 | |
| 525 | (repo_path / "001-init.down.sql").write_text(dirty_down, encoding="utf-8") |
| 526 | |
| 527 | stash_result = runner.invoke( |
| 528 | app, |
| 529 | [ |
| 530 | "stash", |
| 531 | "--project", |
| 532 | str(repo_path), |
| 533 | "--database", |
| 534 | database_url, |
| 535 | ], |
| 536 | ) |
| 537 | assert stash_result.exit_code == 0 |
| 538 | |
| 539 | with sqlite3.connect(db_path) as connection: |
| 540 | rows = connection.execute("SELECT id FROM mitch_migrations").fetchall() |
| 541 | |
| 542 | assert rows == [("001-init",)] |
| 543 | assert (repo_path / "001-init.down.sql").read_text(encoding="utf-8") == original_down |
| 544 | assert _run_git(repo_path, "status", "--porcelain") == "" |
| 545 | assert "stash before mitch stash" in _run_git(repo_path, "stash", "list") |
| 546 | |
| 547 | |
| 548 | def test_stash_allows_locally_deleted_unapplied_migration(tmp_path): |
| 549 | repo_path = tmp_path / "repo" |
| 550 | repo_path.mkdir() |
| 551 | _write_project_config(repo_path) |
| 552 | _init_git_repo(repo_path) |
| 553 | |
| 554 | (repo_path / "001-init.up.sql").write_text( |
| 555 | "CREATE TABLE table_one (id int);", |
| 556 | encoding="utf-8", |
| 557 | ) |
| 558 | _run_git(repo_path, "add", ".") |
| 559 | _run_git(repo_path, "commit", "-m", "add migration") |
| 560 | |
| 561 | db_path = tmp_path / "test.db" |
| 562 | database_url = f"sqlite:///{db_path}" |
| 563 | |
| 564 | (repo_path / "001-init.up.sql").unlink() |
| 565 | |
| 566 | stash_result = runner.invoke( |
| 567 | app, |
| 568 | [ |
| 569 | "stash", |
| 570 | "--project", |
| 571 | str(repo_path), |
| 572 | "--database", |
| 573 | database_url, |
| 574 | ], |
| 575 | ) |
| 576 | assert stash_result.exit_code == 0 |
| 577 | assert (repo_path / "001-init.up.sql").exists() |
| 578 | assert _run_git(repo_path, "status", "--porcelain") == "" |
| 579 | assert "stash before mitch stash" in _run_git(repo_path, "stash", "list") |
| 580 | |
| 581 | |
| 582 | def test_status_reports_r_m_a_and_is_informative_by_default(tmp_path): |
| 583 | repo_path = tmp_path / "repo" |
| 584 | repo_path.mkdir() |
| 585 | _write_project_config(repo_path) |
| 586 | _write_status_fixture_migrations(repo_path) |
| 587 | |
| 588 | db_path = tmp_path / "test.db" |
| 589 | database_url = f"sqlite:///{db_path}" |
| 590 | |
| 591 | up_result = runner.invoke( |
| 592 | app, |
| 593 | [ |
| 594 | "up", |
| 595 | "--project", |
| 596 | str(repo_path), |
| 597 | "--database", |
| 598 | database_url, |
| 599 | "--if-modified", |
| 600 | "ignore", |
| 601 | ], |
| 602 | ) |
| 603 | assert up_result.exit_code == 0 |
| 604 | |
| 605 | (repo_path / "001-init.up.sql").unlink() |
| 606 | (repo_path / "002-next.up.sql").write_text( |
| 607 | "CREATE TABLE table_two (id int, name text);", |
| 608 | encoding="utf-8", |
| 609 | ) |
| 610 | (repo_path / "003-pending.up.sql").write_text( |
| 611 | "CREATE TABLE table_three (id int);", |
| 612 | encoding="utf-8", |
| 613 | ) |
| 614 | |
| 615 | status_result = runner.invoke( |
| 616 | app, |
| 617 | [ |
| 618 | "status", |
| 619 | "--project", |
| 620 | str(repo_path), |
| 621 | "--database", |
| 622 | database_url, |
| 623 | ], |
| 624 | ) |
| 625 | |
| 626 | assert status_result.exit_code == 0 |
| 627 | assert "R 001-init" in status_result.output |
| 628 | assert "M 002-next" in status_result.output |
| 629 | assert "A 003-pending" in status_result.output |
| 630 | |
| 631 | |
| 632 | def test_status_filters_to_specified_migration_ids(tmp_path): |
| 633 | repo_path = tmp_path / "repo" |
| 634 | repo_path.mkdir() |
| 635 | _write_project_config(repo_path) |
| 636 | _write_status_fixture_migrations(repo_path) |
| 637 | |
| 638 | db_path = tmp_path / "test.db" |
| 639 | database_url = f"sqlite:///{db_path}" |
| 640 | |
| 641 | up_result = runner.invoke( |
| 642 | app, |
| 643 | [ |
| 644 | "up", |
| 645 | "--project", |
| 646 | str(repo_path), |
| 647 | "--database", |
| 648 | database_url, |
| 649 | "--if-modified", |
| 650 | "ignore", |
| 651 | ], |
| 652 | ) |
| 653 | assert up_result.exit_code == 0, "Setup failed: " + up_result.output |
| 654 | |
| 655 | (repo_path / "001-init.up.sql").unlink() |
| 656 | (repo_path / "002-next.up.sql").write_text( |
| 657 | "CREATE TABLE table_two (id int, name text);", |
| 658 | encoding="utf-8", |
| 659 | ) |
| 660 | (repo_path / "003-pending.up.sql").write_text( |
| 661 | "CREATE TABLE table_three (id int);", |
| 662 | encoding="utf-8", |
| 663 | ) |
| 664 | |
| 665 | status_result = runner.invoke( |
| 666 | app, |
| 667 | [ |
| 668 | "status", |
| 669 | "002-next", |
| 670 | "003-pending", |
| 671 | "--project", |
| 672 | str(repo_path), |
| 673 | "--database", |
| 674 | database_url, |
| 675 | ], |
| 676 | ) |
| 677 | |
| 678 | assert status_result.exit_code == 0, "Status command failed: " + status_result.output |
| 679 | assert "R 001-init" not in status_result.output |
| 680 | assert "M 002-next" in status_result.output |
| 681 | assert "A 003-pending" in status_result.output |
| 682 | |
| 683 | |
| 684 | def test_status_ci_exit_code_2_for_missing_migrations(tmp_path): |
| 685 | repo_path = tmp_path / "repo" |
| 686 | repo_path.mkdir() |
| 687 | _write_project_config(repo_path) |
| 688 | _write_status_fixture_migrations(repo_path) |
| 689 | |
| 690 | db_path = tmp_path / "test.db" |
| 691 | database_url = f"sqlite:///{db_path}" |
| 692 | |
| 693 | up_result = runner.invoke( |
| 694 | app, |
| 695 | [ |
| 696 | "up", |
| 697 | "--project", |
| 698 | str(repo_path), |
| 699 | "--database", |
| 700 | database_url, |
| 701 | "--if-modified", |
| 702 | "ignore", |
| 703 | ], |
| 704 | ) |
| 705 | assert up_result.exit_code == 0 |
| 706 | |
| 707 | (repo_path / "001-init.up.sql").unlink() |
| 708 | |
| 709 | status_result = runner.invoke( |
| 710 | app, |
| 711 | [ |
| 712 | "status", |
| 713 | "--ci", |
| 714 | "--project", |
| 715 | str(repo_path), |
| 716 | "--database", |
| 717 | database_url, |
| 718 | ], |
| 719 | ) |
| 720 | |
| 721 | assert status_result.exit_code == 2 |
| 722 | assert "R 001-init" in status_result.output |
| 723 | |
| 724 | |
| 725 | def test_status_ci_exit_code_3_for_modified_migrations_in_strict_mode(tmp_path): |
| 726 | repo_path = tmp_path / "repo" |
| 727 | repo_path.mkdir() |
| 728 | _write_project_config(repo_path) |
| 729 | _write_status_fixture_migrations(repo_path) |
| 730 | |
| 731 | db_path = tmp_path / "test.db" |
| 732 | database_url = f"sqlite:///{db_path}" |
| 733 | |
| 734 | up_result = runner.invoke( |
| 735 | app, |
| 736 | [ |
| 737 | "up", |
| 738 | "--project", |
| 739 | str(repo_path), |
| 740 | "--database", |
| 741 | database_url, |
| 742 | "--if-modified", |
| 743 | "ignore", |
| 744 | ], |
| 745 | ) |
| 746 | assert up_result.exit_code == 0 |
| 747 | |
| 748 | (repo_path / "002-next.up.sql").write_text( |
| 749 | "CREATE TABLE table_two (id int, name text);", |
| 750 | encoding="utf-8", |
| 751 | ) |
| 752 | |
| 753 | status_result = runner.invoke( |
| 754 | app, |
| 755 | [ |
| 756 | "status", |
| 757 | "--ci", |
| 758 | "--strict", |
| 759 | "--project", |
| 760 | str(repo_path), |
| 761 | "--database", |
| 762 | database_url, |
| 763 | ], |
| 764 | ) |
| 765 | |
| 766 | assert status_result.exit_code == 3 |
| 767 | assert "M 002-next" in status_result.output |
| 768 | |
| 769 | |
| 770 | def test_status_ci_exit_code_4_for_missing_and_modified_in_strict_mode(tmp_path): |
| 771 | repo_path = tmp_path / "repo" |
| 772 | repo_path.mkdir() |
| 773 | _write_project_config(repo_path) |
| 774 | _write_status_fixture_migrations(repo_path) |
| 775 | |
| 776 | db_path = tmp_path / "test.db" |
| 777 | database_url = f"sqlite:///{db_path}" |
| 778 | |
| 779 | up_result = runner.invoke( |
| 780 | app, |
| 781 | [ |
| 782 | "up", |
| 783 | "--project", |
| 784 | str(repo_path), |
| 785 | "--database", |
| 786 | database_url, |
| 787 | "--if-modified", |
| 788 | "ignore", |
| 789 | ], |
| 790 | ) |
| 791 | assert up_result.exit_code == 0 |
| 792 | |
| 793 | (repo_path / "001-init.up.sql").unlink() |
| 794 | (repo_path / "002-next.up.sql").write_text( |
| 795 | "CREATE TABLE table_two (id int, name text);", |
| 796 | encoding="utf-8", |
| 797 | ) |
| 798 | |
| 799 | status_result = runner.invoke( |
| 800 | app, |
| 801 | [ |
| 802 | "status", |
| 803 | "--ci", |
| 804 | "--strict", |
| 805 | "--project", |
| 806 | str(repo_path), |
| 807 | "--database", |
| 808 | database_url, |
| 809 | ], |
| 810 | ) |
| 811 | |
| 812 | assert status_result.exit_code == 4 |
| 813 | assert "R 001-init" in status_result.output |
| 814 | assert "M 002-next" in status_result.output |
| 815 | |
| 816 | |
| 817 | def test_status_uses_ci_environment_when_flag_not_provided(tmp_path, monkeypatch): |
| 818 | repo_path = tmp_path / "repo" |
| 819 | repo_path.mkdir() |
| 820 | _write_project_config(repo_path) |
| 821 | _write_status_fixture_migrations(repo_path) |
| 822 | |
| 823 | db_path = tmp_path / "test.db" |
| 824 | database_url = f"sqlite:///{db_path}" |
| 825 | |
| 826 | up_result = runner.invoke( |
| 827 | app, |
| 828 | [ |
| 829 | "up", |
| 830 | "--project", |
| 831 | str(repo_path), |
| 832 | "--database", |
| 833 | database_url, |
| 834 | "--if-modified", |
| 835 | "ignore", |
| 836 | ], |
| 837 | ) |
| 838 | assert up_result.exit_code == 0 |
| 839 | |
| 840 | (repo_path / "001-init.up.sql").unlink() |
| 841 | monkeypatch.setenv("CI", "1") |
| 842 | |
| 843 | status_result = runner.invoke( |
| 844 | app, |
| 845 | [ |
| 846 | "status", |
| 847 | "--project", |
| 848 | str(repo_path), |
| 849 | "--database", |
| 850 | database_url, |
| 851 | ], |
| 852 | ) |
| 853 | |
| 854 | assert status_result.exit_code == 2 |
| 855 | |
| 856 | |
| 857 | def test_status_no_ci_overrides_ci_environment(tmp_path, monkeypatch): |
| 858 | repo_path = tmp_path / "repo" |
| 859 | repo_path.mkdir() |
| 860 | _write_project_config(repo_path) |
| 861 | _write_status_fixture_migrations(repo_path) |
| 862 | |
| 863 | db_path = tmp_path / "test.db" |
| 864 | database_url = f"sqlite:///{db_path}" |
| 865 | |
| 866 | up_result = runner.invoke( |
| 867 | app, |
| 868 | [ |
| 869 | "up", |
| 870 | "--project", |
| 871 | str(repo_path), |
| 872 | "--database", |
| 873 | database_url, |
| 874 | "--if-modified", |
| 875 | "ignore", |
| 876 | ], |
| 877 | ) |
| 878 | assert up_result.exit_code == 0 |
| 879 | |
| 880 | (repo_path / "001-init.up.sql").unlink() |
| 881 | monkeypatch.setenv("CI", "1") |
| 882 | |
| 883 | status_result = runner.invoke( |
| 884 | app, |
| 885 | [ |
| 886 | "status", |
| 887 | "--no-ci", |
| 888 | "--project", |
| 889 | str(repo_path), |
| 890 | "--database", |
| 891 | database_url, |
| 892 | ], |
| 893 | ) |
| 894 | |
| 895 | assert status_result.exit_code == 0 |
| 896 | assert "R 001-init" in status_result.output |
| 897 | |
| 898 | |
| 899 | def test_checkout_reverts_migrations_missing_on_target_branch(tmp_path): |
| 900 | repo_path = tmp_path / "repo" |
| 901 | repo_path.mkdir() |
| 902 | _write_project_config(repo_path) |
| 903 | _init_git_repo(repo_path) |
| 904 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 905 | |
| 906 | (repo_path / "001-init.up.sql").write_text( |
| 907 | "CREATE TABLE table_one (id int);", |
| 908 | encoding="utf-8", |
| 909 | ) |
| 910 | (repo_path / "001-init.down.sql").write_text( |
| 911 | "DROP TABLE table_one;", |
| 912 | encoding="utf-8", |
| 913 | ) |
| 914 | _run_git(repo_path, "add", ".") |
| 915 | _run_git(repo_path, "commit", "-m", "add 001") |
| 916 | |
| 917 | db_path = tmp_path / "test.db" |
| 918 | database_url = f"sqlite:///{db_path}" |
| 919 | |
| 920 | up_result = runner.invoke( |
| 921 | app, |
| 922 | [ |
| 923 | "up", |
| 924 | "--project", |
| 925 | str(repo_path), |
| 926 | "--database", |
| 927 | database_url, |
| 928 | "--if-modified", |
| 929 | "ignore", |
| 930 | ], |
| 931 | ) |
| 932 | assert up_result.exit_code == 0 |
| 933 | |
| 934 | _run_git(repo_path, "checkout", "-b", "target") |
| 935 | _run_git(repo_path, "rm", "001-init.up.sql", "001-init.down.sql") |
| 936 | _run_git(repo_path, "commit", "-m", "remove migration") |
| 937 | _run_git(repo_path, "checkout", base_branch) |
| 938 | |
| 939 | checkout_result = runner.invoke( |
| 940 | app, |
| 941 | [ |
| 942 | "checkout", |
| 943 | "target", |
| 944 | "--project", |
| 945 | str(repo_path), |
| 946 | "--database", |
| 947 | database_url, |
| 948 | ], |
| 949 | ) |
| 950 | assert checkout_result.exit_code == 0 |
| 951 | |
| 952 | branch = _run_git(repo_path, "branch", "--show-current") |
| 953 | assert branch == "target" |
| 954 | |
| 955 | with sqlite3.connect(db_path) as connection: |
| 956 | rows = connection.execute("SELECT id FROM mitch_migrations").fetchall() |
| 957 | table = connection.execute( |
| 958 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", |
| 959 | ).fetchone() |
| 960 | |
| 961 | assert rows == [] |
| 962 | assert table is None |
| 963 | |
| 964 | |
| 965 | def test_checkout_works_when_project_is_subfolder_of_git_repo(tmp_path): |
| 966 | repo_path = tmp_path / "repo" |
| 967 | repo_path.mkdir() |
| 968 | _write_project_config(repo_path) |
| 969 | _init_git_repo(repo_path) |
| 970 | migrations_path = repo_path / "migrations" |
| 971 | migrations_path.mkdir() |
| 972 | _write_project_config(migrations_path) |
| 973 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 974 | |
| 975 | (migrations_path / "001-init.up.sql").write_text( |
| 976 | "CREATE TABLE table_one (id int);", |
| 977 | encoding="utf-8", |
| 978 | ) |
| 979 | (migrations_path / "001-init.down.sql").write_text( |
| 980 | "DROP TABLE table_one;", |
| 981 | encoding="utf-8", |
| 982 | ) |
| 983 | _run_git(repo_path, "add", ".") |
| 984 | _run_git(repo_path, "commit", "-m", "add migration") |
| 985 | |
| 986 | db_path = tmp_path / "test.db" |
| 987 | database_url = f"sqlite:///{db_path}" |
| 988 | |
| 989 | up_result = runner.invoke( |
| 990 | app, |
| 991 | [ |
| 992 | "up", |
| 993 | "--project", |
| 994 | str(migrations_path), |
| 995 | "--database", |
| 996 | database_url, |
| 997 | "--if-modified", |
| 998 | "ignore", |
| 999 | ], |
| 1000 | ) |
| 1001 | assert up_result.exit_code == 0 |
| 1002 | |
| 1003 | _run_git(repo_path, "checkout", "-b", "target") |
| 1004 | _run_git(repo_path, "rm", "migrations/001-init.up.sql", "migrations/001-init.down.sql") |
| 1005 | _run_git(repo_path, "commit", "-m", "remove migration") |
| 1006 | _run_git(repo_path, "checkout", base_branch) |
| 1007 | |
| 1008 | checkout_result = runner.invoke( |
| 1009 | app, |
| 1010 | [ |
| 1011 | "checkout", |
| 1012 | "target", |
| 1013 | "--project", |
| 1014 | str(migrations_path), |
| 1015 | "--database", |
| 1016 | database_url, |
| 1017 | ], |
| 1018 | ) |
| 1019 | assert checkout_result.exit_code == 0 |
| 1020 | |
| 1021 | branch = _run_git(repo_path, "branch", "--show-current") |
| 1022 | assert branch == "target" |
| 1023 | |
| 1024 | with sqlite3.connect(db_path) as connection: |
| 1025 | rows = connection.execute("SELECT id FROM mitch_migrations").fetchall() |
| 1026 | |
| 1027 | assert rows == [] |
| 1028 | |
| 1029 | |
| 1030 | def test_checkout_redeploys_changed_deployed_migrations(tmp_path): |
| 1031 | repo_path = tmp_path / "repo" |
| 1032 | repo_path.mkdir() |
| 1033 | _write_project_config(repo_path) |
| 1034 | _init_git_repo(repo_path) |
| 1035 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1036 | |
| 1037 | (repo_path / "001-init.up.sql").write_text( |
| 1038 | "CREATE TABLE table_one (id int);", |
| 1039 | encoding="utf-8", |
| 1040 | ) |
| 1041 | (repo_path / "001-init.down.sql").write_text( |
| 1042 | "DROP TABLE table_one;", |
| 1043 | encoding="utf-8", |
| 1044 | ) |
| 1045 | _run_git(repo_path, "add", ".") |
| 1046 | _run_git(repo_path, "commit", "-m", "add 001") |
| 1047 | |
| 1048 | db_path = tmp_path / "test.db" |
| 1049 | database_url = f"sqlite:///{db_path}" |
| 1050 | |
| 1051 | up_result = runner.invoke( |
| 1052 | app, |
| 1053 | [ |
| 1054 | "up", |
| 1055 | "--project", |
| 1056 | str(repo_path), |
| 1057 | "--database", |
| 1058 | database_url, |
| 1059 | "--if-modified", |
| 1060 | "ignore", |
| 1061 | ], |
| 1062 | ) |
| 1063 | assert up_result.exit_code == 0 |
| 1064 | |
| 1065 | with sqlite3.connect(db_path) as connection: |
| 1066 | initial_applied_at = connection.execute( |
| 1067 | "SELECT applied_at FROM mitch_migrations WHERE id = '001-init'", |
| 1068 | ).fetchone()[0] |
| 1069 | |
| 1070 | _run_git(repo_path, "checkout", "-b", "target") |
| 1071 | (repo_path / "001-init.up.sql").write_text( |
| 1072 | "CREATE TABLE table_one (id int, name text);", |
| 1073 | encoding="utf-8", |
| 1074 | ) |
| 1075 | _run_git(repo_path, "add", ".") |
| 1076 | _run_git(repo_path, "commit", "-m", "change migration") |
| 1077 | _run_git(repo_path, "checkout", base_branch) |
| 1078 | |
| 1079 | checkout_result = runner.invoke( |
| 1080 | app, |
| 1081 | [ |
| 1082 | "checkout", |
| 1083 | "target", |
| 1084 | "--project", |
| 1085 | str(repo_path), |
| 1086 | "--database", |
| 1087 | database_url, |
| 1088 | ], |
| 1089 | ) |
| 1090 | assert checkout_result.exit_code == 0 |
| 1091 | |
| 1092 | branch = _run_git(repo_path, "branch", "--show-current") |
| 1093 | assert branch == "target" |
| 1094 | |
| 1095 | with sqlite3.connect(db_path) as connection: |
| 1096 | applied_at = connection.execute( |
| 1097 | "SELECT applied_at FROM mitch_migrations WHERE id = '001-init'", |
| 1098 | ).fetchone()[0] |
| 1099 | columns = connection.execute( |
| 1100 | "PRAGMA table_info(table_one)", |
| 1101 | ).fetchall() |
| 1102 | |
| 1103 | assert applied_at != initial_applied_at |
| 1104 | assert any(column[1] == "name" for column in columns) |
| 1105 | |
| 1106 | |
| 1107 | def test_checkout_redeploys_renamed_deployed_migrations(tmp_path): |
| 1108 | repo_path = tmp_path / "repo" |
| 1109 | repo_path.mkdir() |
| 1110 | _write_project_config(repo_path) |
| 1111 | _init_git_repo(repo_path) |
| 1112 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1113 | |
| 1114 | (repo_path / "001-init.up.sql").write_text( |
| 1115 | "CREATE TABLE table_one (id int);", |
| 1116 | encoding="utf-8", |
| 1117 | ) |
| 1118 | (repo_path / "001-init.down.sql").write_text( |
| 1119 | "DROP TABLE table_one;", |
| 1120 | encoding="utf-8", |
| 1121 | ) |
| 1122 | _run_git(repo_path, "add", ".") |
| 1123 | _run_git(repo_path, "commit", "-m", "add migration") |
| 1124 | |
| 1125 | db_path = tmp_path / "test.db" |
| 1126 | database_url = f"sqlite:///{db_path}" |
| 1127 | |
| 1128 | up_result = runner.invoke( |
| 1129 | app, |
| 1130 | [ |
| 1131 | "up", |
| 1132 | "--project", |
| 1133 | str(repo_path), |
| 1134 | "--database", |
| 1135 | database_url, |
| 1136 | "--if-modified", |
| 1137 | "ignore", |
| 1138 | ], |
| 1139 | ) |
| 1140 | assert up_result.exit_code == 0 |
| 1141 | |
| 1142 | _run_git(repo_path, "checkout", "-b", "target") |
| 1143 | _run_git(repo_path, "mv", "001-init.up.sql", "001-renamed.up.sql") |
| 1144 | _run_git(repo_path, "mv", "001-init.down.sql", "001-renamed.down.sql") |
| 1145 | _run_git(repo_path, "commit", "-m", "rename migration") |
| 1146 | _run_git(repo_path, "checkout", base_branch) |
| 1147 | |
| 1148 | checkout_result = runner.invoke( |
| 1149 | app, |
| 1150 | [ |
| 1151 | "checkout", |
| 1152 | "target", |
| 1153 | "--project", |
| 1154 | str(repo_path), |
| 1155 | "--database", |
| 1156 | database_url, |
| 1157 | ], |
| 1158 | ) |
| 1159 | assert checkout_result.exit_code == 0 |
| 1160 | |
| 1161 | with sqlite3.connect(db_path) as connection: |
| 1162 | rows = connection.execute( |
| 1163 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1164 | ).fetchall() |
| 1165 | |
| 1166 | assert [row[0] for row in rows] == ["001-renamed"] |
| 1167 | |
| 1168 | |
| 1169 | def test_checkout_refuses_dirty_worktree_before_db_reverts(tmp_path): |
| 1170 | repo_path = tmp_path / "repo" |
| 1171 | repo_path.mkdir() |
| 1172 | _write_project_config(repo_path) |
| 1173 | _init_git_repo(repo_path) |
| 1174 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1175 | |
| 1176 | (repo_path / "README.md").write_text("base\n", encoding="utf-8") |
| 1177 | (repo_path / "001-init.up.sql").write_text( |
| 1178 | "CREATE TABLE table_one (id int);", |
| 1179 | encoding="utf-8", |
| 1180 | ) |
| 1181 | (repo_path / "001-init.down.sql").write_text( |
| 1182 | "DROP TABLE table_one;", |
| 1183 | encoding="utf-8", |
| 1184 | ) |
| 1185 | _run_git(repo_path, "add", ".") |
| 1186 | _run_git(repo_path, "commit", "-m", "base") |
| 1187 | |
| 1188 | db_path = tmp_path / "test.db" |
| 1189 | database_url = f"sqlite:///{db_path}" |
| 1190 | |
| 1191 | up_result = runner.invoke( |
| 1192 | app, |
| 1193 | [ |
| 1194 | "up", |
| 1195 | "--project", |
| 1196 | str(repo_path), |
| 1197 | "--database", |
| 1198 | database_url, |
| 1199 | "--if-modified", |
| 1200 | "ignore", |
| 1201 | ], |
| 1202 | ) |
| 1203 | assert up_result.exit_code == 0 |
| 1204 | |
| 1205 | _run_git(repo_path, "checkout", "-b", "target") |
| 1206 | _run_git(repo_path, "rm", "001-init.up.sql", "001-init.down.sql") |
| 1207 | _run_git(repo_path, "commit", "-m", "remove migration") |
| 1208 | _run_git(repo_path, "checkout", base_branch) |
| 1209 | |
| 1210 | # Dirty tracked file to force checkout refusal before any DB operations. |
| 1211 | (repo_path / "README.md").write_text("dirty\n", encoding="utf-8") |
| 1212 | |
| 1213 | checkout_result = runner.invoke( |
| 1214 | app, |
| 1215 | [ |
| 1216 | "checkout", |
| 1217 | "target", |
| 1218 | "--project", |
| 1219 | str(repo_path), |
| 1220 | "--database", |
| 1221 | database_url, |
| 1222 | ], |
| 1223 | ) |
| 1224 | assert checkout_result.exit_code == 1 |
| 1225 | assert "Working tree is not clean" in checkout_result.output |
| 1226 | |
| 1227 | branch = _run_git(repo_path, "branch", "--show-current") |
| 1228 | assert branch == base_branch |
| 1229 | |
| 1230 | with sqlite3.connect(db_path) as connection: |
| 1231 | rows = connection.execute( |
| 1232 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1233 | ).fetchall() |
| 1234 | table = connection.execute( |
| 1235 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", |
| 1236 | ).fetchone() |
| 1237 | |
| 1238 | assert [row[0] for row in rows] == ["001-init"] |
| 1239 | assert table is not None |
| 1240 | |
| 1241 | |
| 1242 | def test_checkout_allow_dirty_reverts_using_local_migration_files(tmp_path): |
| 1243 | repo_path = tmp_path / "repo" |
| 1244 | repo_path.mkdir() |
| 1245 | _write_project_config(repo_path) |
| 1246 | _init_git_repo(repo_path) |
| 1247 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1248 | |
| 1249 | (repo_path / "001-init.up.sql").write_text( |
| 1250 | "CREATE TABLE table_one (id int);", |
| 1251 | encoding="utf-8", |
| 1252 | ) |
| 1253 | (repo_path / "001-init.down.sql").write_text( |
| 1254 | "DROP TABLE table_one;", |
| 1255 | encoding="utf-8", |
| 1256 | ) |
| 1257 | _run_git(repo_path, "add", ".") |
| 1258 | _run_git(repo_path, "commit", "-m", "base migration") |
| 1259 | |
| 1260 | db_path = tmp_path / "test.db" |
| 1261 | database_url = f"sqlite:///{db_path}" |
| 1262 | |
| 1263 | up_result = runner.invoke( |
| 1264 | app, |
| 1265 | [ |
| 1266 | "up", |
| 1267 | "--project", |
| 1268 | str(repo_path), |
| 1269 | "--database", |
| 1270 | database_url, |
| 1271 | "--if-modified", |
| 1272 | "ignore", |
| 1273 | ], |
| 1274 | ) |
| 1275 | assert up_result.exit_code == 0 |
| 1276 | |
| 1277 | _run_git(repo_path, "checkout", "-b", "target") |
| 1278 | (repo_path / "001-init.up.sql").write_text( |
| 1279 | "CREATE TABLE table_one (id int, name text);", |
| 1280 | encoding="utf-8", |
| 1281 | ) |
| 1282 | _run_git(repo_path, "add", ".") |
| 1283 | _run_git(repo_path, "commit", "-m", "changed migration on target") |
| 1284 | _run_git(repo_path, "checkout", base_branch) |
| 1285 | |
| 1286 | # Dirty local down-script change should be used for the pre-checkout revert. |
| 1287 | (repo_path / "001-init.down.sql").write_text( |
| 1288 | "DROP TABLE table_one;\nCREATE TABLE dirty_marker (id int);", |
| 1289 | encoding="utf-8", |
| 1290 | ) |
| 1291 | |
| 1292 | checkout_result = runner.invoke( |
| 1293 | app, |
| 1294 | [ |
| 1295 | "checkout", |
| 1296 | "target", |
| 1297 | "--project", |
| 1298 | str(repo_path), |
| 1299 | "--database", |
| 1300 | database_url, |
| 1301 | "--allow-dirty", |
| 1302 | ], |
| 1303 | ) |
| 1304 | assert checkout_result.exit_code == 0 |
| 1305 | |
| 1306 | branch = _run_git(repo_path, "branch", "--show-current") |
| 1307 | assert branch == "target" |
| 1308 | |
| 1309 | # Stash pop restored local dirty change on top of target branch. |
| 1310 | down_script = (repo_path / "001-init.down.sql").read_text(encoding="utf-8") |
| 1311 | assert "dirty_marker" in down_script |
| 1312 | |
| 1313 | with sqlite3.connect(db_path) as connection: |
| 1314 | applied_ids = connection.execute( |
| 1315 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1316 | ).fetchall() |
| 1317 | table_columns = connection.execute( |
| 1318 | "PRAGMA table_info(table_one)", |
| 1319 | ).fetchall() |
| 1320 | marker_table = connection.execute( |
| 1321 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='dirty_marker'", |
| 1322 | ).fetchone() |
| 1323 | |
| 1324 | assert [row[0] for row in applied_ids] == ["001-init"] |
| 1325 | assert any(column[1] == "name" for column in table_columns) |
| 1326 | assert marker_table is not None |
| 1327 | |
| 1328 | |
| 1329 | def test_checkout_allow_dirty_reverts_dirty_migration_without_branch_migration_changes(tmp_path): |
| 1330 | repo_path = tmp_path / "repo" |
| 1331 | repo_path.mkdir() |
| 1332 | _write_project_config(repo_path) |
| 1333 | _init_git_repo(repo_path) |
| 1334 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1335 | |
| 1336 | (repo_path / "README.md").write_text("base\n", encoding="utf-8") |
| 1337 | (repo_path / "001-init.up.sql").write_text( |
| 1338 | "CREATE TABLE table_one (id int);", |
| 1339 | encoding="utf-8", |
| 1340 | ) |
| 1341 | (repo_path / "001-init.down.sql").write_text( |
| 1342 | "DROP TABLE table_one;", |
| 1343 | encoding="utf-8", |
| 1344 | ) |
| 1345 | _run_git(repo_path, "add", ".") |
| 1346 | _run_git(repo_path, "commit", "-m", "base migration") |
| 1347 | |
| 1348 | db_path = tmp_path / "test.db" |
| 1349 | database_url = f"sqlite:///{db_path}" |
| 1350 | |
| 1351 | up_result = runner.invoke( |
| 1352 | app, |
| 1353 | [ |
| 1354 | "up", |
| 1355 | "--project", |
| 1356 | str(repo_path), |
| 1357 | "--database", |
| 1358 | database_url, |
| 1359 | "--if-modified", |
| 1360 | "ignore", |
| 1361 | ], |
| 1362 | ) |
| 1363 | assert up_result.exit_code == 0 |
| 1364 | |
| 1365 | _run_git(repo_path, "checkout", "-b", "target") |
| 1366 | (repo_path / "README.md").write_text("target\n", encoding="utf-8") |
| 1367 | _run_git(repo_path, "add", "README.md") |
| 1368 | _run_git(repo_path, "commit", "-m", "non-migration target change") |
| 1369 | _run_git(repo_path, "checkout", base_branch) |
| 1370 | |
| 1371 | # Dirty down-script change should still trigger a pre-checkout revert. |
| 1372 | (repo_path / "001-init.down.sql").write_text( |
| 1373 | "DROP TABLE table_one;\nCREATE TABLE dirty_marker (id int);", |
| 1374 | encoding="utf-8", |
| 1375 | ) |
| 1376 | |
| 1377 | checkout_result = runner.invoke( |
| 1378 | app, |
| 1379 | [ |
| 1380 | "checkout", |
| 1381 | "target", |
| 1382 | "--project", |
| 1383 | str(repo_path), |
| 1384 | "--database", |
| 1385 | database_url, |
| 1386 | "--allow-dirty", |
| 1387 | ], |
| 1388 | ) |
| 1389 | assert checkout_result.exit_code == 0 |
| 1390 | |
| 1391 | branch = _run_git(repo_path, "branch", "--show-current") |
| 1392 | assert branch == "target" |
| 1393 | |
| 1394 | down_script = (repo_path / "001-init.down.sql").read_text(encoding="utf-8") |
| 1395 | assert "dirty_marker" in down_script |
| 1396 | |
| 1397 | with sqlite3.connect(db_path) as connection: |
| 1398 | applied_ids = connection.execute( |
| 1399 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1400 | ).fetchall() |
| 1401 | table_exists = connection.execute( |
| 1402 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", |
| 1403 | ).fetchone() |
| 1404 | marker_table = connection.execute( |
| 1405 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='dirty_marker'", |
| 1406 | ).fetchone() |
| 1407 | |
| 1408 | assert [row[0] for row in applied_ids] == ["001-init"] |
| 1409 | assert table_exists is not None |
| 1410 | assert marker_table is not None |
| 1411 | |
| 1412 | |
| 1413 | def test_checkout_allow_dirty_redeploys_migration_locally_edited_to_match_target(tmp_path): |
| 1414 | # The current branch has v2 of a migration applied, the target branch has v1, and the file is |
| 1415 | # edited locally back to v1. A diff of the target against the working tree shows no migration |
| 1416 | # change, so a working-tree-based plan would skip the redeploy and leave the DB at v2. |
| 1417 | repo_path = tmp_path / "repo" |
| 1418 | repo_path.mkdir() |
| 1419 | _write_project_config(repo_path) |
| 1420 | _init_git_repo(repo_path) |
| 1421 | |
| 1422 | v1 = "CREATE TABLE table_one (id int);" |
| 1423 | v2 = "CREATE TABLE table_one (id int, name text);" |
| 1424 | |
| 1425 | (repo_path / "001-init.up.sql").write_text(v1, encoding="utf-8") |
| 1426 | (repo_path / "001-init.down.sql").write_text("DROP TABLE table_one;", encoding="utf-8") |
| 1427 | _run_git(repo_path, "add", ".") |
| 1428 | _run_git(repo_path, "commit", "-m", "add 001 v1") |
| 1429 | _run_git(repo_path, "branch", "target") |
| 1430 | |
| 1431 | (repo_path / "001-init.up.sql").write_text(v2, encoding="utf-8") |
| 1432 | _run_git(repo_path, "add", ".") |
| 1433 | _run_git(repo_path, "commit", "-m", "change 001 to v2") |
| 1434 | |
| 1435 | db_path = tmp_path / "test.db" |
| 1436 | database_url = f"sqlite:///{db_path}" |
| 1437 | |
| 1438 | up_result = runner.invoke( |
| 1439 | app, |
| 1440 | ["up", "--project", str(repo_path), "--database", database_url, "--if-modified", "ignore"], |
| 1441 | ) |
| 1442 | assert up_result.exit_code == 0 |
| 1443 | |
| 1444 | # Edit the migration locally back to what the target branch has. |
| 1445 | (repo_path / "001-init.up.sql").write_text(v1, encoding="utf-8") |
| 1446 | assert _run_git(repo_path, "diff", "--name-only", "target", "--", "001-init.up.sql") == "" |
| 1447 | |
| 1448 | checkout_result = runner.invoke( |
| 1449 | app, |
| 1450 | [ |
| 1451 | "checkout", |
| 1452 | "target", |
| 1453 | "--project", |
| 1454 | str(repo_path), |
| 1455 | "--database", |
| 1456 | database_url, |
| 1457 | "--allow-dirty", |
| 1458 | ], |
| 1459 | ) |
| 1460 | assert checkout_result.exit_code == 0, checkout_result.output |
| 1461 | |
| 1462 | assert _run_git(repo_path, "branch", "--show-current") == "target" |
| 1463 | assert (repo_path / "001-init.up.sql").read_text(encoding="utf-8") == v1 |
| 1464 | |
| 1465 | with sqlite3.connect(db_path) as connection: |
| 1466 | applied_ids = connection.execute("SELECT id FROM mitch_migrations").fetchall() |
| 1467 | columns = connection.execute("PRAGMA table_info(table_one)").fetchall() |
| 1468 | |
| 1469 | assert [row[0] for row in applied_ids] == ["001-init"] |
| 1470 | assert [column[1] for column in columns] == ["id"] |
| 1471 | |
| 1472 | |
| 1473 | def test_checkout_allow_dirty_restores_locally_added_applied_migration_without_and_up(tmp_path): |
| 1474 | repo_path = tmp_path / "repo" |
| 1475 | repo_path.mkdir() |
| 1476 | _write_project_config(repo_path) |
| 1477 | _init_git_repo(repo_path) |
| 1478 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1479 | |
| 1480 | (repo_path / "README.md").write_text("base\n", encoding="utf-8") |
| 1481 | _run_git(repo_path, "add", ".") |
| 1482 | _run_git(repo_path, "commit", "-m", "base commit") |
| 1483 | |
| 1484 | db_path = tmp_path / "test.db" |
| 1485 | database_url = f"sqlite:///{db_path}" |
| 1486 | |
| 1487 | # Local-only migration (uncommitted) that is already applied. |
| 1488 | (repo_path / "001-local.up.sql").write_text( |
| 1489 | "CREATE TABLE local_table (id int);", |
| 1490 | encoding="utf-8", |
| 1491 | ) |
| 1492 | (repo_path / "001-local.down.sql").write_text( |
| 1493 | "DROP TABLE local_table;", |
| 1494 | encoding="utf-8", |
| 1495 | ) |
| 1496 | up_result = runner.invoke( |
| 1497 | app, |
| 1498 | [ |
| 1499 | "up", |
| 1500 | "--project", |
| 1501 | str(repo_path), |
| 1502 | "--database", |
| 1503 | database_url, |
| 1504 | "--if-modified", |
| 1505 | "ignore", |
| 1506 | ], |
| 1507 | ) |
| 1508 | assert up_result.exit_code == 0 |
| 1509 | |
| 1510 | _run_git(repo_path, "checkout", "-b", "target") |
| 1511 | (repo_path / "README.md").write_text("target\n", encoding="utf-8") |
| 1512 | _run_git(repo_path, "add", "README.md") |
| 1513 | _run_git(repo_path, "commit", "-m", "non-migration target change") |
| 1514 | _run_git(repo_path, "checkout", base_branch) |
| 1515 | |
| 1516 | checkout_result = runner.invoke( |
| 1517 | app, |
| 1518 | [ |
| 1519 | "checkout", |
| 1520 | "target", |
| 1521 | "--project", |
| 1522 | str(repo_path), |
| 1523 | "--database", |
| 1524 | database_url, |
| 1525 | "--allow-dirty", |
| 1526 | ], |
| 1527 | ) |
| 1528 | assert checkout_result.exit_code == 0 |
| 1529 | |
| 1530 | with sqlite3.connect(db_path) as connection: |
| 1531 | applied_ids = connection.execute( |
| 1532 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1533 | ).fetchall() |
| 1534 | local_table = connection.execute( |
| 1535 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='local_table'", |
| 1536 | ).fetchone() |
| 1537 | |
| 1538 | assert [row[0] for row in applied_ids] == ["001-local"] |
| 1539 | assert local_table is not None |
| 1540 | assert (repo_path / "001-local.up.sql").exists() |
| 1541 | |
| 1542 | |
| 1543 | def test_checkout_allow_dirty_does_not_apply_locally_added_pending_migration_without_and_up(tmp_path): |
| 1544 | repo_path = tmp_path / "repo" |
| 1545 | repo_path.mkdir() |
| 1546 | _write_project_config(repo_path) |
| 1547 | _init_git_repo(repo_path) |
| 1548 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1549 | |
| 1550 | (repo_path / "README.md").write_text("base\n", encoding="utf-8") |
| 1551 | _run_git(repo_path, "add", ".") |
| 1552 | _run_git(repo_path, "commit", "-m", "base commit") |
| 1553 | |
| 1554 | db_path = tmp_path / "test.db" |
| 1555 | database_url = f"sqlite:///{db_path}" |
| 1556 | |
| 1557 | _run_git(repo_path, "checkout", "-b", "target") |
| 1558 | (repo_path / "README.md").write_text("target\n", encoding="utf-8") |
| 1559 | _run_git(repo_path, "add", "README.md") |
| 1560 | _run_git(repo_path, "commit", "-m", "non-migration target change") |
| 1561 | _run_git(repo_path, "checkout", base_branch) |
| 1562 | |
| 1563 | # Local-only migration (uncommitted) that was never applied. |
| 1564 | (repo_path / "001-local.up.sql").write_text( |
| 1565 | "CREATE TABLE local_table (id int);", |
| 1566 | encoding="utf-8", |
| 1567 | ) |
| 1568 | (repo_path / "001-local.down.sql").write_text( |
| 1569 | "DROP TABLE local_table;", |
| 1570 | encoding="utf-8", |
| 1571 | ) |
| 1572 | |
| 1573 | checkout_result = runner.invoke( |
| 1574 | app, |
| 1575 | [ |
| 1576 | "checkout", |
| 1577 | "target", |
| 1578 | "--project", |
| 1579 | str(repo_path), |
| 1580 | "--database", |
| 1581 | database_url, |
| 1582 | "--allow-dirty", |
| 1583 | ], |
| 1584 | ) |
| 1585 | assert checkout_result.exit_code == 0 |
| 1586 | |
| 1587 | with sqlite3.connect(db_path) as connection: |
| 1588 | applied_ids = connection.execute( |
| 1589 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1590 | ).fetchall() |
| 1591 | local_table = connection.execute( |
| 1592 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='local_table'", |
| 1593 | ).fetchone() |
| 1594 | |
| 1595 | assert applied_ids == [] |
| 1596 | assert local_table is None |
| 1597 | assert (repo_path / "001-local.up.sql").exists() |
| 1598 | |
| 1599 | |
| 1600 | def test_checkout_does_not_deploy_migrations_added_on_target_branch_without_and_up(tmp_path): |
| 1601 | repo_path = tmp_path / "repo" |
| 1602 | repo_path.mkdir() |
| 1603 | _write_project_config(repo_path) |
| 1604 | _init_git_repo(repo_path) |
| 1605 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1606 | |
| 1607 | (repo_path / "001-init.up.sql").write_text( |
| 1608 | "CREATE TABLE table_one (id int);", |
| 1609 | encoding="utf-8", |
| 1610 | ) |
| 1611 | (repo_path / "001-init.down.sql").write_text( |
| 1612 | "DROP TABLE table_one;", |
| 1613 | encoding="utf-8", |
| 1614 | ) |
| 1615 | _run_git(repo_path, "add", ".") |
| 1616 | _run_git(repo_path, "commit", "-m", "base migration") |
| 1617 | |
| 1618 | db_path = tmp_path / "test.db" |
| 1619 | database_url = f"sqlite:///{db_path}" |
| 1620 | |
| 1621 | up_result = runner.invoke( |
| 1622 | app, |
| 1623 | [ |
| 1624 | "up", |
| 1625 | "--project", |
| 1626 | str(repo_path), |
| 1627 | "--database", |
| 1628 | database_url, |
| 1629 | "--if-modified", |
| 1630 | "ignore", |
| 1631 | ], |
| 1632 | ) |
| 1633 | assert up_result.exit_code == 0 |
| 1634 | |
| 1635 | _run_git(repo_path, "checkout", "-b", "target") |
| 1636 | (repo_path / "002-add.up.sql").write_text( |
| 1637 | "CREATE TABLE table_two (id int);", |
| 1638 | encoding="utf-8", |
| 1639 | ) |
| 1640 | (repo_path / "002-add.down.sql").write_text( |
| 1641 | "DROP TABLE table_two;", |
| 1642 | encoding="utf-8", |
| 1643 | ) |
| 1644 | _run_git(repo_path, "add", ".") |
| 1645 | _run_git(repo_path, "commit", "-m", "add migration") |
| 1646 | _run_git(repo_path, "checkout", base_branch) |
| 1647 | |
| 1648 | checkout_result = runner.invoke( |
| 1649 | app, |
| 1650 | [ |
| 1651 | "checkout", |
| 1652 | "target", |
| 1653 | "--project", |
| 1654 | str(repo_path), |
| 1655 | "--database", |
| 1656 | database_url, |
| 1657 | ], |
| 1658 | ) |
| 1659 | assert checkout_result.exit_code == 0 |
| 1660 | |
| 1661 | with sqlite3.connect(db_path) as connection: |
| 1662 | applied_ids = connection.execute( |
| 1663 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1664 | ).fetchall() |
| 1665 | second_table = connection.execute( |
| 1666 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_two'", |
| 1667 | ).fetchone() |
| 1668 | |
| 1669 | assert [row[0] for row in applied_ids] == ["001-init"] |
| 1670 | assert second_table is None |
| 1671 | |
| 1672 | |
| 1673 | def test_checkout_deploys_migrations_added_on_target_branch_with_and_up(tmp_path): |
| 1674 | repo_path = tmp_path / "repo" |
| 1675 | repo_path.mkdir() |
| 1676 | _write_project_config(repo_path) |
| 1677 | _init_git_repo(repo_path) |
| 1678 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1679 | |
| 1680 | (repo_path / "001-init.up.sql").write_text( |
| 1681 | "CREATE TABLE table_one (id int);", |
| 1682 | encoding="utf-8", |
| 1683 | ) |
| 1684 | (repo_path / "001-init.down.sql").write_text( |
| 1685 | "DROP TABLE table_one;", |
| 1686 | encoding="utf-8", |
| 1687 | ) |
| 1688 | _run_git(repo_path, "add", ".") |
| 1689 | _run_git(repo_path, "commit", "-m", "base migration") |
| 1690 | |
| 1691 | db_path = tmp_path / "test.db" |
| 1692 | database_url = f"sqlite:///{db_path}" |
| 1693 | |
| 1694 | up_result = runner.invoke( |
| 1695 | app, |
| 1696 | [ |
| 1697 | "up", |
| 1698 | "--project", |
| 1699 | str(repo_path), |
| 1700 | "--database", |
| 1701 | database_url, |
| 1702 | "--if-modified", |
| 1703 | "ignore", |
| 1704 | ], |
| 1705 | ) |
| 1706 | assert up_result.exit_code == 0 |
| 1707 | |
| 1708 | _run_git(repo_path, "checkout", "-b", "target") |
| 1709 | (repo_path / "002-add.up.sql").write_text( |
| 1710 | "CREATE TABLE table_two (id int);", |
| 1711 | encoding="utf-8", |
| 1712 | ) |
| 1713 | (repo_path / "002-add.down.sql").write_text( |
| 1714 | "DROP TABLE table_two;", |
| 1715 | encoding="utf-8", |
| 1716 | ) |
| 1717 | _run_git(repo_path, "add", ".") |
| 1718 | _run_git(repo_path, "commit", "-m", "add migration") |
| 1719 | _run_git(repo_path, "checkout", base_branch) |
| 1720 | |
| 1721 | checkout_result = runner.invoke( |
| 1722 | app, |
| 1723 | [ |
| 1724 | "checkout", |
| 1725 | "target", |
| 1726 | "--project", |
| 1727 | str(repo_path), |
| 1728 | "--database", |
| 1729 | database_url, |
| 1730 | "--and-up", |
| 1731 | ], |
| 1732 | ) |
| 1733 | assert checkout_result.exit_code == 0 |
| 1734 | |
| 1735 | with sqlite3.connect(db_path) as connection: |
| 1736 | applied_ids = connection.execute( |
| 1737 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1738 | ).fetchall() |
| 1739 | second_table = connection.execute( |
| 1740 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_two'", |
| 1741 | ).fetchone() |
| 1742 | |
| 1743 | assert [row[0] for row in applied_ids] == ["001-init", "002-add"] |
| 1744 | assert second_table is not None |
| 1745 | |
| 1746 | |
| 1747 | def test_checkout_redeploys_dependants_of_changed_migration(tmp_path): |
| 1748 | repo_path = tmp_path / "repo" |
| 1749 | repo_path.mkdir() |
| 1750 | _write_project_config(repo_path) |
| 1751 | _init_git_repo(repo_path) |
| 1752 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1753 | |
| 1754 | (repo_path / "001-init.up.sql").write_text( |
| 1755 | "CREATE TABLE table_one (id int);", |
| 1756 | encoding="utf-8", |
| 1757 | ) |
| 1758 | (repo_path / "001-init.down.sql").write_text( |
| 1759 | "DROP TABLE table_one;", |
| 1760 | encoding="utf-8", |
| 1761 | ) |
| 1762 | (repo_path / "002-child.up.sql").write_text( |
| 1763 | "CREATE TABLE table_two (id int);", |
| 1764 | encoding="utf-8", |
| 1765 | ) |
| 1766 | (repo_path / "002-child.down.sql").write_text( |
| 1767 | "DROP TABLE table_two;", |
| 1768 | encoding="utf-8", |
| 1769 | ) |
| 1770 | (repo_path / "002-child.config.toml").write_text( |
| 1771 | 'dependencies = ["001-init"]', |
| 1772 | encoding="utf-8", |
| 1773 | ) |
| 1774 | _run_git(repo_path, "add", ".") |
| 1775 | _run_git(repo_path, "commit", "-m", "base chain") |
| 1776 | |
| 1777 | db_path = tmp_path / "test.db" |
| 1778 | database_url = f"sqlite:///{db_path}" |
| 1779 | |
| 1780 | up_result = runner.invoke( |
| 1781 | app, |
| 1782 | [ |
| 1783 | "up", |
| 1784 | "--project", |
| 1785 | str(repo_path), |
| 1786 | "--database", |
| 1787 | database_url, |
| 1788 | "--if-modified", |
| 1789 | "ignore", |
| 1790 | ], |
| 1791 | ) |
| 1792 | assert up_result.exit_code == 0 |
| 1793 | |
| 1794 | _run_git(repo_path, "checkout", "-b", "target") |
| 1795 | (repo_path / "001-init.up.sql").write_text( |
| 1796 | "CREATE TABLE table_one (id int, name text);", |
| 1797 | encoding="utf-8", |
| 1798 | ) |
| 1799 | _run_git(repo_path, "add", ".") |
| 1800 | _run_git(repo_path, "commit", "-m", "change parent migration") |
| 1801 | _run_git(repo_path, "checkout", base_branch) |
| 1802 | |
| 1803 | checkout_result = runner.invoke( |
| 1804 | app, |
| 1805 | [ |
| 1806 | "checkout", |
| 1807 | "target", |
| 1808 | "--project", |
| 1809 | str(repo_path), |
| 1810 | "--database", |
| 1811 | database_url, |
| 1812 | ], |
| 1813 | ) |
| 1814 | assert checkout_result.exit_code == 0 |
| 1815 | |
| 1816 | with sqlite3.connect(db_path) as connection: |
| 1817 | applied_ids = connection.execute( |
| 1818 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1819 | ).fetchall() |
| 1820 | parent_columns = connection.execute( |
| 1821 | "PRAGMA table_info(table_one)", |
| 1822 | ).fetchall() |
| 1823 | child_table = connection.execute( |
| 1824 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_two'", |
| 1825 | ).fetchone() |
| 1826 | |
| 1827 | assert [row[0] for row in applied_ids] == ["001-init", "002-child"] |
| 1828 | assert any(column[1] == "name" for column in parent_columns) |
| 1829 | assert child_table is not None |
| 1830 | |
| 1831 | |
| 1832 | def test_checkout_handles_migration_id_changed_in_config(tmp_path): |
| 1833 | repo_path = tmp_path / "repo" |
| 1834 | repo_path.mkdir() |
| 1835 | _write_project_config(repo_path) |
| 1836 | _init_git_repo(repo_path) |
| 1837 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1838 | |
| 1839 | (repo_path / "001-init.up.sql").write_text( |
| 1840 | "CREATE TABLE table_one (id int);", |
| 1841 | encoding="utf-8", |
| 1842 | ) |
| 1843 | (repo_path / "001-init.down.sql").write_text( |
| 1844 | "DROP TABLE table_one;", |
| 1845 | encoding="utf-8", |
| 1846 | ) |
| 1847 | (repo_path / "001-init.config.toml").write_text( |
| 1848 | 'id = "001-init"', |
| 1849 | encoding="utf-8", |
| 1850 | ) |
| 1851 | _run_git(repo_path, "add", ".") |
| 1852 | _run_git(repo_path, "commit", "-m", "base id") |
| 1853 | |
| 1854 | db_path = tmp_path / "test.db" |
| 1855 | database_url = f"sqlite:///{db_path}" |
| 1856 | |
| 1857 | up_result = runner.invoke( |
| 1858 | app, |
| 1859 | [ |
| 1860 | "up", |
| 1861 | "--project", |
| 1862 | str(repo_path), |
| 1863 | "--database", |
| 1864 | database_url, |
| 1865 | "--if-modified", |
| 1866 | "ignore", |
| 1867 | ], |
| 1868 | ) |
| 1869 | assert up_result.exit_code == 0 |
| 1870 | |
| 1871 | _run_git(repo_path, "checkout", "-b", "target") |
| 1872 | (repo_path / "001-init.config.toml").write_text( |
| 1873 | 'id = "001-renamed"', |
| 1874 | encoding="utf-8", |
| 1875 | ) |
| 1876 | _run_git(repo_path, "add", ".") |
| 1877 | _run_git(repo_path, "commit", "-m", "rename migration id") |
| 1878 | _run_git(repo_path, "checkout", base_branch) |
| 1879 | |
| 1880 | checkout_result = runner.invoke( |
| 1881 | app, |
| 1882 | [ |
| 1883 | "checkout", |
| 1884 | "target", |
| 1885 | "--project", |
| 1886 | str(repo_path), |
| 1887 | "--database", |
| 1888 | database_url, |
| 1889 | ], |
| 1890 | ) |
| 1891 | assert checkout_result.exit_code == 0 |
| 1892 | assert checkout_result.exception is None |
| 1893 | |
| 1894 | with sqlite3.connect(db_path) as connection: |
| 1895 | applied_ids = connection.execute( |
| 1896 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1897 | ).fetchall() |
| 1898 | |
| 1899 | assert [row[0] for row in applied_ids] == ["001-renamed"] |
| 1900 | |
| 1901 | |
| 1902 | def test_checkout_handles_config_added_for_existing_migration(tmp_path): |
| 1903 | repo_path = tmp_path / "repo" |
| 1904 | repo_path.mkdir() |
| 1905 | _write_project_config(repo_path) |
| 1906 | _init_git_repo(repo_path) |
| 1907 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1908 | |
| 1909 | (repo_path / "001-init.up.sql").write_text( |
| 1910 | "CREATE TABLE table_one (id int);", |
| 1911 | encoding="utf-8", |
| 1912 | ) |
| 1913 | (repo_path / "001-init.down.sql").write_text( |
| 1914 | "DROP TABLE table_one;", |
| 1915 | encoding="utf-8", |
| 1916 | ) |
| 1917 | _run_git(repo_path, "add", ".") |
| 1918 | _run_git(repo_path, "commit", "-m", "base without config") |
| 1919 | |
| 1920 | db_path = tmp_path / "test.db" |
| 1921 | database_url = f"sqlite:///{db_path}" |
| 1922 | |
| 1923 | up_result = runner.invoke( |
| 1924 | app, |
| 1925 | [ |
| 1926 | "up", |
| 1927 | "--project", |
| 1928 | str(repo_path), |
| 1929 | "--database", |
| 1930 | database_url, |
| 1931 | "--if-modified", |
| 1932 | "ignore", |
| 1933 | ], |
| 1934 | ) |
| 1935 | assert up_result.exit_code == 0 |
| 1936 | |
| 1937 | _run_git(repo_path, "checkout", "-b", "target") |
| 1938 | (repo_path / "001-init.config.toml").write_text( |
| 1939 | 'id = "001-renamed"', |
| 1940 | encoding="utf-8", |
| 1941 | ) |
| 1942 | _run_git(repo_path, "add", ".") |
| 1943 | _run_git(repo_path, "commit", "-m", "add config and rename id") |
| 1944 | _run_git(repo_path, "checkout", base_branch) |
| 1945 | |
| 1946 | checkout_result = runner.invoke( |
| 1947 | app, |
| 1948 | [ |
| 1949 | "checkout", |
| 1950 | "target", |
| 1951 | "--project", |
| 1952 | str(repo_path), |
| 1953 | "--database", |
| 1954 | database_url, |
| 1955 | ], |
| 1956 | ) |
| 1957 | assert checkout_result.exit_code == 0 |
| 1958 | assert checkout_result.exception is None |
| 1959 | |
| 1960 | with sqlite3.connect(db_path) as connection: |
| 1961 | applied_ids = connection.execute( |
| 1962 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 1963 | ).fetchall() |
| 1964 | |
| 1965 | assert [row[0] for row in applied_ids] == ["001-renamed"] |
| 1966 | |
| 1967 | |
| 1968 | def test_checkout_handles_config_removed_for_existing_migration(tmp_path): |
| 1969 | repo_path = tmp_path / "repo" |
| 1970 | repo_path.mkdir() |
| 1971 | _write_project_config(repo_path) |
| 1972 | _init_git_repo(repo_path) |
| 1973 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 1974 | |
| 1975 | (repo_path / "001-init.up.sql").write_text( |
| 1976 | "CREATE TABLE table_one (id int);", |
| 1977 | encoding="utf-8", |
| 1978 | ) |
| 1979 | (repo_path / "001-init.down.sql").write_text( |
| 1980 | "DROP TABLE table_one;", |
| 1981 | encoding="utf-8", |
| 1982 | ) |
| 1983 | (repo_path / "001-init.config.toml").write_text( |
| 1984 | 'id = "001-custom"', |
| 1985 | encoding="utf-8", |
| 1986 | ) |
| 1987 | _run_git(repo_path, "add", ".") |
| 1988 | _run_git(repo_path, "commit", "-m", "base with config") |
| 1989 | |
| 1990 | db_path = tmp_path / "test.db" |
| 1991 | database_url = f"sqlite:///{db_path}" |
| 1992 | |
| 1993 | up_result = runner.invoke( |
| 1994 | app, |
| 1995 | [ |
| 1996 | "up", |
| 1997 | "--project", |
| 1998 | str(repo_path), |
| 1999 | "--database", |
| 2000 | database_url, |
| 2001 | "--if-modified", |
| 2002 | "ignore", |
| 2003 | ], |
| 2004 | ) |
| 2005 | assert up_result.exit_code == 0 |
| 2006 | |
| 2007 | _run_git(repo_path, "checkout", "-b", "target") |
| 2008 | _run_git(repo_path, "rm", "001-init.config.toml") |
| 2009 | _run_git(repo_path, "commit", "-m", "remove config") |
| 2010 | _run_git(repo_path, "checkout", base_branch) |
| 2011 | |
| 2012 | checkout_result = runner.invoke( |
| 2013 | app, |
| 2014 | [ |
| 2015 | "checkout", |
| 2016 | "target", |
| 2017 | "--project", |
| 2018 | str(repo_path), |
| 2019 | "--database", |
| 2020 | database_url, |
| 2021 | ], |
| 2022 | ) |
| 2023 | assert checkout_result.exit_code == 0 |
| 2024 | assert checkout_result.exception is None |
| 2025 | |
| 2026 | with sqlite3.connect(db_path) as connection: |
| 2027 | applied_ids = connection.execute( |
| 2028 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 2029 | ).fetchall() |
| 2030 | table_exists = connection.execute( |
| 2031 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", |
| 2032 | ).fetchone() |
| 2033 | |
| 2034 | assert [row[0] for row in applied_ids] == ["001-init"] |
| 2035 | assert table_exists is not None |
| 2036 | |
| 2037 | |
| 2038 | def test_checkout_handles_config_and_scripts_deleted_together(tmp_path): |
| 2039 | repo_path = tmp_path / "repo" |
| 2040 | repo_path.mkdir() |
| 2041 | _write_project_config(repo_path) |
| 2042 | _init_git_repo(repo_path) |
| 2043 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 2044 | |
| 2045 | (repo_path / "001-init.up.sql").write_text( |
| 2046 | "CREATE TABLE table_one (id int);", |
| 2047 | encoding="utf-8", |
| 2048 | ) |
| 2049 | (repo_path / "001-init.down.sql").write_text( |
| 2050 | "DROP TABLE table_one;", |
| 2051 | encoding="utf-8", |
| 2052 | ) |
| 2053 | (repo_path / "001-init.config.toml").write_text( |
| 2054 | 'id = "001-custom"', |
| 2055 | encoding="utf-8", |
| 2056 | ) |
| 2057 | _run_git(repo_path, "add", ".") |
| 2058 | _run_git(repo_path, "commit", "-m", "base with migration config") |
| 2059 | |
| 2060 | db_path = tmp_path / "test.db" |
| 2061 | database_url = f"sqlite:///{db_path}" |
| 2062 | |
| 2063 | up_result = runner.invoke( |
| 2064 | app, |
| 2065 | [ |
| 2066 | "up", |
| 2067 | "--project", |
| 2068 | str(repo_path), |
| 2069 | "--database", |
| 2070 | database_url, |
| 2071 | "--if-modified", |
| 2072 | "ignore", |
| 2073 | ], |
| 2074 | ) |
| 2075 | assert up_result.exit_code == 0 |
| 2076 | |
| 2077 | _run_git(repo_path, "checkout", "-b", "target") |
| 2078 | _run_git( |
| 2079 | repo_path, |
| 2080 | "rm", |
| 2081 | "001-init.config.toml", |
| 2082 | "001-init.up.sql", |
| 2083 | "001-init.down.sql", |
| 2084 | ) |
| 2085 | _run_git(repo_path, "commit", "-m", "remove migration and config") |
| 2086 | _run_git(repo_path, "checkout", base_branch) |
| 2087 | |
| 2088 | checkout_result = runner.invoke( |
| 2089 | app, |
| 2090 | [ |
| 2091 | "checkout", |
| 2092 | "target", |
| 2093 | "--project", |
| 2094 | str(repo_path), |
| 2095 | "--database", |
| 2096 | database_url, |
| 2097 | ], |
| 2098 | ) |
| 2099 | assert checkout_result.exit_code == 0 |
| 2100 | assert checkout_result.exception is None |
| 2101 | |
| 2102 | with sqlite3.connect(db_path) as connection: |
| 2103 | applied_ids = connection.execute( |
| 2104 | "SELECT id FROM mitch_migrations ORDER BY id", |
| 2105 | ).fetchall() |
| 2106 | table_exists = connection.execute( |
| 2107 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", |
| 2108 | ).fetchone() |
| 2109 | |
| 2110 | assert applied_ids == [] |
| 2111 | assert table_exists is None |
| 2112 | |
| 2113 | |
| 2114 | def test_checkout_allow_dirty_restores_stashed_changes_after_failure(tmp_path): |
| 2115 | repo_path = tmp_path / "repo" |
| 2116 | repo_path.mkdir() |
| 2117 | _write_project_config(repo_path) |
| 2118 | _init_git_repo(repo_path) |
| 2119 | base_branch = _run_git(repo_path, "branch", "--show-current") |
| 2120 | |
| 2121 | (repo_path / "README.md").write_text("base\n", encoding="utf-8") |
| 2122 | (repo_path / "001-init.up.sql").write_text( |
| 2123 | "CREATE TABLE table_one (id int);", |
| 2124 | encoding="utf-8", |
| 2125 | ) |
| 2126 | (repo_path / "001-init.down.sql").write_text( |
| 2127 | "DROP TABLE table_one;", |
| 2128 | encoding="utf-8", |
| 2129 | ) |
| 2130 | _run_git(repo_path, "add", ".") |
| 2131 | _run_git(repo_path, "commit", "-m", "base migration") |
| 2132 | |
| 2133 | db_path = tmp_path / "test.db" |
| 2134 | database_url = f"sqlite:///{db_path}" |
| 2135 | |
| 2136 | up_result = runner.invoke( |
| 2137 | app, |
| 2138 | [ |
| 2139 | "up", |
| 2140 | "--project", |
| 2141 | str(repo_path), |
| 2142 | "--database", |
| 2143 | database_url, |
| 2144 | "--if-modified", |
| 2145 | "ignore", |
| 2146 | ], |
| 2147 | ) |
| 2148 | assert up_result.exit_code == 0 |
| 2149 | |
| 2150 | _run_git(repo_path, "checkout", "-b", "target") |
| 2151 | (repo_path / "002-broken.up.sql").write_text( |
| 2152 | "THIS IS NOT VALID SQL;", |
| 2153 | encoding="utf-8", |
| 2154 | ) |
| 2155 | (repo_path / "002-broken.down.sql").write_text( |
| 2156 | "-- noop", |
| 2157 | encoding="utf-8", |
| 2158 | ) |
| 2159 | _run_git(repo_path, "add", ".") |
| 2160 | _run_git(repo_path, "commit", "-m", "broken migration") |
| 2161 | _run_git(repo_path, "checkout", base_branch) |
| 2162 | |
| 2163 | (repo_path / "README.md").write_text("dirty local change\n", encoding="utf-8") |
| 2164 | |
| 2165 | checkout_result = runner.invoke( |
| 2166 | app, |
| 2167 | [ |
| 2168 | "checkout", |
| 2169 | "target", |
| 2170 | "--project", |
| 2171 | str(repo_path), |
| 2172 | "--database", |
| 2173 | database_url, |
| 2174 | "--allow-dirty", |
| 2175 | "--and-up", |
| 2176 | ], |
| 2177 | ) |
| 2178 | assert checkout_result.exit_code == 1 |
| 2179 | |
| 2180 | restored_readme = (repo_path / "README.md").read_text(encoding="utf-8") |
| 2181 | stash_list = _run_git(repo_path, "stash", "list") |
| 2182 | |
| 2183 | assert restored_readme == "dirty local change\n" |
| 2184 | assert stash_list == "" |