Public repository: timo / mitch

Reader

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

mitch / tests / test_linked_projects.py · 25.2 KB · at 76a2c3d332a1 · raw

Outline: 56 definitions
1
"""Linked projects: migrations that depend on migrations from other projects.
2
3
All tests drive mitch through the CLI against SQLite files under tmp_path.
4
"""
5
6
import sqlite3
7
from pathlib import Path
8
9
from typer.testing import CliRunner
10
11
from mitch.cli import app
12
13
runner = CliRunner()
14
15
16
def make_project(
17
    path: Path,
18
    project_id: str,
19
    migrations: dict[str, dict] | None = None,
20
    links: dict[str, str] | None = None,
21
) -> Path:
22
    """Write a project directory.
23
24
    `migrations` maps a migration ID to a dict with optional keys `up`, `down` and `dependencies`.
25
    `links` maps an alias to a path relative to the project root.
26
    """
27
    path.mkdir(parents=True, exist_ok=True)
28
    lines = ["[project]", f'id = "{project_id}"']
29
    if links:
30
        lines.append("")
31
        lines.append("[projects]")
32
        lines.extend(f'"{alias}" = "{target}"' for alias, target in links.items())
33
    (path / "mitch.toml").write_text("\n".join(lines) + "\n", encoding="utf-8")
34
35
    for migration_id, spec in (migrations or {}).items():
36
        (path / f"{migration_id}.up.sql").write_text(spec.get("up", ""), encoding="utf-8")
37
        if "down" in spec:
38
            (path / f"{migration_id}.down.sql").write_text(spec["down"], encoding="utf-8")
39
        if spec.get("dependencies"):
40
            deps = ", ".join(f'"{d}"' for d in spec["dependencies"])
41
            (path / f"{migration_id}.config.toml").write_text(f"dependencies = [{deps}]\n", encoding="utf-8")
42
    return path
43
44
45
def table(name: str) -> dict:
46
    """Migration spec creating and dropping a table."""
47
    return {"up": f"CREATE TABLE {name} (id int);", "down": f"DROP TABLE {name};"}
48
49
50
def table_with(name: str, *dependencies: str) -> dict:
51
    return {**table(name), "dependencies": list(dependencies)}
52
53
54
def tables(db_path: Path) -> set[str]:
55
    with sqlite3.connect(db_path) as connection:
56
        rows = connection.execute("SELECT name FROM sqlite_schema WHERE type = 'table'").fetchall()
57
    return {row[0] for row in rows if not row[0].startswith("mitch_")}
58
59
60
def applied(db_path: Path) -> set[tuple[str, str]]:
61
    """(project ID, migration ID) pairs recorded as applied."""
62
    with sqlite3.connect(db_path) as connection:
63
        rows = connection.execute(
64
            "SELECT p.id, m.id FROM mitch_migrations m JOIN mitch_projects p ON p.local_id = m.project_id"
65
        ).fetchall()
66
    return {(row[0], row[1]) for row in rows}
67
68
69
def mitch(*args: str):
70
    return runner.invoke(app, list(args))
71
72
73
def test_up_applies_required_migration_of_nested_project_but_nothing_else(tmp_path):
74
    root = make_project(tmp_path / "app", "app", {"groups": table_with("groups", "users:create-users")})
75
    make_project(root / "users", "org.example.users", {"create-users": table("users"), "audit": table("audit")})
76
    db = tmp_path / "test.db"
77
78
    result = mitch("up", "-p", str(root), "-d", f"sqlite:///{db}")
79
80
    assert result.exit_code == 0, result.output
81
    assert tables(db) == {"users", "groups"}
82
    assert applied(db) == {("app", "groups"), ("org.example.users", "create-users")}
83
84
85
def db_url(tmp_path: Path) -> str:
86
    return f"sqlite:///{tmp_path / 'test.db'}"
87
88
89
def test_declared_link_to_sibling_directory(tmp_path):
90
    root = make_project(
91
        tmp_path / "app", "app", {"groups": table_with("groups", "um:create-users")}, links={"um": "../shared/users"}
92
    )
93
    make_project(tmp_path / "shared" / "users", "org.example.users", {"create-users": table("users")})
94
95
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
96
97
    assert result.exit_code == 0, result.output
98
    assert tables(tmp_path / "test.db") == {"users", "groups"}
99
100
101
def test_declaration_overrides_alias_of_nested_project(tmp_path):
102
    root = make_project(
103
        tmp_path / "app", "app", {"groups": table_with("groups", "um:create-users")}, links={"um": "vendor/users"}
104
    )
105
    make_project(root / "vendor" / "users", "org.example.users", {"create-users": table("users")})
106
107
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
108
109
    assert result.exit_code == 0, result.output
110
    assert tables(tmp_path / "test.db") == {"users", "groups"}
111
112
113
def test_two_nested_projects_with_the_same_directory_name_collide(tmp_path):
114
    root = make_project(tmp_path / "app", "app", {"groups": table("groups")})
115
    make_project(root / "a" / "users", "org.example.users")
116
    make_project(root / "b" / "users", "org.example.other-users")
117
118
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
119
120
    assert result.exit_code == 1
121
    assert "Alias 'users'" in result.output
122
123
124
def test_declared_link_without_project_fails_with_alias_and_path(tmp_path):
125
    root = make_project(tmp_path / "app", "app", {"groups": table("groups")}, links={"um": "vendor/users"})
126
    (root / "vendor" / "users").mkdir(parents=True)
127
128
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
129
130
    assert result.exit_code == 1
131
    assert "'um'" in result.output
132
    assert str(root / "vendor" / "users") in result.output
133
134
135
def test_status_only_warns_about_missing_linked_project(tmp_path):
136
    root = make_project(tmp_path / "app", "app", {"groups": table("groups")}, links={"um": "vendor/users"})
137
138
    result = mitch("status", "-p", str(root), "-d", db_url(tmp_path), "--no-ci")
139
140
    assert result.exit_code == 0, result.output
141
    assert "'um'" in result.output
142
    assert "A groups" in result.output
143
144
145
def test_alias_with_colon_is_rejected(tmp_path):
146
    root = make_project(tmp_path / "app", "app", {"groups": table("groups")}, links={"u:m": "../users"})
147
    make_project(tmp_path / "users", "org.example.users")
148
149
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
150
151
    assert result.exit_code == 1
152
    assert "u:m" in result.output
153
154
155
def test_reference_to_transitively_linked_project_is_rejected(tmp_path):
156
    root = make_project(tmp_path / "app", "app", {"groups": table_with("groups", "utils:helpers")})
157
    users = make_project(root / "users", "org.example.users", {"create-users": table("users")})
158
    make_project(users / "utils", "org.example.utils", {"helpers": table("helpers")})
159
160
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
161
162
    assert result.exit_code == 1
163
    assert "utils:helpers" in result.output
164
    assert tables(tmp_path / "test.db") == set()
165
166
167
def _diamond(tmp_path, utils_in_b: dict | None = None):
168
    """app links a and b; both link their own copy of utils under different aliases."""
169
    root = make_project(
170
        tmp_path / "app", "app", {"top": table_with("top", "a:in-a", "b:in-b")}, links={"a": "../a", "b": "../b"}
171
    )
172
    a = make_project(
173
        tmp_path / "a", "org.example.a", {"in-a": table_with("in_a", "u:helpers")}, links={"u": "vendor/utils"}
174
    )
175
    b = make_project(
176
        tmp_path / "b", "org.example.b", {"in-b": table_with("in_b", "tools:helpers")}, links={"tools": "vendor/utils"}
177
    )
178
    make_project(a / "vendor" / "utils", "org.example.utils", {"helpers": table("helpers")})
179
    make_project(b / "vendor" / "utils", "org.example.utils", utils_in_b or {"helpers": table("helpers")})
180
    return root
181
182
183
def test_diamond_applies_shared_project_once_under_its_project_id(tmp_path):
184
    root = _diamond(tmp_path)
185
186
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
187
188
    assert result.exit_code == 0, result.output
189
    assert tables(tmp_path / "test.db") == {"top", "in_a", "in_b", "helpers"}
190
    assert ("org.example.utils", "helpers") in applied(tmp_path / "test.db")
191
192
193
def test_copies_with_different_content_for_the_same_migration_conflict(tmp_path):
194
    root = _diamond(tmp_path, utils_in_b={"helpers": {"up": "CREATE TABLE helpers (id int, name text);"}})
195
196
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
197
198
    assert result.exit_code == 1
199
    assert "helpers" in result.output and "differs between copies" in result.output
200
    assert tables(tmp_path / "test.db") == set()
201
202
203
def test_copies_differing_only_in_formatting_or_set_of_migrations_are_merged(tmp_path):
204
    root = _diamond(
205
        tmp_path,
206
        utils_in_b={"helpers": {"up": "create table helpers (id int); -- same"}, "extra": table("extra")},
207
    )
208
209
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
210
211
    assert result.exit_code == 0, result.output
212
    assert tables(tmp_path / "test.db") == {"top", "in_a", "in_b", "helpers"}
213
214
215
def test_links_may_form_a_cycle(tmp_path):
216
    root = make_project(tmp_path / "a", "org.example.a", {"in-a": table_with("in_a", "b:in-b")}, links={"b": "../b"})
217
    make_project(
218
        tmp_path / "b",
219
        "org.example.b",
220
        {"in-b": table("in_b"), "uses-a": table_with("uses_a", "a:in-a")},
221
        links={"a": "../a"},
222
    )
223
224
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
225
226
    assert result.exit_code == 0, result.output
227
    assert tables(tmp_path / "test.db") == {"in_a", "in_b"}
228
229
230
def test_cycle_in_migration_graph_across_projects_is_an_error(tmp_path):
231
    root = make_project(tmp_path / "a", "org.example.a", {"in-a": table_with("in_a", "b:in-b")}, links={"b": "../b"})
232
    make_project(tmp_path / "b", "org.example.b", {"in-b": table_with("in_b", "a:in-a")}, links={"a": "../a"})
233
234
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
235
236
    assert result.exit_code == 1
237
    assert "cycle" in result.output
238
239
240
def _app_with_users(tmp_path, app_migrations: dict | None = None) -> Path:
241
    root = make_project(
242
        tmp_path / "app",
243
        "app",
244
        app_migrations or {"groups": table_with("groups", "um:create-users"), "teams": table("teams")},
245
        links={"um": "../users"},
246
    )
247
    make_project(tmp_path / "users", "org.example.users", {"create-users": table("users"), "audit": table("audit")})
248
    return root
249
250
251
def _write_dependencies(project: Path, migration_id: str, *dependencies: str) -> None:
252
    deps = ", ".join(f'"{d}"' for d in dependencies)
253
    (project / f"{migration_id}.config.toml").write_text(f"dependencies = [{deps}]\n", encoding="utf-8")
254
255
256
def test_add_rejects_migrations_of_linked_projects(tmp_path):
257
    root = _app_with_users(tmp_path)
258
259
    result = mitch("add", "um:audit", "-p", str(root), "-d", db_url(tmp_path))
260
261
    assert result.exit_code == 1
262
    assert "um:audit" in result.output
263
    assert tables(tmp_path / "test.db") == set()
264
265
266
def test_drop_of_linked_migration_reverts_it_with_its_dependants(tmp_path):
267
    root = _app_with_users(tmp_path)
268
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
269
270
    result = mitch("drop", "um:create-users", "-p", str(root), "-d", db_url(tmp_path))
271
272
    assert result.exit_code == 0, result.output
273
    assert tables(tmp_path / "test.db") == {"teams"}
274
275
276
def test_down_also_reverts_orphaned_linked_migrations(tmp_path):
277
    root = _app_with_users(tmp_path)
278
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
279
280
    result = mitch("down", "-p", str(root), "-d", db_url(tmp_path))
281
282
    assert result.exit_code == 0, result.output
283
    assert tables(tmp_path / "test.db") == set()
284
    assert applied(tmp_path / "test.db") == set()
285
286
287
def test_drop_of_own_migration_reverts_linked_migration_it_alone_required(tmp_path):
288
    root = _app_with_users(tmp_path)
289
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
290
291
    result = mitch("drop", "groups", "-p", str(root), "-d", db_url(tmp_path))
292
293
    assert result.exit_code == 0, result.output
294
    assert tables(tmp_path / "test.db") == {"teams"}
295
296
297
def test_down_keeps_linked_migration_another_project_on_the_deployment_still_needs(tmp_path):
298
    root = _app_with_users(tmp_path)
299
    other = make_project(
300
        tmp_path / "other",
301
        "org.example.other",
302
        {"profiles": table_with("profiles", "people:create-users")},
303
        links={"people": "../users"},
304
    )
305
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
306
    mitch("up", "-p", str(other), "-d", db_url(tmp_path))
307
308
    result = mitch("down", "-p", str(root), "-d", db_url(tmp_path))
309
310
    assert result.exit_code == 0, result.output
311
    assert tables(tmp_path / "test.db") == {"users", "profiles"}
312
313
    result = mitch("down", "-p", str(other), "-d", db_url(tmp_path))
314
315
    assert result.exit_code == 0, result.output
316
    assert tables(tmp_path / "test.db") == set()
317
318
319
def test_revert_is_refused_while_a_project_that_is_not_loaded_depends_on_it(tmp_path):
320
    root = _app_with_users(tmp_path)
321
    other = make_project(
322
        tmp_path / "other",
323
        "org.example.other",
324
        {"profiles": table_with("profiles", "people:create-users")},
325
        links={"people": "../users"},
326
    )
327
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
328
    mitch("up", "-p", str(other), "-d", db_url(tmp_path))
329
330
    result = mitch("drop", "um:create-users", "-p", str(root), "-d", db_url(tmp_path))
331
332
    assert result.exit_code == 1
333
    assert "org.example.other:profiles" in result.output
334
    assert tables(tmp_path / "test.db") == {"users", "groups", "teams", "profiles"}
335
336
337
def test_down_keeps_migrations_the_linked_project_applied_as_its_own(tmp_path):
338
    root = _app_with_users(tmp_path)
339
    mitch("up", "-p", str(tmp_path / "users"), "-d", db_url(tmp_path))
340
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
341
342
    result = mitch("down", "-p", str(root), "-d", db_url(tmp_path))
343
344
    assert result.exit_code == 0, result.output
345
    assert tables(tmp_path / "test.db") == {"users", "audit"}
346
347
348
def test_removed_dependency_lets_the_next_revert_clean_up_the_linked_migration(tmp_path):
349
    root = _app_with_users(tmp_path)
350
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
351
    (root / "groups.config.toml").unlink()
352
353
    result = mitch("drop", "teams", "-p", str(root), "-d", db_url(tmp_path))
354
355
    assert result.exit_code == 0, result.output
356
    assert tables(tmp_path / "test.db") == {"groups"}
357
358
359
def test_added_dependency_on_unapplied_migration_counts_as_modified(tmp_path):
360
    root = _app_with_users(tmp_path)
361
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
362
    _write_dependencies(root, "teams", "um:audit")
363
364
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
365
366
    assert result.exit_code != 0
367
    assert "teams" in result.output
368
    assert "audit" not in tables(tmp_path / "test.db")
369
370
371
def test_late_dependencies_flag_applies_added_dependency_and_keeps_the_dependant(tmp_path):
372
    root = _app_with_users(tmp_path)
373
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
374
    _write_dependencies(root, "teams", "um:audit")
375
376
    result = mitch("up", "--late-dependencies", "-p", str(root), "-d", db_url(tmp_path))
377
378
    assert result.exit_code == 0, result.output
379
    assert "audit" in tables(tmp_path / "test.db")
380
    assert "Warning" in result.output and "um:audit" in result.output
381
    with sqlite3.connect(tmp_path / "test.db") as connection:
382
        reverted = connection.execute(
383
            "SELECT count(*) FROM mitch_migration_audit WHERE migration_id = 'teams' AND action = 'reverted'"
384
        ).fetchone()[0]
385
    assert reverted == 0
386
387
388
def status_lines(result) -> list[str]:
389
    return [line for line in result.output.splitlines() if line[:2] in ("A ", "M ", "R ", "- ")]
390
391
392
def test_status_lists_required_linked_migrations_by_alias_and_hides_the_rest(tmp_path):
393
    root = _app_with_users(tmp_path)
394
395
    result = mitch("status", "-p", str(root), "-d", db_url(tmp_path), "--no-ci")
396
397
    assert result.exit_code == 0, result.output
398
    assert status_lines(result) == ["A groups", "A teams", "A um:create-users"]
399
400
401
def test_status_all_also_shows_migrations_of_direct_links_that_are_not_required(tmp_path):
402
    root = _app_with_users(tmp_path)
403
404
    result = mitch("status", "--all", "-p", str(root), "-d", db_url(tmp_path), "--no-ci")
405
406
    assert result.exit_code == 0, result.output
407
    assert "- um:audit" in status_lines(result)
408
409
410
def test_status_names_deeper_migrations_by_their_only_alias_path(tmp_path):
411
    root = make_project(tmp_path / "app", "app", {"groups": table_with("groups", "users:create-users")})
412
    users = make_project(root / "users", "org.example.users", {"create-users": table_with("users", "utils:helpers")})
413
    make_project(users / "utils", "org.example.utils", {"helpers": table("helpers")})
414
415
    result = mitch("status", "-p", str(root), "-d", db_url(tmp_path), "--no-ci")
416
417
    assert result.exit_code == 0, result.output
418
    assert "A users:utils:helpers" in status_lines(result)
419
420
421
def test_status_names_migrations_reachable_via_several_paths_by_project_id(tmp_path):
422
    root = _diamond(tmp_path)
423
424
    result = mitch("status", "-p", str(root), "-d", db_url(tmp_path), "--no-ci")
425
426
    assert result.exit_code == 0, result.output
427
    assert "A org.example.utils:helpers" in status_lines(result)
428
429
430
def test_status_fails_ci_for_modified_required_linked_migration_in_strict_mode(tmp_path):
431
    root = _app_with_users(tmp_path)
432
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
433
    (tmp_path / "users" / "create-users.up.sql").write_text("CREATE TABLE users (id int, name text);", encoding="utf-8")
434
435
    result = mitch("status", "--ci", "--strict", "-p", str(root), "-d", db_url(tmp_path))
436
437
    assert result.exit_code == 3
438
    assert "M um:create-users" in status_lines(result)
439
440
441
def test_status_reports_missing_only_for_own_migrations(tmp_path):
442
    root = _app_with_users(tmp_path)
443
    newer = make_project(
444
        tmp_path / "other",
445
        "org.example.other",
446
        {"profiles": table_with("profiles", "people:extra")},
447
        links={"people": "vendor/users"},
448
    )
449
    make_project(
450
        newer / "vendor" / "users", "org.example.users", {"create-users": table("users"), "extra": table("extra")}
451
    )
452
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
453
    mitch("up", "-p", str(newer), "-d", db_url(tmp_path))
454
455
    result = mitch("status", "--ci", "-p", str(root), "-d", db_url(tmp_path))
456
457
    assert result.exit_code == 0, result.output
458
    assert status_lines(result) == []
459
460
461
def test_log_shows_required_linked_migrations_by_alias(tmp_path):
462
    root = _app_with_users(tmp_path)
463
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
464
465
    result = mitch("log", "-p", str(root), "-d", db_url(tmp_path))
466
467
    assert result.exit_code == 0, result.output
468
    assert "um:create-users" in result.output
469
    assert "groups" in result.output
470
471
472
def test_create_accepts_dependency_on_linked_migration(tmp_path):
473
    root = _app_with_users(tmp_path)
474
475
    result = mitch("create", "profiles", "-p", str(root), "-d", "um:create-users")
476
477
    assert result.exit_code == 0, result.output
478
    assert "um:create-users" in (root / "profiles.config.toml").read_text(encoding="utf-8")
479
480
481
def test_create_rejects_unknown_linked_migration(tmp_path):
482
    root = _app_with_users(tmp_path)
483
484
    result = mitch("create", "profiles", "-p", str(root), "-d", "um:nope")
485
486
    assert result.exit_code == 1
487
    assert "um:nope" in result.output
488
    assert not (root / "profiles.up.sql").exists()
489
490
491
def test_init_rejects_project_id_with_whitespace(tmp_path, monkeypatch):
492
    monkeypatch.chdir(tmp_path)
493
494
    result = mitch("init", "my project")
495
496
    assert result.exit_code == 1
497
    assert not (tmp_path / "mitch.toml").exists()
498
499
500
# ---------------------------------------------------------------- checkout
501
502
503
def _git(cwd: Path, *args: str) -> str:
504
    import subprocess
505
506
    result = subprocess.run(
507
        ["git", "-c", "protocol.file.allow=always", *args], cwd=cwd, capture_output=True, text=True, check=False
508
    )
509
    assert result.returncode == 0, result.stderr or result.stdout
510
    return result.stdout.strip()
511
512
513
def _git_init(path: Path) -> None:
514
    _git(path, "init")
515
    _git(path, "config", "user.email", "test@example.com")
516
    _git(path, "config", "user.name", "Test User")
517
518
519
def _commit_all(path: Path, message: str) -> None:
520
    _git(path, "add", ".")
521
    _git(path, "commit", "-m", message)
522
523
524
def test_checkout_warns_when_nested_linked_project_differs_on_target(tmp_path):
525
    root = make_project(tmp_path / "app", "app", {"groups": table_with("groups", "users:create-users")})
526
    users = make_project(root / "users", "org.example.users", {"create-users": table("users")})
527
    _git_init(root)
528
    _commit_all(root, "base")
529
    base = _git(root, "branch", "--show-current")
530
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
531
532
    _git(root, "checkout", "-b", "target")
533
    (users / "create-users.up.sql").write_text("CREATE TABLE users (id int, name text);", encoding="utf-8")
534
    _commit_all(root, "change linked migration")
535
    _git(root, "checkout", base)
536
537
    result = mitch("checkout", "target", "-p", str(root), "-d", db_url(tmp_path))
538
539
    assert result.exit_code == 0, result.output
540
    assert _git(root, "branch", "--show-current") == "target"
541
    assert "Warning" in result.output and "users" in result.output
542
    assert tables(tmp_path / "test.db") == {"users", "groups"}
543
544
545
def test_checkout_warns_when_submodule_of_linked_project_moves(tmp_path):
546
    upstream = make_project(tmp_path / "users-upstream", "org.example.users", {"create-users": table("users")})
547
    _git_init(upstream)
548
    _commit_all(upstream, "v1")
549
550
    root = make_project(tmp_path / "app", "app", {"groups": table_with("groups", "users:create-users")})
551
    _git_init(root)
552
    _git(root, "submodule", "add", str(upstream), "users")
553
    _commit_all(root, "base")
554
    base = _git(root, "branch", "--show-current")
555
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
556
557
    (upstream / "audit.up.sql").write_text("CREATE TABLE audit (id int);", encoding="utf-8")
558
    _commit_all(upstream, "v2")
559
    _git(root, "checkout", "-b", "target")
560
    _git(root / "users", "pull", "origin")
561
    _commit_all(root, "bump users")
562
    _git(root, "checkout", base)
563
    _git(root, "submodule", "update")
564
565
    result = mitch("checkout", "target", "-p", str(root), "-d", db_url(tmp_path))
566
567
    assert result.exit_code == 0, result.output
568
    assert "Warning" in result.output and "users" in result.output
569
570
571
# ---------------------------------------------------------------- review findings
572
573
574
def _other_with_users(tmp_path) -> Path:
575
    other = make_project(
576
        tmp_path / "other",
577
        "org.example.other",
578
        {"profiles": table_with("profiles", "people:create-users")},
579
        links={"people": "vendor/users"},
580
    )
581
    make_project(
582
        other / "vendor" / "users", "org.example.users", {"create-users": table("users"), "audit": table("audit")}
583
    )
584
    return other
585
586
587
def test_status_with_missing_linked_project_keeps_recorded_dependencies(tmp_path):
588
    root = _app_with_users(tmp_path)
589
    other = _other_with_users(tmp_path)
590
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
591
    mitch("up", "-p", str(other), "-d", db_url(tmp_path))
592
    (tmp_path / "users").rename(tmp_path / "users-away")
593
594
    assert mitch("status", "--no-ci", "-p", str(root), "-d", db_url(tmp_path)).exit_code == 0
595
596
    (tmp_path / "users-away").rename(tmp_path / "users")
597
    result = mitch("down", "-p", str(other), "-d", db_url(tmp_path))
598
599
    assert result.exit_code == 0, result.output
600
    assert "users" in tables(tmp_path / "test.db")
601
602
603
def test_redeploy_keeps_linked_migration_that_its_own_project_applied(tmp_path):
604
    root = _app_with_users(tmp_path)
605
    mitch("up", "-p", str(tmp_path / "users"), "-d", db_url(tmp_path))
606
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
607
    (tmp_path / "users" / "create-users.up.sql").write_text("CREATE TABLE users (id int, name text);", encoding="utf-8")
608
    assert mitch("up", "-m", "redeploy", "-p", str(root), "-d", db_url(tmp_path)).exit_code == 0
609
610
    result = mitch("down", "-p", str(root), "-d", db_url(tmp_path))
611
612
    assert result.exit_code == 0, result.output
613
    assert tables(tmp_path / "test.db") == {"users", "audit"}
614
615
616
def test_redeploy_is_refused_while_a_project_that_is_not_loaded_depends_on_it(tmp_path):
617
    root = _app_with_users(tmp_path)
618
    other = make_project(
619
        tmp_path / "other",
620
        "org.example.other",
621
        {"profiles": table_with("profiles", "people:create-users")},
622
        links={"people": "../users"},
623
    )
624
    mitch("up", "-p", str(root), "-d", db_url(tmp_path))
625
    mitch("up", "-p", str(other), "-d", db_url(tmp_path))
626
    (tmp_path / "users" / "create-users.up.sql").write_text("CREATE TABLE users (id int, name text);", encoding="utf-8")
627
628
    result = mitch("up", "-m", "redeploy", "-p", str(root), "-d", db_url(tmp_path))
629
630
    assert result.exit_code == 1
631
    assert "org.example.other:profiles" in result.output
632
    assert tables(tmp_path / "test.db") == {"users", "groups", "teams", "profiles"}
633
634
635
def test_status_all_shows_applied_migrations_that_are_not_required(tmp_path):
636
    root = _app_with_users(tmp_path)
637
    mitch("up", "-p", str(tmp_path / "users"), "-d", db_url(tmp_path))
638
639
    result = mitch("status", "--all", "--no-ci", "-p", str(root), "-d", db_url(tmp_path))
640
641
    assert "- um:audit" in status_lines(result)
642
643
644
def test_copies_with_different_dependencies_for_the_same_migration_conflict(tmp_path):
645
    root = _diamond(
646
        tmp_path,
647
        utils_in_b={"helpers": table_with("helpers", "base"), "base": table("base")},
648
    )
649
650
    result = mitch("up", "-p", str(root), "-d", db_url(tmp_path))
651
652
    assert result.exit_code == 1
653
    assert "differs between copies" in result.output
654
    assert tables(tmp_path / "test.db") == set()
655
656
657
def test_copy_of_the_root_project_inside_a_linked_project_is_not_own(tmp_path):
658
    root = make_project(tmp_path / "p", "org.example.p", {"top": table_with("top", "b:in-b")}, links={"b": "../b"})
659
    b = make_project(tmp_path / "b", "org.example.b", {"in-b": table_with("in_b", "p:extra")}, links={"p": "vendor/p"})
660
    make_project(
661
        b / "vendor" / "p",
662
        "org.example.p",
663
        {"top": table_with("top", "b:in-b"), "extra": table("extra")},
664
        links={"b": "../.."},
665
    )
666
    assert mitch("up", "-p", str(root), "-d", db_url(tmp_path)).exit_code == 0
667
668
    result = mitch("down", "-p", str(root), "-d", db_url(tmp_path))
669
670
    assert result.exit_code == 0, result.output
671
    assert tables(tmp_path / "test.db") == set()