From 540647189b19ddac6510f91655bab81b26a21e98 Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Thu, 16 Jul 2020 09:22:15 -0700 Subject: [PATCH] Add on_duplicate_key_update for mysql testCollections still fails. --- python/lsst/daf/butler/registry/databases/mysql.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python/lsst/daf/butler/registry/databases/mysql.py b/python/lsst/daf/butler/registry/databases/mysql.py index 1a992240d4..74d77eaef5 100644 --- a/python/lsst/daf/butler/registry/databases/mysql.py +++ b/python/lsst/daf/butler/registry/databases/mysql.py @@ -99,9 +99,15 @@ def expandDatabaseEntityName(self, shrunk: str) -> str: return self._shrinker.expand(shrunk) def replace(self, table: sqlalchemy.schema.Table, *rows: dict) -> None: - # This is all wrong if not self.isWriteable(): raise ReadOnlyDatabaseError(f"Attempt to replace into read-only database '{self}'.") if not rows: return - raise NotImplementedError("No support for replace in MySQL") + # This uses special support for UPSERT in MySQL backend: + # https://docs.sqlalchemy.org/en/13/dialects/mysql.html#insert-on-duplicate-key-update-upsert + query = sqlalchemy.dialects.mysql.dml.insert(table) + data = {column.name: getattr(query.inserted, column.name) + for column in table.columns + if hasattr(query.inserted, column.name)} + query = query.on_duplicate_key_update(**data) + self._connection.execute(query, *rows)