122 lines
4.0 KiB
Python
122 lines
4.0 KiB
Python
from typing import Annotated
|
|
|
|
from fastapi import Depends
|
|
from sqlalchemy import inspect, text
|
|
from sqlmodel import SQLModel, Session, create_engine
|
|
|
|
from inse import config
|
|
|
|
settings = config.get_settings()
|
|
|
|
connect_args = {"check_same_thread": False}
|
|
engine = create_engine(
|
|
url=settings.database_url,
|
|
echo=settings.database_debug,
|
|
connect_args=connect_args,
|
|
)
|
|
|
|
|
|
def _migrate_secret_table():
|
|
"""Upgrade the legacy login-only secret table without losing its data."""
|
|
columns = {column["name"] for column in inspect(engine).get_columns("secret")}
|
|
required_columns = {
|
|
"purpose",
|
|
"request_count",
|
|
"request_window_started_at",
|
|
}
|
|
if required_columns.issubset(columns):
|
|
return
|
|
|
|
# SQLite cannot alter a UNIQUE constraint in place. Rebuild the table so an
|
|
# account can hold both a login OTK and a password-reset secret.
|
|
with engine.begin() as connection:
|
|
connection.execute(text("ALTER TABLE secret RENAME TO secret_legacy"))
|
|
connection.execute(
|
|
text(
|
|
"""
|
|
CREATE TABLE secret (
|
|
id CHAR(32) NOT NULL PRIMARY KEY,
|
|
account_id CHAR(32) NOT NULL,
|
|
purpose VARCHAR NOT NULL,
|
|
secret_value VARCHAR NOT NULL,
|
|
expires_at DATETIME NOT NULL,
|
|
request_count INTEGER NOT NULL DEFAULT 0,
|
|
request_window_started_at DATETIME,
|
|
CONSTRAINT uq_secret_account_purpose
|
|
UNIQUE (account_id, purpose),
|
|
FOREIGN KEY(account_id) REFERENCES account (id)
|
|
)
|
|
"""
|
|
)
|
|
)
|
|
connection.execute(
|
|
text(
|
|
"""
|
|
INSERT INTO secret (
|
|
id, account_id, purpose, secret_value, expires_at,
|
|
request_count, request_window_started_at
|
|
)
|
|
SELECT id, account_id, 'LOGIN_OTK', secret_value, expires_at,
|
|
0, NULL
|
|
FROM secret_legacy
|
|
"""
|
|
)
|
|
)
|
|
connection.execute(text("DROP TABLE secret_legacy"))
|
|
connection.execute(
|
|
text("CREATE INDEX ix_secret_account_id ON secret (account_id)")
|
|
)
|
|
connection.execute(text("CREATE INDEX ix_secret_purpose ON secret (purpose)"))
|
|
connection.execute(
|
|
text("CREATE INDEX ix_secret_expires_at ON secret (expires_at)")
|
|
)
|
|
connection.execute(
|
|
text(
|
|
"CREATE INDEX ix_secret_request_window_started_at "
|
|
"ON secret (request_window_started_at)"
|
|
)
|
|
)
|
|
|
|
|
|
def create_db_and_tables():
|
|
"""Creates the database and tables based on the SQLModel metadata."""
|
|
SQLModel.metadata.create_all(engine)
|
|
_migrate_secret_table()
|
|
columns = {column["name"] for column in inspect(engine).get_columns("account")}
|
|
if "activated" not in columns:
|
|
with engine.begin() as connection:
|
|
connection.execute(
|
|
text(
|
|
"ALTER TABLE account ADD COLUMN activated "
|
|
"BOOLEAN NOT NULL DEFAULT TRUE"
|
|
)
|
|
)
|
|
if "roles" not in columns:
|
|
with engine.begin() as connection:
|
|
connection.execute(
|
|
text(
|
|
"ALTER TABLE account ADD COLUMN roles "
|
|
"JSON NOT NULL DEFAULT '[\"USER\"]'"
|
|
)
|
|
)
|
|
if "role" in columns:
|
|
connection.execute(
|
|
text(
|
|
"UPDATE account SET roles = '[\"' || role || '\"]' "
|
|
"WHERE role IS NOT NULL"
|
|
)
|
|
)
|
|
|
|
|
|
def get_session():
|
|
"""Yields a database session for use in FastAPI endpoints.
|
|
|
|
Yields:
|
|
Session: A SQLModel session connected to the database.
|
|
"""
|
|
with Session(engine) as session:
|
|
yield session
|
|
|
|
|
|
SessionDep = Annotated[Session, Depends(get_session)]
|