from .db import db # format: {integer: str|list} MIGRATIONS = { } def run_migrations(): print("Running migrations...") ran = 0 db.execute(""" CREATE TABLE IF NOT EXISTS _migrations( id INTEGER PRIMARY KEY ) """) completed = [row["id"] for row in db.query("SELECT id FROM _migrations")] for migration_id in sorted(MIGRATIONS.keys()): if migration_id not in completed: print(f"Running migration #{migration_id}") ran += 1 statements = MIGRATIONS[migration_id] # support both strings and lists if isinstance(statements, str): statements = [statements] for sql in statements: db.execute(sql) db.execute("INSERT INTO _migrations (id) VALUES (?)", migration_id) print(f"Ran {ran} migrations.")