|
| 1 | +"""Add war_room_topic table + topic_id column on war_room_chat_message. |
| 2 | +
|
| 3 | +Topics are top-level partitions of the chat stream — each war room |
| 4 | +has one non-archivable "Main" topic (created lazily by the business |
| 5 | +layer on first read) plus any number of operator-created ones. A |
| 6 | +message with `topic_id IS NULL` is on Main; the read path treats |
| 7 | +NULL and the Main topic row as equivalent so pre-migration rows keep |
| 8 | +working without a backfill. |
| 9 | +
|
| 10 | +Archive is soft (`archived_at`). No hard delete — archived topics |
| 11 | +still render (read-only) in the sidebar. |
| 12 | +
|
| 13 | +Idempotent via `_has_table` / `_table_has_column` so re-running on |
| 14 | +environments that already applied the migration is a no-op. |
| 15 | +
|
| 16 | +Revision ID: c3d4e5f6a9b0 |
| 17 | +Revises: f3c8d2a1b47e |
| 18 | +Create Date: 2026-07-09 10:00:00.000000 |
| 19 | +""" |
| 20 | +import sqlalchemy as sa |
| 21 | +from alembic import op |
| 22 | + |
| 23 | +from app.alembic.alembic_utils import _has_table, _table_has_column |
| 24 | + |
| 25 | + |
| 26 | +revision = 'c3d4e5f6a9b0' |
| 27 | +down_revision = 'f3c8d2a1b47e' |
| 28 | +branch_labels = None |
| 29 | +depends_on = None |
| 30 | + |
| 31 | + |
| 32 | +def upgrade(): |
| 33 | + if not _has_table('war_room_topic'): |
| 34 | + op.create_table( |
| 35 | + 'war_room_topic', |
| 36 | + sa.Column('topic_id', sa.BigInteger(), primary_key=True), |
| 37 | + sa.Column( |
| 38 | + 'war_room_id', sa.BigInteger(), |
| 39 | + sa.ForeignKey('war_room.war_room_id', ondelete='CASCADE'), |
| 40 | + nullable=False, |
| 41 | + ), |
| 42 | + sa.Column('name', sa.String(length=80), nullable=False), |
| 43 | + sa.Column( |
| 44 | + 'is_main', sa.Boolean(), nullable=False, |
| 45 | + server_default=sa.text('false'), |
| 46 | + ), |
| 47 | + sa.Column( |
| 48 | + 'created_by_id', sa.BigInteger(), |
| 49 | + sa.ForeignKey('user.id'), nullable=True, |
| 50 | + ), |
| 51 | + sa.Column( |
| 52 | + 'created_at', sa.DateTime(), nullable=False, |
| 53 | + server_default=sa.text('now()'), |
| 54 | + ), |
| 55 | + sa.Column('archived_at', sa.DateTime(), nullable=True), |
| 56 | + sa.UniqueConstraint('war_room_id', 'name', |
| 57 | + name='uq_war_room_topic_name'), |
| 58 | + ) |
| 59 | + op.create_index( |
| 60 | + 'ix_war_room_topic_war_room_id', |
| 61 | + 'war_room_topic', |
| 62 | + ['war_room_id'], |
| 63 | + ) |
| 64 | + |
| 65 | + if _has_table('war_room_chat_message'): |
| 66 | + if not _table_has_column('war_room_chat_message', 'topic_id'): |
| 67 | + op.add_column( |
| 68 | + 'war_room_chat_message', |
| 69 | + sa.Column( |
| 70 | + 'topic_id', sa.BigInteger(), |
| 71 | + sa.ForeignKey( |
| 72 | + 'war_room_topic.topic_id', ondelete='SET NULL' |
| 73 | + ), |
| 74 | + nullable=True, |
| 75 | + ), |
| 76 | + ) |
| 77 | + op.create_index( |
| 78 | + 'ix_war_room_chat_message_topic_id', |
| 79 | + 'war_room_chat_message', |
| 80 | + ['topic_id'], |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def downgrade(): |
| 85 | + if _has_table('war_room_chat_message'): |
| 86 | + if _table_has_column('war_room_chat_message', 'topic_id'): |
| 87 | + op.drop_index( |
| 88 | + 'ix_war_room_chat_message_topic_id', |
| 89 | + table_name='war_room_chat_message', |
| 90 | + ) |
| 91 | + op.drop_column('war_room_chat_message', 'topic_id') |
| 92 | + |
| 93 | + if _has_table('war_room_topic'): |
| 94 | + op.drop_index( |
| 95 | + 'ix_war_room_topic_war_room_id', |
| 96 | + table_name='war_room_topic', |
| 97 | + ) |
| 98 | + op.drop_table('war_room_topic') |
0 commit comments