feat: add Electric SQL replication for chat_comment_mentions

This commit is contained in:
CREDO23 2026-01-19 17:28:44 +02:00
parent 6b9695e848
commit 2839501503
3 changed files with 191 additions and 17 deletions

View file

@ -1,17 +1,16 @@
"""Add Electric SQL replication for chat_comments table
"""Add Electric SQL replication for chat_comment_mentions table
Revision ID: 71
Revises: 70
Enables Electric SQL replication for the chat_comments table to support
real-time live updates for comments in chat threads.
Enables Electric SQL replication for the chat_comment_mentions table to support
real-time live updates for mentions.
"""
from collections.abc import Sequence
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "71"
down_revision: str | None = "70"
branch_labels: str | Sequence[str] | None = None
@ -19,22 +18,19 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
"""Enable Electric SQL replication for chat_comments table."""
# Set REPLICA IDENTITY FULL (required by Electric SQL for replication)
op.execute("ALTER TABLE chat_comments REPLICA IDENTITY FULL;")
"""Enable Electric SQL replication for chat_comment_mentions table."""
op.execute("ALTER TABLE chat_comment_mentions REPLICA IDENTITY FULL;")
# Add chat_comments to Electric SQL publication for replication
op.execute(
"""
DO $$
BEGIN
-- Add chat_comments if not already added
IF NOT EXISTS (
SELECT 1 FROM pg_publication_tables
WHERE pubname = 'electric_publication_default'
AND tablename = 'chat_comments'
AND tablename = 'chat_comment_mentions'
) THEN
ALTER PUBLICATION electric_publication_default ADD TABLE chat_comments;
ALTER PUBLICATION electric_publication_default ADD TABLE chat_comment_mentions;
END IF;
END
$$;
@ -43,8 +39,7 @@ def upgrade() -> None:
def downgrade() -> None:
"""Remove chat_comments from Electric SQL replication."""
# Remove chat_comments from publication
"""Remove chat_comment_mentions from Electric SQL replication."""
op.execute(
"""
DO $$
@ -52,14 +47,13 @@ def downgrade() -> None:
IF EXISTS (
SELECT 1 FROM pg_publication_tables
WHERE pubname = 'electric_publication_default'
AND tablename = 'chat_comments'
AND tablename = 'chat_comment_mentions'
) THEN
ALTER PUBLICATION electric_publication_default DROP TABLE chat_comments;
ALTER PUBLICATION electric_publication_default DROP TABLE chat_comment_mentions;
END IF;
END
$$;
"""
)
# Reset REPLICA IDENTITY to default
op.execute("ALTER TABLE chat_comments REPLICA IDENTITY DEFAULT;")
op.execute("ALTER TABLE chat_comment_mentions REPLICA IDENTITY DEFAULT;")