31 lines
894 B
Python
31 lines
894 B
Python
|
|
"""Database utilities — SQLite for local/dev, prepared for Postgres."""
|
||
|
|
|
||
|
|
import sqlite3
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
DB_PATH = Path(__file__).resolve().parent.parent / "data" / "app.db"
|
||
|
|
|
||
|
|
|
||
|
|
def get_connection() -> sqlite3.Connection:
|
||
|
|
"""Return a new SQLite connection with WAL mode and foreign keys enabled."""
|
||
|
|
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
conn = sqlite3.connect(str(DB_PATH))
|
||
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
||
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
||
|
|
conn.row_factory = sqlite3.Row
|
||
|
|
return conn
|
||
|
|
|
||
|
|
|
||
|
|
def init_db():
|
||
|
|
"""Initialize database schema."""
|
||
|
|
conn = get_connection()
|
||
|
|
conn.executescript("""
|
||
|
|
CREATE TABLE IF NOT EXISTS items (
|
||
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
|
name TEXT NOT NULL,
|
||
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||
|
|
);
|
||
|
|
""")
|
||
|
|
conn.commit()
|
||
|
|
conn.close()
|