comparison libervia/backend/memory/sqla_mapping.py @ 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
parents 02f0adc745c6
children 2074b2bbe616
comparison
equal deleted inserted replaced
4151:18026ce0819c 4152:23d21daed216
33 ForeignKey, 33 ForeignKey,
34 Index, 34 Index,
35 Integer, 35 Integer,
36 JSON, 36 JSON,
37 MetaData, 37 MetaData,
38 text,
38 Text, 39 Text,
39 UniqueConstraint, 40 UniqueConstraint,
40 event, 41 event,
41 ) 42 )
42 from sqlalchemy.orm import declarative_base, relationship 43 from sqlalchemy.orm import declarative_base, relationship
265 "history__profile_id_received_timestamp", "profile_id", "received_timestamp" 266 "history__profile_id_received_timestamp", "profile_id", "received_timestamp"
266 ), 267 ),
267 ) 268 )
268 269
269 uid = Column(Text, primary_key=True) 270 uid = Column(Text, primary_key=True)
271 # FIXME: version_id is only needed for changes in `extra` column. It would maybe be
272 # better to use separate table for `extra` data instead.
273 version_id = Column(Integer, nullable=False, server_default=text("1"))
270 origin_id = Column(Text) 274 origin_id = Column(Text)
271 stanza_id = Column(Text) 275 stanza_id = Column(Text)
272 update_uid = Column(Text) 276 update_uid = Column(Text)
273 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE")) 277 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"))
274 source = Column(Text) 278 source = Column(Text)
298 messages = relationship("Message", backref="history", passive_deletes=True) 302 messages = relationship("Message", backref="history", passive_deletes=True)
299 subjects = relationship("Subject", backref="history", passive_deletes=True) 303 subjects = relationship("Subject", backref="history", passive_deletes=True)
300 thread = relationship( 304 thread = relationship(
301 "Thread", uselist=False, back_populates="history", passive_deletes=True 305 "Thread", uselist=False, back_populates="history", passive_deletes=True
302 ) 306 )
307 __mapper_args__ = {"version_id_col": version_id}
303 308
304 def __init__(self, *args, **kwargs): 309 def __init__(self, *args, **kwargs):
305 source_jid = kwargs.pop("source_jid", None) 310 source_jid = kwargs.pop("source_jid", None)
306 if source_jid is not None: 311 if source_jid is not None:
307 kwargs["source"] = source_jid.userhost() 312 kwargs["source"] = source_jid.userhost()