def migrate(connection: sqlite3.Connection) -> None:
connection.execute(
"CREATE TABLE IF NOT EXISTS schema_migrations "
"(version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL)"
)
applied = connection.execute(
"SELECT 1 FROM schema_migrations WHERE version = ?", (SCHEMA_VERSION,)
).fetchone()
if applied is None:
schema = Path(__file__).with_name("schema.sql").read_text()
connection.executescript(schema)
connection.execute(
"INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
(SCHEMA_VERSION, datetime.now(UTC).isoformat()),
)
connection.commit()