From f834f9adaf9c228ff4ec6a5e24e6d4cf3ca6a992 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Mon, 6 Mar 2023 18:47:03 +0000 Subject: [PATCH] Fix NotImplementedError in dbcounter on SQLA 2.x This patch fixes a NotImplementedError raised in the dbcounter plugin when using SQLAlchemy 2.x. The plugin signature has changed and now requires an "update_url" method as part of the plugin[1]. This patch also updates the do_incr() explicit SQL string to use a TextClause and the new requirement for named bound parameters[2]. Closes-Bug: #2009521 [1] https://docs.sqlalchemy.org/en/20/changelog/migration_14.html#changes-to-createengineplugin [2] https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#execute-method-more-strict-execution-options-are-more-prominent Change-Id: Ie5484597057a3306757cc46b657446ad61ac2098 --- ...ementedError-on-SQLAlchemy-2-21bb6dcdf3ce4225.yaml | 5 +++++ tools/dbcounter/dbcounter.py | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/Fix-dbcounter-NotImplementedError-on-SQLAlchemy-2-21bb6dcdf3ce4225.yaml diff --git a/releasenotes/notes/Fix-dbcounter-NotImplementedError-on-SQLAlchemy-2-21bb6dcdf3ce4225.yaml b/releasenotes/notes/Fix-dbcounter-NotImplementedError-on-SQLAlchemy-2-21bb6dcdf3ce4225.yaml new file mode 100644 index 0000000000..f815e14ccb --- /dev/null +++ b/releasenotes/notes/Fix-dbcounter-NotImplementedError-on-SQLAlchemy-2-21bb6dcdf3ce4225.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixes a NotImplementedError when using the dbcounter SQLAlchemy plugin on + SQLAlchemy 2.x. diff --git a/tools/dbcounter/dbcounter.py b/tools/dbcounter/dbcounter.py index 5057f0f393..0ed7bb813a 100644 --- a/tools/dbcounter/dbcounter.py +++ b/tools/dbcounter/dbcounter.py @@ -40,6 +40,9 @@ class LogCursorEventsPlugin(CreateEnginePlugin): self.queue = queue.Queue() self.thread = None + def update_url(self, url): + return url.difference_update_query(["dbcounter"]) + def engine_created(self, engine): """Hook the engine creation process. @@ -77,12 +80,12 @@ class LogCursorEventsPlugin(CreateEnginePlugin): def do_incr(self, db, op, count): """Increment the counter for (db,op) by count.""" - query = ('INSERT INTO queries (db, op, count) ' - ' VALUES (%s, %s, %s) ' - ' ON DUPLICATE KEY UPDATE count=count+%s') + query = sqlalchemy.text('INSERT INTO queries (db, op, count) ' + ' VALUES (:db, :op, :count) ' + ' ON DUPLICATE KEY UPDATE count=count+:count') try: with self.engine.begin() as conn: - r = conn.execute(query, (db, op, count, count)) + r = conn.execute(query, {'db': db, 'op': op, 'count': count}) except Exception as e: LOG.error('Failed to account for access to database %r: %s', db, e)