QoL and metric value inverter
This commit is contained in:
@@ -44,12 +44,13 @@ CREATE TABLE IF NOT EXISTS metrics (
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS domain_metric_weights (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
domain_id INTEGER NOT NULL REFERENCES domains(id),
|
||||
metric_id INTEGER NOT NULL REFERENCES metrics(id),
|
||||
weight REAL NOT NULL,
|
||||
norm_min REAL,
|
||||
norm_max REAL,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
domain_id INTEGER NOT NULL REFERENCES domains(id),
|
||||
metric_id INTEGER NOT NULL REFERENCES metrics(id),
|
||||
weight REAL NOT NULL,
|
||||
norm_min REAL,
|
||||
norm_max REAL,
|
||||
lower_is_better INTEGER NOT NULL DEFAULT 0,
|
||||
UNIQUE(domain_id, metric_id)
|
||||
);
|
||||
|
||||
@@ -117,6 +118,23 @@ CREATE INDEX IF NOT EXISTS idx_pipeline_runs_domain ON pipeline_runs(domain_id);
|
||||
"""
|
||||
|
||||
|
||||
def _migrate(conn: sqlite3.Connection) -> None:
|
||||
"""Apply incremental migrations for existing databases."""
|
||||
cols = {r[1] for r in conn.execute("PRAGMA table_info(domain_metric_weights)").fetchall()}
|
||||
if "lower_is_better" not in cols:
|
||||
conn.execute(
|
||||
"ALTER TABLE domain_metric_weights ADD COLUMN lower_is_better INTEGER NOT NULL DEFAULT 0"
|
||||
)
|
||||
|
||||
# Backfill: cost_efficiency is lower-is-better in all domains
|
||||
conn.execute(
|
||||
"""UPDATE domain_metric_weights SET lower_is_better = 1
|
||||
WHERE lower_is_better = 0
|
||||
AND metric_id IN (SELECT id FROM metrics WHERE name = 'cost_efficiency')"""
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def init_db(db_path: str | Path) -> sqlite3.Connection:
|
||||
"""Create/open the database and ensure all tables exist."""
|
||||
db_path = Path(db_path)
|
||||
@@ -125,5 +143,6 @@ def init_db(db_path: str | Path) -> sqlite3.Connection:
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
conn.executescript(DDL)
|
||||
_migrate(conn)
|
||||
conn.commit()
|
||||
return conn
|
||||
|
||||
Reference in New Issue
Block a user