refactor: remove legacy history and proxy migrations (#6518)
This commit is contained in:
1 parent
1c28b20c7e
commit
cb3ba45317
47 files changed
+207
-1925
No files matched your search
@@ -1,6 +1,5 @@
|
||||
"""Session schema upgrades preserve records and fail atomically."""
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
from contextlib import closing
|
||||
from pathlib import Path
|
||||
@@ -38,22 +37,6 @@ def _session() -> Session:
|
||||
)
|
||||
|
||||
|
||||
def _write_legacy(workspace: Path, session: Session) -> Path:
|
||||
directory = workspace / "sessions"
|
||||
directory.mkdir(parents=True)
|
||||
path = directory / "session.jsonl"
|
||||
assert session.provider_state is not None
|
||||
records = [
|
||||
{"_type": "metadata", "key": session.key, "metadata": session.metadata,
|
||||
"created_at": session.created_at.isoformat(), "updated_at": session.updated_at.isoformat(),
|
||||
"last_archived": session.last_archived},
|
||||
{"_type": "provider_state", "state": session.provider_state.to_private_record()},
|
||||
*session.messages,
|
||||
]
|
||||
path.write_text("\n".join(json.dumps(record) for record in records) + "\n", encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def _database_snapshot(path: Path) -> tuple[int, str, str]:
|
||||
with closing(sqlite3.connect(path)) as db:
|
||||
return (
|
||||
@@ -63,10 +46,8 @@ def _database_snapshot(path: Path) -> tuple[int, str, str]:
|
||||
)
|
||||
|
||||
|
||||
def test_ordered_upgrades_preserve_records_and_do_not_repeat_import(tmp_path, monkeypatch):
|
||||
workspace = tmp_path / "workspace"
|
||||
def test_ordered_upgrades_preserve_records(tmp_path, monkeypatch):
|
||||
original = _session()
|
||||
source = _write_legacy(workspace, original)
|
||||
store = SqliteSessionStore(tmp_path)
|
||||
with closing(sqlite3.connect(store.path)) as db:
|
||||
schema = Path(__file__).with_name("fixtures") / "sqlite_v1.sql"
|
||||
@@ -83,9 +64,8 @@ def test_ordered_upgrades_preserve_records_and_do_not_repeat_import(tmp_path, mo
|
||||
monkeypatch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", (
|
||||
*sqlite_store._SCHEMA_MIGRATIONS, add_archived, index_archived,
|
||||
))
|
||||
source.write_text("stale malformed source must not be imported again", encoding="utf-8")
|
||||
upgraded = SqliteSessionStore(tmp_path)
|
||||
upgraded.initialize(workspace)
|
||||
upgraded.initialize()
|
||||
assert _database_snapshot(store.path)[0] == len(sqlite_store._SCHEMA_MIGRATIONS)
|
||||
loaded = upgraded.load(original.key)
|
||||
assert loaded is not None
|
||||
@@ -95,7 +75,7 @@ def test_ordered_upgrades_preserve_records_and_do_not_repeat_import(tmp_path, mo
|
||||
upgraded.save(loaded)
|
||||
|
||||
reopened = SqliteSessionStore(tmp_path)
|
||||
reopened.initialize(workspace)
|
||||
reopened.initialize()
|
||||
roundtrip = reopened.load(original.key)
|
||||
assert roundtrip is not None
|
||||
assert roundtrip.messages == original.messages
|
||||
@@ -104,13 +84,11 @@ def test_ordered_upgrades_preserve_records_and_do_not_repeat_import(tmp_path, mo
|
||||
with closing(sqlite3.connect(store.path)) as db:
|
||||
assert db.execute("SELECT archived FROM sessions").fetchall() == [(0,)]
|
||||
assert db.execute("SELECT name FROM sqlite_master WHERE name='sessions_archived'").fetchone()
|
||||
assert db.execute("SELECT value FROM storage_meta WHERE key='jsonl_import'").fetchone() == ("complete",)
|
||||
|
||||
|
||||
def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp_path, monkeypatch):
|
||||
workspace = tmp_path / "workspace"
|
||||
store = SqliteSessionStore(tmp_path)
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
original = _session()
|
||||
store.save(original)
|
||||
before = _database_snapshot(store.path)
|
||||
@@ -128,7 +106,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp
|
||||
*migrations, add_archived, fail_upgrade,
|
||||
))
|
||||
with pytest.raises(RuntimeError, match="migration interrupted"):
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
assert _database_snapshot(store.path) == before
|
||||
|
||||
def complete_upgrade(db: sqlite3.Connection) -> None:
|
||||
@@ -137,7 +115,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp
|
||||
monkeypatch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", (
|
||||
*migrations, add_archived, complete_upgrade,
|
||||
))
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
assert _database_snapshot(store.path)[0] == len(sqlite_store._SCHEMA_MIGRATIONS)
|
||||
loaded = store.load(original.key)
|
||||
assert loaded is not None
|
||||
@@ -146,7 +124,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp
|
||||
assert loaded.provider_state == original.provider_state
|
||||
|
||||
|
||||
def test_added_columns_allow_import_and_all_session_write_paths(tmp_path, monkeypatch):
|
||||
def test_added_columns_allow_all_session_write_paths(tmp_path, monkeypatch):
|
||||
def add_optional_columns(db: sqlite3.Connection) -> None:
|
||||
for table in ("storage_meta", "sessions", "messages", "checkpoints", "pending_inputs"):
|
||||
db.execute(f"ALTER TABLE {table} ADD COLUMN extension TEXT")
|
||||
@@ -155,16 +133,8 @@ def test_added_columns_allow_import_and_all_session_write_paths(tmp_path, monkey
|
||||
monkeypatch.setattr(sqlite_store, "_SCHEMA_MIGRATIONS", (
|
||||
*sqlite_store._SCHEMA_MIGRATIONS, add_optional_columns,
|
||||
))
|
||||
workspace = tmp_path / "workspace"
|
||||
imported = _session()
|
||||
_write_legacy(workspace, imported)
|
||||
store = SqliteSessionStore(tmp_path)
|
||||
store.initialize(workspace)
|
||||
loaded = store.load(imported.key)
|
||||
assert loaded is not None
|
||||
assert loaded.messages == imported.messages
|
||||
assert loaded.metadata == imported.metadata
|
||||
assert loaded.provider_state == imported.provider_state
|
||||
store.initialize()
|
||||
|
||||
created = _session()
|
||||
created.key = "chat:new"
|
||||
@@ -183,15 +153,14 @@ def test_added_columns_allow_import_and_all_session_write_paths(tmp_path, monkey
|
||||
assert roundtrip.metadata == current.metadata
|
||||
assert roundtrip.provider_state == current.provider_state
|
||||
with closing(sqlite3.connect(store.path)) as db:
|
||||
for table in ("storage_meta", "sessions", "messages", "checkpoints", "pending_inputs"):
|
||||
for table in ("sessions", "messages", "checkpoints", "pending_inputs"):
|
||||
rows = db.execute(f"SELECT extension, format_version FROM {table}").fetchall()
|
||||
assert rows and all(row == (None, 1) for row in rows)
|
||||
|
||||
|
||||
def test_future_schema_is_rejected_without_modifying_database(tmp_path):
|
||||
workspace = tmp_path / "workspace"
|
||||
store = SqliteSessionStore(tmp_path)
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
store.save(_session())
|
||||
future_version = len(sqlite_store._SCHEMA_MIGRATIONS) + 1
|
||||
with closing(sqlite3.connect(store.path)) as db:
|
||||
@@ -202,16 +171,15 @@ def test_future_schema_is_rejected_without_modifying_database(tmp_path):
|
||||
before_files = {path.name for path in tmp_path.iterdir()}
|
||||
|
||||
with pytest.raises(RuntimeError, match=f"unsupported session database schema: {future_version}"):
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
assert _database_snapshot(store.path) == before
|
||||
assert store.path.read_bytes() == before_bytes
|
||||
assert {path.name for path in tmp_path.iterdir()} == before_files
|
||||
|
||||
|
||||
def test_missing_sqlite_json_support_is_rejected_before_database_changes(tmp_path, monkeypatch):
|
||||
workspace = tmp_path / "workspace"
|
||||
store = SqliteSessionStore(tmp_path)
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
store.save(_session())
|
||||
with closing(sqlite3.connect(store.path)) as db:
|
||||
db.execute("PRAGMA journal_mode = DELETE")
|
||||
@@ -231,6 +199,6 @@ def test_missing_sqlite_json_support_is_rejected_before_database_changes(tmp_pat
|
||||
with monkeypatch.context() as patch:
|
||||
patch.setattr(sqlite3, "connect", connect)
|
||||
with pytest.raises(RuntimeError, match="requires SQLite JSON support"):
|
||||
store.initialize(workspace)
|
||||
store.initialize()
|
||||
assert _database_snapshot(store.path) == before
|
||||
assert store.path.read_bytes() == before_bytes
|
||||
Reference in new issue
Block a user