Make connect() apply the schema and seed the defaults for a fresh database
This commit is contained in:
1 parent
ceec94c1fc
commit
f7106e8132
1 file changed
+9
-2
+9
-2
@@ -40,14 +40,21 @@ def database_path():
|
|||||||
|
|
||||||
|
|
||||||
def connect(path=None):
|
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()
|
resolved = path or database_path()
|
||||||
if resolved != ":memory:":
|
if resolved != ":memory:":
|
||||||
Path(resolved).expanduser().resolve().parent.mkdir(parents=True, exist_ok=True)
|
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.row_factory = sqlite3.Row
|
||||||
conn.execute("PRAGMA foreign_keys = ON")
|
conn.execute("PRAGMA foreign_keys = ON")
|
||||||
conn.execute("PRAGMA journal_mode = WAL")
|
conn.execute("PRAGMA journal_mode = WAL")
|
||||||
|
apply_schema(conn)
|
||||||
|
seed_defaults(conn)
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user