changeset 4152:23d21daed216

core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
author Goffi <goffi@goffi.org>
date Wed, 22 Nov 2023 14:52:00 +0100 (13 months ago)
parents 18026ce0819c
children 9162d3480b9e
files libervia/backend/memory/migration/versions/610345f77e75_add_version_id_to_history.py libervia/backend/memory/sqla_mapping.py
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/backend/memory/migration/versions/610345f77e75_add_version_id_to_history.py	Wed Nov 22 14:52:00 2023 +0100
@@ -0,0 +1,26 @@
+"""add "version_id" to History
+
+Revision ID: 610345f77e75
+Revises: 2ab01aa1f686
+Create Date: 2023-11-20 17:33:53.544032
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '610345f77e75'
+down_revision = '2ab01aa1f686'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    with op.batch_alter_table('history', schema=None) as batch_op:
+        batch_op.add_column(sa.Column('version_id', sa.Integer(), server_default=sa.text('1'), nullable=False))
+
+
+def downgrade():
+    with op.batch_alter_table('history', schema=None) as batch_op:
+        batch_op.drop_column('version_id')
--- a/libervia/backend/memory/sqla_mapping.py	Wed Nov 22 14:50:35 2023 +0100
+++ b/libervia/backend/memory/sqla_mapping.py	Wed Nov 22 14:52:00 2023 +0100
@@ -35,6 +35,7 @@
     Integer,
     JSON,
     MetaData,
+    text,
     Text,
     UniqueConstraint,
     event,
@@ -267,6 +268,9 @@
     )
 
     uid = Column(Text, primary_key=True)
+    # FIXME: version_id is only needed for changes in `extra` column. It would maybe be
+    # better to use separate table for `extra` data instead.
+    version_id = Column(Integer, nullable=False, server_default=text("1"))
     origin_id = Column(Text)
     stanza_id = Column(Text)
     update_uid = Column(Text)
@@ -300,6 +304,7 @@
     thread = relationship(
         "Thread", uselist=False, back_populates="history", passive_deletes=True
     )
+    __mapper_args__ = {"version_id_col": version_id}
 
     def __init__(self, *args, **kwargs):
         source_jid = kwargs.pop("source_jid", None)