Skip to content

Commit

Permalink
Make DB.replace delegate to DB.ensure when there are no update columns.
Browse files Browse the repository at this point in the history
This is especially common in tables where the only columns are primary
keys.
  • Loading branch information
TallJimbo committed Oct 23, 2024
1 parent 2f041c1 commit e75146c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/changes/DM-46631.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix inserts with `replace=True` on dimensions with only primary key columns.
3 changes: 3 additions & 0 deletions python/lsst/daf/butler/registry/databases/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ def replace(self, table: sqlalchemy.schema.Table, *rows: dict) -> None:
for column in table.columns
if column.name not in table.primary_key
}
if not data:
self.ensure(table, *rows)
return
query = query.on_conflict_do_update(constraint=table.primary_key, set_=data)
with self._transaction() as (_, connection):
connection.execute(query, rows)
Expand Down
3 changes: 3 additions & 0 deletions python/lsst/daf/butler/registry/databases/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ def replace(self, table: sqlalchemy.schema.Table, *rows: dict) -> None:
for column in table.columns
if column.name not in table.primary_key
}
if not data:
self.ensure(table, *rows)
return
query = query.on_conflict_do_update(index_elements=table.primary_key, set_=data)
with self._transaction() as (_, connection):
connection.execute(query, rows)
Expand Down

0 comments on commit e75146c

Please sign in to comment.