Create the health check index only when the indexed column exists

This commit is contained in:
ai-bot committed 2026-10-02 06:26:50 +00:00
1 parent 38d7999c87
commit 98cbde8cbd
1 file changed
+21 -1
+21 -1
View File
@@ -80,7 +80,6 @@ SCHEMA_STATEMENTS = (
UNIQUE (health_check_id, provider) UNIQUE (health_check_id, provider)
) )
""", """,
"CREATE INDEX IF NOT EXISTS idx_health_checks_checked_at ON health_checks(checked_at DESC)",
) )
OPTIONAL_COLUMNS = ( OPTIONAL_COLUMNS = (
("key_checks", "address_verified", "INTEGER CHECK (address_verified IN (0, 1))"), ("key_checks", "address_verified", "INTEGER CHECK (address_verified IN (0, 1))"),
@@ -88,6 +87,13 @@ OPTIONAL_COLUMNS = (
("health_checks", "software_comments", "TEXT"), ("health_checks", "software_comments", "TEXT"),
("key_checks", "seed_backup_verified", "INTEGER CHECK (seed_backup_verified IN (0, 1))"), ("key_checks", "seed_backup_verified", "INTEGER CHECK (seed_backup_verified IN (0, 1))"),
) )
INDEX_STATEMENTS = (
(
"health_checks",
"checked_at",
"CREATE INDEX IF NOT EXISTS idx_health_checks_checked_at ON health_checks(checked_at DESC)",
),
)
def initialize_database(database_path="data/helsesjekk.sqlite3"): def initialize_database(database_path="data/helsesjekk.sqlite3"):
@@ -109,6 +115,8 @@ def initialize_database(database_path="data/helsesjekk.sqlite3"):
connection.execute(statement) connection.execute(statement)
for table, column, definition in OPTIONAL_COLUMNS: for table, column, definition in OPTIONAL_COLUMNS:
_ensure_column(connection, table, column, definition) _ensure_column(connection, table, column, definition)
for table, column, statement in INDEX_STATEMENTS:
_ensure_index(connection, table, column, statement)
connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION}") connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION}")
connection.commit() connection.commit()
except Exception: except Exception:
@@ -127,6 +135,18 @@ def _ensure_column(connection, table, column, definition):
connection.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}") connection.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
def _ensure_index(connection, table, column, statement):
"""Create an index only when its table has the indexed column.
A partially migrated database can contain the table without the column, and
creating the index would then fail. Such a database cannot serve health
checks until the column exists, so the index is skipped instead.
"""
columns = {row[1] for row in connection.execute(f"PRAGMA table_info({table})")}
if column in columns:
connection.execute(statement)
def seed_fixed_keys(database_path="data/helsesjekk.sqlite3"): def seed_fixed_keys(database_path="data/helsesjekk.sqlite3"):
"""Ensure the three fixed key profiles exist without overwriting custom names.""" """Ensure the three fixed key profiles exist without overwriting custom names."""
connection = sqlite3.connect(database_path, timeout=5) connection = sqlite3.connect(database_path, timeout=5)