Coverage for src/copytrading_engine/backup/schema_catalog.py: 84%
40 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""The operational SQLite components that backups carry, and their expected catalogs."""
3import re
4import sqlite3
5from pathlib import Path
7from copytrading_engine.control.sqlite import CONTROL_AUDIT_SCHEMA
8from copytrading_engine.execution.adapters.sqlite_ledger import EXECUTION_SCHEMA
9from copytrading_engine.host.installation import APPLICATION_DATABASE_VERSION, INSTALLATION_SCHEMA
10from copytrading_engine.host.self_test.store import SELF_TEST_SCHEMA
11from copytrading_engine.parsing.sqlite import PARSER_CORRECTIONS_SCHEMA, PARSER_SCHEMA
12from copytrading_engine.shared.sqlite import SchemaComponent, ensure_schema
13from copytrading_engine.sources.sqlite import REJECTED_ATTACHMENT_SCHEMA, SOURCE_SCHEMA
14from copytrading_engine.trading.adapters.routing import ROUTING_SCHEMA
16__all__ = [
17 "ACCOUNT_COMPONENTS",
18 "APPLICATION_COMPONENTS",
19 "APPLICATION_DATABASE_VERSION",
20 "create_application_schema",
21 "current_operational_schema_catalog",
22]
24APPLICATION_COMPONENTS: tuple[SchemaComponent, ...] = (
25 INSTALLATION_SCHEMA,
26 SELF_TEST_SCHEMA,
27 PARSER_SCHEMA,
28 PARSER_CORRECTIONS_SCHEMA,
29 SOURCE_SCHEMA,
30 REJECTED_ATTACHMENT_SCHEMA,
31 ROUTING_SCHEMA,
32 CONTROL_AUDIT_SCHEMA,
33)
34ACCOUNT_COMPONENTS: tuple[SchemaComponent, ...] = (EXECUTION_SCHEMA,)
37def create_application_schema(path: Path) -> None:
38 """Give application.db its complete format up front, so an installation is backup-ready
39 before trading ever starts. Each store still verifies its own component when it opens."""
40 connection = sqlite3.connect(path, timeout=5, isolation_level=None)
41 try:
42 connection.execute("BEGIN IMMEDIATE")
43 for component in APPLICATION_COMPONENTS:
44 ensure_schema(connection, component)
45 connection.execute("COMMIT")
46 except BaseException:
47 if connection.in_transaction:
48 connection.execute("ROLLBACK")
49 raise
50 finally:
51 connection.close()
54_ACCOUNT_DATABASE = re.compile(r"^accounts/[A-Za-z0-9_-]{1,64}/execution\.sqlite3$")
57def current_operational_schema_catalog(
58 member_path: str,
59) -> tuple[tuple[str, str, str, str], ...]:
60 """Build a schema catalog from the components used to create operational stores."""
61 if member_path == "application.db":
62 components = APPLICATION_COMPONENTS
63 elif _ACCOUNT_DATABASE.fullmatch(member_path): 63 ↛ 66line 63 didn't jump to line 66 because the condition on line 63 was always true
64 components = ACCOUNT_COMPONENTS
65 else:
66 raise ValueError("unsupported operational SQLite member")
67 connection = sqlite3.connect(":memory:")
68 try:
69 for component in components:
70 ensure_schema(connection, component)
71 objects = connection.execute(
72 "SELECT type,name,tbl_name,sql FROM sqlite_master "
73 "WHERE name NOT LIKE 'sqlite_%' ORDER BY type,name"
74 ).fetchall()
75 return tuple(
76 (kind, name, table_name, statement.strip())
77 for kind, name, table_name, statement in objects
78 if isinstance(statement, str)
79 )
80 finally:
81 connection.close()