diff --git a/database.py b/database.py index 13fd964..1c9e4a8 100644 --- a/database.py +++ b/database.py @@ -40,14 +40,21 @@ def database_path(): def connect(path=None): - """Open a connection with foreign keys on and WAL journaling.""" + """Open a connection with foreign keys on and WAL journaling. + + A fresh database is made ready here: the schema is applied and the default + settings and rates are seeded. Both steps are idempotent, so a caller never + has to know whether the file was empty. + """ resolved = path or database_path() if resolved != ":memory:": Path(resolved).expanduser().resolve().parent.mkdir(parents=True, exist_ok=True) - conn = sqlite3.connect(resolved) + conn = sqlite3.connect(resolved, timeout=30) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON") conn.execute("PRAGMA journal_mode = WAL") + apply_schema(conn) + seed_defaults(conn) return conn