Skip to content

better upsert of adaptor properties #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions cads_broker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sqlalchemy.orm.exc
import sqlalchemy_utils
import structlog
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import JSONB, insert
from typing_extensions import Iterable

import alembic.command
Expand Down Expand Up @@ -800,19 +800,15 @@ def ensure_adaptor_properties(
session: sa.orm.Session,
) -> None:
"""Create adaptor properties (if not exists) or update its timestamp."""
try:
adaptor_properties = AdaptorProperties(hash=hash, config=config, form=form)
session.add(adaptor_properties)
session.commit()
except sa.exc.IntegrityError: # hash already present
session.rollback()
statement = (
AdaptorProperties.__table__.update()
.where(AdaptorProperties.__table__.c.hash == hash)
.values(timestamp=datetime.datetime.now())
)
session.execute(statement)
session.commit()
insert_stmt = insert(AdaptorProperties.__table__).values(
hash=hash, config=config, form=form
)
do_update_stmt = insert_stmt.on_conflict_do_update(
constraint="adaptor_properties_pkey",
set_=dict(timestamp=datetime.datetime.now()),
)
session.execute(do_update_stmt)
session.commit()


def add_event(
Expand Down
Loading