- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
38 lines
1.3 KiB
Python
Executable File
38 lines
1.3 KiB
Python
Executable File
import os
|
|
import sqlite3
|
|
|
|
from contextlib import closing
|
|
from pathlib import Path
|
|
from flask import current_app, g
|
|
from backend.app.infrastructure.persistence.schema import SCHEMA
|
|
|
|
|
|
def database_path():
|
|
default = Path(__file__).resolve().parents[4] / "ledger_data" / "ledger.sqlite3"
|
|
return Path(os.environ.get("LEDGER_DB", default)).resolve()
|
|
|
|
def initialize(path):
|
|
path = Path(path)
|
|
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
with closing(sqlite3.connect(path)) as connection:
|
|
connection.execute("PRAGMA auto_vacuum = INCREMENTAL")
|
|
connection.execute("PRAGMA journal_mode = WAL")
|
|
connection.execute("PRAGMA busy_timeout = 5000")
|
|
connection.execute("PRAGMA foreign_keys = ON")
|
|
connection.executescript(SCHEMA)
|
|
connection.execute("PRAGMA user_version = 9")
|
|
|
|
def database():
|
|
if "database" not in g:
|
|
connection = sqlite3.connect(current_app.config["DATABASE"], timeout=5)
|
|
connection.row_factory = sqlite3.Row
|
|
connection.execute("PRAGMA busy_timeout = 5000")
|
|
connection.execute("PRAGMA foreign_keys = ON")
|
|
g.database = connection
|
|
return g.database
|
|
|
|
def close_database(_error):
|
|
connection = g.pop("database", None)
|
|
if connection is not None:
|
|
connection.close()
|