changeset 4365:f6672bc80897

storage (mapping): Fix `nullable` for thread ID, and add a `is_retroactive` field: `is_retroactive` is used to link a thread in a parent message when it didn't have the thread initially. rel 457
author Goffi <goffi@goffi.org>
date Tue, 06 May 2025 00:21:24 +0200
parents fa300abc3cb6
children 1ef32316a55e
files libervia/backend/memory/migration/versions/6af2d8f6be76_add_is_retroactive_to_thread_and_fix_.py libervia/backend/memory/sqla_mapping.py
diffstat 2 files changed, 46 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/backend/memory/migration/versions/6af2d8f6be76_add_is_retroactive_to_thread_and_fix_.py	Tue May 06 00:21:24 2025 +0200
@@ -0,0 +1,33 @@
+"""add "is_retroactive" to Thread and fix nullable columns
+
+Revision ID: 6af2d8f6be76
+Revises: 29f85e56b6d4
+Create Date: 2025-05-02 11:21:54.023587
+
+"""
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.sql import false
+
+
+# revision identifiers, used by Alembic.
+revision = '6af2d8f6be76'
+down_revision = '29f85e56b6d4'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    with op.batch_alter_table('thread', schema=None) as batch_op:
+        batch_op.add_column(sa.Column('is_retroactive', sa.Boolean(), server_default=false(), nullable=False, comment="Indicates if thread ID was added after the original message was sent (e.g. to link a parent message which didn't have a thread ID to a thread)."))
+        batch_op.alter_column('thread_id',
+               existing_type=sa.TEXT(),
+               nullable=False)
+
+
+def downgrade():
+    with op.batch_alter_table('thread', schema=None) as batch_op:
+        batch_op.alter_column('thread_id',
+               existing_type=sa.TEXT(),
+               nullable=True)
+        batch_op.drop_column('is_retroactive')
--- a/libervia/backend/memory/sqla_mapping.py	Tue May 06 00:19:43 2025 +0200
+++ b/libervia/backend/memory/sqla_mapping.py	Tue May 06 00:21:24 2025 +0200
@@ -40,6 +40,7 @@
     event,
 )
 from sqlalchemy.orm import declarative_base, relationship
+from sqlalchemy.sql import false
 from sqlalchemy.sql.functions import now
 from sqlalchemy.types import TypeDecorator
 from twisted.words.protocols.jabber import jid
@@ -433,8 +434,18 @@
         primary_key=True,
     )
     history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE"))
-    thread_id = Column(Text)
-    parent_id = Column(Text)
+    thread_id = Column(Text, nullable=False)
+    parent_id = Column(Text, nullable=True)
+    is_retroactive = Column(
+        Boolean,
+        default=False,
+        nullable=False,
+        server_default=false(),
+        comment=(
+            "Indicates if thread ID was added after the original message was sent (e.g. "
+            "to link a parent message which didn't have a thread ID to a thread)."
+        )
+    )
 
     history = relationship("History", uselist=False, back_populates="thread")