app: add SQLite schema and db helper (WAL, busy_timeout, four tables + indexes)
This commit is contained in:
29
app/db.py
Normal file
29
app/db.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""SQLite connection + schema init for Bike-Tinder.
|
||||
|
||||
Stdlib only. The sqlite3 CLI is not installed on this host, so everything
|
||||
goes through the sqlite3 module.
|
||||
"""
|
||||
import os
|
||||
import sqlite3
|
||||
|
||||
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema.sql')
|
||||
|
||||
|
||||
def connect(db_path):
|
||||
"""Open a connection with WAL mode and a busy_timeout, then ensure schema."""
|
||||
parent = os.path.dirname(os.path.abspath(db_path))
|
||||
if parent:
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute('PRAGMA journal_mode=WAL')
|
||||
conn.execute('PRAGMA busy_timeout=5000')
|
||||
conn.execute('PRAGMA foreign_keys=ON')
|
||||
return conn
|
||||
|
||||
|
||||
def init_db(conn):
|
||||
"""Apply schema.sql (idempotent)."""
|
||||
with open(SCHEMA_PATH, encoding='utf-8') as fh:
|
||||
conn.executescript(fh.read())
|
||||
conn.commit()
|
||||
Reference in New Issue
Block a user