|
| 1 | +"""Ensure message ingestion records keep their message FK |
| 2 | +
|
| 3 | +Phase: EXPAND |
| 4 | +
|
| 5 | +Revision ID: b4c2f8e9a1d3 |
| 6 | +Revises: mb00a1b2c3d4 |
| 7 | +Create Date: 2026-05-09 00:00:00.000000 |
| 8 | +""" |
| 9 | + |
| 10 | +from collections.abc import Sequence |
| 11 | + |
| 12 | +import sqlalchemy as sa |
| 13 | +from alembic import op |
| 14 | +from langflow.utils import migration |
| 15 | + |
| 16 | +# revision identifiers, used by Alembic. |
| 17 | +revision: str = "b4c2f8e9a1d3" # pragma: allowlist secret |
| 18 | +down_revision: str | None = "mb00a1b2c3d4" # pragma: allowlist secret |
| 19 | +branch_labels: str | Sequence[str] | None = None |
| 20 | +depends_on: str | Sequence[str] | None = None |
| 21 | + |
| 22 | +MIR_TABLE = "message_ingestion_record" |
| 23 | +MESSAGE_TABLE = "message" |
| 24 | +MESSAGE_FK_NAME = "fk_message_ingestion_record_message_id_message" |
| 25 | + |
| 26 | + |
| 27 | +def _message_fk_exists(conn) -> bool: |
| 28 | + inspector = sa.inspect(conn) |
| 29 | + for fk in inspector.get_foreign_keys(MIR_TABLE): |
| 30 | + options = fk.get("options") or {} |
| 31 | + ondelete = (options.get("ondelete") or "").upper() |
| 32 | + if ( |
| 33 | + fk.get("constrained_columns") == ["message_id"] |
| 34 | + and fk.get("referred_table") == MESSAGE_TABLE |
| 35 | + and fk.get("referred_columns") == ["id"] |
| 36 | + and ondelete == "CASCADE" |
| 37 | + ): |
| 38 | + return True |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +def _constraint_exists(conn, constraint_name: str) -> bool: |
| 43 | + inspector = sa.inspect(conn) |
| 44 | + return any(fk.get("name") == constraint_name for fk in inspector.get_foreign_keys(MIR_TABLE)) |
| 45 | + |
| 46 | + |
| 47 | +def upgrade() -> None: |
| 48 | + conn = op.get_bind() |
| 49 | + |
| 50 | + # This repairs a PostgreSQL startup race where an older idempotent migration can |
| 51 | + # replay DROP TABLE "message" CASCADE after the memory-base table already exists. |
| 52 | + # That drops only the inbound message_id FK, while the mb00 migration then skips |
| 53 | + # recreating the existing message_ingestion_record table. |
| 54 | + if conn.dialect.name != "postgresql": |
| 55 | + return |
| 56 | + |
| 57 | + if not migration.table_exists(MIR_TABLE, conn) or not migration.table_exists(MESSAGE_TABLE, conn): |
| 58 | + return |
| 59 | + |
| 60 | + if _message_fk_exists(conn): |
| 61 | + return |
| 62 | + |
| 63 | + op.create_foreign_key( |
| 64 | + MESSAGE_FK_NAME, |
| 65 | + MIR_TABLE, |
| 66 | + MESSAGE_TABLE, |
| 67 | + ["message_id"], |
| 68 | + ["id"], |
| 69 | + ondelete="CASCADE", |
| 70 | + postgresql_not_valid=True, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def downgrade() -> None: |
| 75 | + conn = op.get_bind() |
| 76 | + |
| 77 | + if conn.dialect.name != "postgresql" or not migration.table_exists(MIR_TABLE, conn): |
| 78 | + return |
| 79 | + |
| 80 | + if _constraint_exists(conn, MESSAGE_FK_NAME): |
| 81 | + op.drop_constraint(MESSAGE_FK_NAME, MIR_TABLE, type_="foreignkey") |
0 commit comments