Files
pipeline-lifetime/app/migrations.py
T

23 lines
923 B
Python

"""Small, dependency-free schema migrations for the deployed SQLite database."""
from sqlalchemy import inspect, text
from .extensions import db
def upgrade_schema() -> None:
"""Create new tables and add authentication columns without losing existing data."""
db.create_all()
inspector = inspect(db.engine)
columns = {item["name"] for item in inspector.get_columns("users")}
additions = {
"email": "VARCHAR(254)",
"email_verified_at": "DATETIME",
"is_active_account": "BOOLEAN NOT NULL DEFAULT 0",
"auth_version": "INTEGER NOT NULL DEFAULT 1",
}
for name, sql_type in additions.items():
if name not in columns:
db.session.execute(text(f"ALTER TABLE users ADD COLUMN {name} {sql_type}"))
db.session.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS ix_users_email_unique ON users (email) WHERE email IS NOT NULL"))
db.session.commit()