| 1 | import sqlite3 |
| 2 | import subprocess |
| 3 | |
| 4 | import pytest |
| 5 | from typer.testing import CliRunner |
| 6 | |
| 7 | from mitch.cli import app |
| 8 | |
| 9 | runner = CliRunner() |
| 10 | |
| 11 | |
| 12 | def _write_project_config(project_root, project_id="tests-hg-project"): |
| 13 | (project_root / "mitch.toml").write_text(f'[project]\nid = "{project_id}"\n', encoding="utf-8") |
| 14 | |
| 15 | |
| 16 | def _has_hg() -> bool: |
| 17 | result = subprocess.run( |
| 18 | ["hg", "--version"], |
| 19 | capture_output=True, |
| 20 | text=True, |
| 21 | check=False, |
| 22 | ) |
| 23 | return result.returncode == 0 |
| 24 | |
| 25 | |
| 26 | pytestmark = pytest.mark.skipif(not _has_hg(), reason="Mercurial (hg) is not available") |
| 27 | |
| 28 | |
| 29 | def _run_hg(cwd, *args): |
| 30 | result = subprocess.run( |
| 31 | ["hg", *args], |
| 32 | cwd=cwd, |
| 33 | capture_output=True, |
| 34 | text=True, |
| 35 | check=False, |
| 36 | ) |
| 37 | assert result.returncode == 0, result.stderr or result.stdout |
| 38 | return result.stdout.strip() |
| 39 | |
| 40 | |
| 41 | def _init_hg_repo(path): |
| 42 | _run_hg(path, "init") |
| 43 | |
| 44 | |
| 45 | def test_checkout_hg_reverts_migrations_missing_on_target_branch(tmp_path): |
| 46 | repo_path = tmp_path / "repo" |
| 47 | repo_path.mkdir() |
| 48 | _write_project_config(repo_path) |
| 49 | _init_hg_repo(repo_path) |
| 50 | |
| 51 | (repo_path / "001-init.up.sql").write_text( |
| 52 | "CREATE TABLE table_one (id int);", |
| 53 | encoding="utf-8", |
| 54 | ) |
| 55 | (repo_path / "001-init.down.sql").write_text( |
| 56 | "DROP TABLE table_one;", |
| 57 | encoding="utf-8", |
| 58 | ) |
| 59 | _run_hg(repo_path, "add", ".") |
| 60 | _run_hg(repo_path, "commit", "-m", "add 001", "-u", "test") |
| 61 | |
| 62 | db_path = tmp_path / "test.db" |
| 63 | database_url = f"sqlite:///{db_path}" |
| 64 | |
| 65 | up_result = runner.invoke( |
| 66 | app, |
| 67 | [ |
| 68 | "up", |
| 69 | "--project", |
| 70 | str(repo_path), |
| 71 | "--database", |
| 72 | database_url, |
| 73 | "--if-modified", |
| 74 | "ignore", |
| 75 | ], |
| 76 | ) |
| 77 | assert up_result.exit_code == 0 |
| 78 | |
| 79 | _run_hg(repo_path, "branch", "target") |
| 80 | _run_hg(repo_path, "rm", "001-init.up.sql", "001-init.down.sql") |
| 81 | _run_hg(repo_path, "commit", "-m", "remove migration", "-u", "test") |
| 82 | _run_hg(repo_path, "update", "default") |
| 83 | |
| 84 | checkout_result = runner.invoke( |
| 85 | app, |
| 86 | [ |
| 87 | "checkout", |
| 88 | "target", |
| 89 | "--project", |
| 90 | str(repo_path), |
| 91 | "--database", |
| 92 | database_url, |
| 93 | ], |
| 94 | ) |
| 95 | assert checkout_result.exit_code == 0 |
| 96 | |
| 97 | branch = _run_hg(repo_path, "branch") |
| 98 | assert branch == "target" |
| 99 | |
| 100 | with sqlite3.connect(db_path) as connection: |
| 101 | rows = connection.execute("SELECT id FROM mitch_migrations").fetchall() |
| 102 | table = connection.execute( |
| 103 | "SELECT name FROM sqlite_schema WHERE type='table' AND name='table_one'", |
| 104 | ).fetchone() |
| 105 | |
| 106 | assert rows == [] |
| 107 | assert table is None |
| 108 | |
| 109 | |
| 110 | def _write_linked_layout(root, users): |
| 111 | _write_project_config(root, "app") |
| 112 | (root / "groups.up.sql").write_text("CREATE TABLE groups (id int);", encoding="utf-8") |
| 113 | (root / "groups.down.sql").write_text("DROP TABLE groups;", encoding="utf-8") |
| 114 | (root / "groups.config.toml").write_text('dependencies = ["users:create-users"]\n', encoding="utf-8") |
| 115 | users.mkdir(exist_ok=True) |
| 116 | _write_project_config(users, "org.example.users") |
| 117 | (users / "create-users.up.sql").write_text("CREATE TABLE users (id int);", encoding="utf-8") |
| 118 | (users / "create-users.down.sql").write_text("DROP TABLE users;", encoding="utf-8") |
| 119 | |
| 120 | |
| 121 | def test_checkout_hg_warns_when_nested_linked_project_differs_on_target(tmp_path): |
| 122 | root = tmp_path / "app" |
| 123 | root.mkdir() |
| 124 | _write_linked_layout(root, root / "users") |
| 125 | _init_hg_repo(root) |
| 126 | _run_hg(root, "add", ".") |
| 127 | _run_hg(root, "commit", "-m", "base", "-u", "test") |
| 128 | database_url = f"sqlite:///{tmp_path / 'test.db'}" |
| 129 | assert runner.invoke(app, ["up", "-p", str(root), "-d", database_url]).exit_code == 0 |
| 130 | |
| 131 | (root / "users" / "create-users.up.sql").write_text("CREATE TABLE users (id int, name text);", encoding="utf-8") |
| 132 | _run_hg(root, "commit", "-m", "change linked", "-u", "test") |
| 133 | _run_hg(root, "update", "0") |
| 134 | |
| 135 | result = runner.invoke(app, ["checkout", "1", "-p", str(root), "-d", database_url]) |
| 136 | |
| 137 | assert result.exit_code == 0, result.output |
| 138 | assert "Warning" in result.output and "users" in result.output |
| 139 | |
| 140 | |
| 141 | def test_checkout_hg_warns_when_subrepository_of_linked_project_moves(tmp_path): |
| 142 | root = tmp_path / "app" |
| 143 | root.mkdir() |
| 144 | _init_hg_repo(root) |
| 145 | users = root / "users" |
| 146 | users.mkdir() |
| 147 | _init_hg_repo(users) |
| 148 | _write_linked_layout(root, users) |
| 149 | _run_hg(users, "add", ".") |
| 150 | _run_hg(users, "commit", "-m", "v1", "-u", "test") |
| 151 | (root / ".hgsub").write_text("users = users\n", encoding="utf-8") |
| 152 | _run_hg(root, "add", ".hgsub", "groups.up.sql", "groups.down.sql", "groups.config.toml", "mitch.toml") |
| 153 | _run_hg(root, "commit", "-m", "base", "-u", "test") |
| 154 | database_url = f"sqlite:///{tmp_path / 'test.db'}" |
| 155 | assert runner.invoke(app, ["up", "-p", str(root), "-d", database_url]).exit_code == 0 |
| 156 | |
| 157 | (users / "audit.up.sql").write_text("CREATE TABLE audit (id int);", encoding="utf-8") |
| 158 | _run_hg(users, "add", "audit.up.sql") |
| 159 | _run_hg(users, "commit", "-m", "v2", "-u", "test") |
| 160 | _run_hg(root, "commit", "-m", "bump users", "-u", "test") |
| 161 | _run_hg(root, "update", "0") |
| 162 | |
| 163 | result = runner.invoke(app, ["checkout", "1", "-p", str(root), "-d", database_url]) |
| 164 | |
| 165 | assert result.exit_code == 0, result.output |
| 166 | assert "Warning" in result.output and "users" in result.output |