Skip to content

Commit f15f08b

Browse files
authored
bugfix: ensure_adaptor_properties without rare integrity errors (#111)
1 parent ad7b2bc commit f15f08b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

cads_broker/database.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,16 +734,18 @@ def ensure_adaptor_properties(
734734
) -> None:
735735
"""Create adaptor properties (if not exists) or update its timestamp."""
736736
try:
737+
adaptor_properties = AdaptorProperties(hash=hash, config=config, form=form)
738+
session.add(adaptor_properties)
739+
session.commit()
740+
except sa.exc.IntegrityError: # hash already present
741+
session.rollback()
737742
statement = (
738743
AdaptorProperties.__table__.update()
739-
.returning(AdaptorProperties.hash)
740744
.where(AdaptorProperties.__table__.c.hash == hash)
741745
.values(timestamp=datetime.datetime.now())
742746
)
743-
session.execute(statement).one()
744-
except sqlalchemy.orm.exc.NoResultFound:
745-
adaptor_properties = AdaptorProperties(hash=hash, config=config, form=form)
746-
session.add(adaptor_properties)
747+
session.execute(statement)
748+
session.commit()
747749

748750

749751
def add_event(

0 commit comments

Comments
 (0)