fixes and refactor
This commit is contained in:
31
app/database/indexes.py
Normal file
31
app/database/indexes.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
Database secondary indexes helper.
|
||||
|
||||
Creates small B-tree indexes for common equality filters to speed up searches.
|
||||
Uses CREATE INDEX IF NOT EXISTS so it is safe to call repeatedly at startup
|
||||
and works for existing databases without running a migration.
|
||||
"""
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy import text
|
||||
|
||||
|
||||
def ensure_secondary_indexes(engine: Engine) -> None:
|
||||
statements = [
|
||||
# Files
|
||||
"CREATE INDEX IF NOT EXISTS idx_files_status ON files(status)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_files_file_type ON files(file_type)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_files_empl_num ON files(empl_num)",
|
||||
# Ledger
|
||||
"CREATE INDEX IF NOT EXISTS idx_ledger_t_type ON ledger(t_type)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_ledger_empl_num ON ledger(empl_num)",
|
||||
]
|
||||
with engine.begin() as conn:
|
||||
for stmt in statements:
|
||||
try:
|
||||
conn.execute(text(stmt))
|
||||
except Exception:
|
||||
# Ignore failures (e.g., non-SQLite engines that still support IF NOT EXISTS;
|
||||
# if not supported, users should manage indexes via migrations)
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user