|
| 1 | +"""Add first-class actor identity to authorization audit rows. |
| 2 | +
|
| 3 | +Phase: EXPAND |
| 4 | +Revision ID: a6c4e2f8b1d3 |
| 5 | +Revises: d19e7b3c5a42 |
| 6 | +Create Date: 2026-07-22 00:00:00.000000 |
| 7 | +
|
| 8 | +Both columns are nullable for N-1 compatibility and to preserve legacy rows. |
| 9 | +``actor_id`` intentionally has no foreign key: audit attribution must survive |
| 10 | +deletion of an API key or other credential record. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +import sqlalchemy as sa |
| 18 | +import sqlmodel |
| 19 | +from alembic import op |
| 20 | +from langflow.utils import migration |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from collections.abc import Sequence |
| 24 | + |
| 25 | +# revision identifiers, used by Alembic. |
| 26 | +revision: str = "a6c4e2f8b1d3" # pragma: allowlist secret |
| 27 | +down_revision: str | None = "d19e7b3c5a42" # pragma: allowlist secret |
| 28 | +branch_labels: str | Sequence[str] | None = None |
| 29 | +depends_on: str | Sequence[str] | None = None |
| 30 | + |
| 31 | +TABLE_NAME = "authz_audit_log" |
| 32 | +ACTOR_INDEX = "ix_authz_audit_log_actor_timestamp" |
| 33 | +ACTOR_TYPE_INDEX = "ix_authz_audit_log_actor_type_timestamp" |
| 34 | + |
| 35 | + |
| 36 | +def _index_exists(conn, index_name: str) -> bool: |
| 37 | + return index_name in {index["name"] for index in sa.inspect(conn).get_indexes(TABLE_NAME)} |
| 38 | + |
| 39 | + |
| 40 | +def upgrade() -> None: |
| 41 | + conn = op.get_bind() |
| 42 | + if not migration.table_exists(TABLE_NAME, conn): |
| 43 | + return |
| 44 | + |
| 45 | + with op.batch_alter_table(TABLE_NAME, schema=None) as batch_op: |
| 46 | + if not migration.column_exists(TABLE_NAME, "actor_type", conn): |
| 47 | + batch_op.add_column(sa.Column("actor_type", sqlmodel.sql.sqltypes.AutoString(), nullable=True)) |
| 48 | + if not migration.column_exists(TABLE_NAME, "actor_id", conn): |
| 49 | + batch_op.add_column(sa.Column("actor_id", sa.Uuid(), nullable=True)) |
| 50 | + |
| 51 | + # Re-inspect after the batch because SQLite recreates the table. |
| 52 | + if not _index_exists(conn, ACTOR_INDEX): |
| 53 | + op.create_index(ACTOR_INDEX, TABLE_NAME, ["actor_id", "timestamp"], unique=False) |
| 54 | + if not _index_exists(conn, ACTOR_TYPE_INDEX): |
| 55 | + op.create_index(ACTOR_TYPE_INDEX, TABLE_NAME, ["actor_type", "timestamp"], unique=False) |
| 56 | + |
| 57 | + |
| 58 | +def downgrade() -> None: |
| 59 | + conn = op.get_bind() |
| 60 | + if not migration.table_exists(TABLE_NAME, conn): |
| 61 | + return |
| 62 | + |
| 63 | + for index_name in (ACTOR_INDEX, ACTOR_TYPE_INDEX): |
| 64 | + if _index_exists(conn, index_name): |
| 65 | + op.drop_index(index_name, table_name=TABLE_NAME) |
| 66 | + |
| 67 | + with op.batch_alter_table(TABLE_NAME, schema=None) as batch_op: |
| 68 | + if migration.column_exists(TABLE_NAME, "actor_id", conn): |
| 69 | + batch_op.drop_column("actor_id") |
| 70 | + if migration.column_exists(TABLE_NAME, "actor_type", conn): |
| 71 | + batch_op.drop_column("actor_type") |
0 commit comments