19 lines
572 B
Python
19 lines
572 B
Python
from __future__ import annotations
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
_MIGRATIONS_DIR = Path(__file__).parent / "migrations"
|
|
|
|
|
|
def get_connection(db_path: str) -> sqlite3.Connection:
|
|
conn = sqlite3.connect(db_path, check_same_thread=False)
|
|
conn.row_factory = sqlite3.Row
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
return conn
|
|
|
|
|
|
def run_migrations(conn: sqlite3.Connection) -> None:
|
|
for migration in sorted(_MIGRATIONS_DIR.glob("*.sql")):
|
|
conn.executescript(migration.read_text())
|
|
conn.commit()
|