From 0a60ab7ed8b99bbc826b7e7493df425e9370a193 Mon Sep 17 00:00:00 2001 From: Arina Stebenkova Date: Fri, 4 Jul 2025 19:20:56 +0300 Subject: [PATCH] [grafana] Adjust scripts for SQLAlchemy 2.0 compatibility * wrap statements with `text()` where appropriate * add commit handling for explicit transactions * replace deprecated .execute() usage with SQLAlchemy 2.0 syntax Change-Id: If528ed9240ede4478aba53249f0bfe2be41d8769 Signed-off-by: Arina Stebenkova --- grafana/templates/bin/_db-session-sync.py.tpl | 23 +++++++++++-------- .../notes/grafana-b3fac6a311d115a6.yaml | 4 ++++ 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 releasenotes/notes/grafana-b3fac6a311d115a6.yaml diff --git a/grafana/templates/bin/_db-session-sync.py.tpl b/grafana/templates/bin/_db-session-sync.py.tpl index b145fdfcd3..1a2352e47c 100644 --- a/grafana/templates/bin/_db-session-sync.py.tpl +++ b/grafana/templates/bin/_db-session-sync.py.tpl @@ -11,7 +11,7 @@ import os import sys import logging -from sqlalchemy import create_engine +from sqlalchemy import create_engine, text # Create logger, console handler and formatter logger = logging.getLogger('OpenStack-Helm DB Init') @@ -58,13 +58,18 @@ except: # Create Table try: - user_engine.execute('''CREATE TABLE IF NOT EXISTS `session` ( - `key`CHAR(16) NOT NULL, - `data` BLOB, - `expiry` INT(11) UNSIGNED NOT NULL, - PRIMARY KEY (`key`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;''') + with user_engine.connect() as conn: + conn.execute(text('''CREATE TABLE IF NOT EXISTS `session` ( + `key` CHAR(16) NOT NULL, + `data` BLOB, + `expiry` INT(11) UNSIGNED NOT NULL, + PRIMARY KEY (`key`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;''')) + try: + conn.commit() + except AttributeError: + pass logger.info('Created table for session cache') -except: - logger.critical('Could not create table for session cache') +except Exception as e: + logger.critical(f'Could not create table for session cache: {e}') raise diff --git a/releasenotes/notes/grafana-b3fac6a311d115a6.yaml b/releasenotes/notes/grafana-b3fac6a311d115a6.yaml new file mode 100644 index 0000000000..9caf07bba2 --- /dev/null +++ b/releasenotes/notes/grafana-b3fac6a311d115a6.yaml @@ -0,0 +1,4 @@ +--- +grafana: + - Adjust Python code for SQLAlchemy 2.0 compatibility +...