add settings table to database

This commit is contained in:
matt committed 2026-10-16 03:40:21 -07:00
1 parent 602d7d4aa4
commit 63734c87bc
3 files changed
+155

No files matched your search

+48
View File
@@ -68,6 +68,15 @@ def initialize_db() -> None:
);
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS settings (
name TEXT PRIMARY KEY,
value TEXT
);
"""
)
conn.commit()
# ---------------------------------------------------------------------------
@@ -276,6 +285,45 @@ def update_visit(id: int, user_ids: Optional[List[int]] = None, provider_ids: Op
conn.commit()
return {"status": "ok", "id": id}
# --- Settings helper functions ---
def add_setting(name: str, value: str) -> Dict[str, Any]:
"""Insert a new setting. If a setting with the same name exists, it will be replaced.
Returns a status dictionary.
"""
with _get_connection() as conn:
cursor = conn.execute(
"INSERT OR REPLACE INTO settings (name, value) VALUES (?, ?)",
(name, value),
)
conn.commit()
return {"status": "ok", "name": name}
def change_setting(name: str, value: str) -> Dict[str, Any]:
"""Update the value of an existing setting. If the setting does not exist, return not-found.
"""
with _get_connection() as conn:
cursor = conn.execute(
"UPDATE settings SET value = ? WHERE name = ?",
(value, name),
)
conn.commit()
if cursor.rowcount == 0:
return {"status": "not-found", "name": name}
return {"status": "ok", "name": name}
def view_settings(names: List[str] | None = None) -> List[Dict[str, Any]]:
"""Return a list of settings.
If names is provided, only those names are returned.
"""
if not names:
return _select("settings")
placeholders = ", ".join(["?" for _ in names])
rows = _select("settings", f"name IN ({placeholders})", tuple(names))
return rows
# Example usage (to be removed or guarded by ``if __name__ == '__main__'``)
if __name__ == "__main__":
initialize_db()