# HG changeset patch # User Goffi # Date 1700661120 -3600 # Node ID 23d21daed21611ac9705b399f4528cb59b395ecb # Parent 18026ce0819cf8bf52bac50ff090c96ebf4dfe7d core (memory/sqla_mapping): add a `version_id` column to detect race conditions. diff -r 18026ce0819c -r 23d21daed216 libervia/backend/memory/migration/versions/610345f77e75_add_version_id_to_history.py --- /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') diff -r 18026ce0819c -r 23d21daed216 libervia/backend/memory/sqla_mapping.py --- 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)