|
10 | 10 | """ |
11 | 11 |
|
12 | 12 | import sqlite3 |
| 13 | +from pathlib import Path |
13 | 14 |
|
14 | 15 | import pytest |
15 | 16 |
|
| 17 | +from agent_session_tools.migrations import ( |
| 18 | + CURRENT_VERSION, |
| 19 | + MIGRATIONS, |
| 20 | + get_user_version, |
| 21 | + migrate, |
| 22 | + set_user_version, |
| 23 | +) |
| 24 | + |
16 | 25 | pytestmark = [] |
17 | 26 |
|
| 27 | +SCHEMA_PATH = ( |
| 28 | + Path(__file__).parent.parent / "src" / "agent_session_tools" / "schema.sql" |
| 29 | +) |
| 30 | + |
18 | 31 | LEGACY_SCHEMA = """ |
19 | 32 | CREATE TABLE IF NOT EXISTS study_sessions (id TEXT PRIMARY KEY, started_at TEXT); |
20 | 33 | CREATE TABLE IF NOT EXISTS sessions ( |
@@ -62,3 +75,107 @@ def test_init_db_on_legacy_db_is_repeatable(legacy_db_path): |
62 | 75 | assert conn.execute("SELECT id FROM sessions").fetchone()[0] == "legacy1" |
63 | 76 | finally: |
64 | 77 | conn.close() |
| 78 | + |
| 79 | + |
| 80 | +def _database_stopped_at(tmp_path: Path, version: int) -> Path: |
| 81 | + """A sessions.db exactly as a fresh install left it at schema ``version``. |
| 82 | +
|
| 83 | + schema.sql plus migrations 1..version, with ``user_version`` recorded -- |
| 84 | + the shape of a database last touched by an older release. |
| 85 | + """ |
| 86 | + db = tmp_path / f"sessions-v{version}.db" |
| 87 | + conn = sqlite3.connect(db) |
| 88 | + try: |
| 89 | + conn.executescript(SCHEMA_PATH.read_text()) |
| 90 | + for v in range(1, version + 1): |
| 91 | + MIGRATIONS[v][1](conn) |
| 92 | + set_user_version(conn, v) |
| 93 | + conn.execute( |
| 94 | + "INSERT INTO sessions(id,source,created_at) VALUES ('kept','kiro','2024-01-01')" |
| 95 | + ) |
| 96 | + conn.commit() |
| 97 | + finally: |
| 98 | + conn.close() |
| 99 | + return db |
| 100 | + |
| 101 | + |
| 102 | +def _widen_to_final_shape(conn: sqlite3.Connection) -> None: |
| 103 | + """What ``_reconcile_legacy_base_tables`` did before the fix. |
| 104 | +
|
| 105 | + It diffed every live table against the *final* schema and ADDed each |
| 106 | + missing column -- including columns a not-yet-applied migration was |
| 107 | + about to add itself. Those ALTERs auto-committed before ``migrate()`` |
| 108 | + ran and then survived its rollback, so a database it touched once was |
| 109 | + left with the columns present and ``user_version`` unchanged. |
| 110 | + """ |
| 111 | + pristine = sqlite3.connect(":memory:") |
| 112 | + try: |
| 113 | + pristine.executescript(SCHEMA_PATH.read_text()) |
| 114 | + migrate(pristine) |
| 115 | + tables = [ |
| 116 | + row[0] |
| 117 | + for row in pristine.execute( |
| 118 | + "SELECT name FROM sqlite_master WHERE type='table'" |
| 119 | + ) |
| 120 | + ] |
| 121 | + for table in tables: |
| 122 | + live = {row[1] for row in conn.execute(f'PRAGMA table_info("{table}")')} |
| 123 | + if not live: |
| 124 | + continue |
| 125 | + for _, name, ctype, _, default, pk in pristine.execute( |
| 126 | + f'PRAGMA table_info("{table}")' |
| 127 | + ).fetchall(): |
| 128 | + if name in live or pk: |
| 129 | + continue |
| 130 | + ddl = f'ALTER TABLE "{table}" ADD COLUMN "{name}" {ctype}' |
| 131 | + if default is not None and "(" not in str(default): |
| 132 | + ddl += f" DEFAULT {default}" |
| 133 | + conn.execute(ddl) |
| 134 | + conn.commit() |
| 135 | + finally: |
| 136 | + pristine.close() |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.parametrize("version", range(CURRENT_VERSION)) |
| 140 | +def test_init_db_converges_a_database_stopped_at_any_version(tmp_path, version): |
| 141 | + """Every release's schema must migrate forward through ``init_db``. |
| 142 | +
|
| 143 | + The legacy reconciliation runs *before* the migrations. If it adds a |
| 144 | + column a pending migration adds unguarded, that migration fails with |
| 145 | + ``duplicate column name`` and the whole sequence rolls back -- which is |
| 146 | + how a v30 database (practice_attempts from v24, ``topic`` added by v37) |
| 147 | + could never reach v48. |
| 148 | + """ |
| 149 | + from agent_session_tools.export_sessions import init_db |
| 150 | + |
| 151 | + conn = init_db(str(_database_stopped_at(tmp_path, version))) |
| 152 | + try: |
| 153 | + assert get_user_version(conn) == CURRENT_VERSION |
| 154 | + assert conn.execute("SELECT id FROM sessions").fetchone()[0] == "kept" |
| 155 | + finally: |
| 156 | + conn.close() |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.parametrize("version", range(CURRENT_VERSION)) |
| 160 | +def test_migrate_recovers_a_database_the_old_reconciliation_already_widened( |
| 161 | + tmp_path, version |
| 162 | +): |
| 163 | + """Databases the old reconciliation touched are stuck until migrations tolerate it. |
| 164 | +
|
| 165 | + Such a database already carries every final-shape column while its |
| 166 | + ``user_version`` still says ``version``. ``session-maint embed`` calls |
| 167 | + ``migrate()`` directly on it (no reconciliation), so the pending |
| 168 | + migrations themselves must skip a column that is already there. |
| 169 | + """ |
| 170 | + db = _database_stopped_at(tmp_path, version) |
| 171 | + conn = sqlite3.connect(db) |
| 172 | + try: |
| 173 | + _widen_to_final_shape(conn) |
| 174 | + assert get_user_version(conn) == version |
| 175 | + |
| 176 | + migrate(conn) |
| 177 | + |
| 178 | + assert get_user_version(conn) == CURRENT_VERSION |
| 179 | + assert conn.execute("SELECT id FROM sessions").fetchone()[0] == "kept" |
| 180 | + finally: |
| 181 | + conn.close() |
0 commit comments