From 209fe50e025e62a5bf26ff8fb50a9de1b5ffa7e4 Mon Sep 17 00:00:00 2001 From: Nick Pilon Date: Fri, 5 Jan 2024 11:04:56 -0400 Subject: [PATCH 1/6] Use SQLAlchemy 2.x execution API --- src/pyramid_debugtoolbar/panels/sqla.py | 32 +++++++++++++------------ 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/pyramid_debugtoolbar/panels/sqla.py b/src/pyramid_debugtoolbar/panels/sqla.py index 85e249e..421726c 100644 --- a/src/pyramid_debugtoolbar/panels/sqla.py +++ b/src/pyramid_debugtoolbar/panels/sqla.py @@ -229,14 +229,15 @@ def sql_select(self): engines = self.request.registry.parent_registry.pdtb_sqla_engines engine = engines[int(engine_id)]() - result = engine.execute(stmt, params) + with engine.connect() as connection: + result = connection.exec_driver_sql(stmt, params) - return { - 'result': result.fetchall(), - 'headers': result.keys(), - 'sql': format_sql(stmt), - 'duration': float(query_dict['duration']), - } + return { + 'result': result.fetchall(), + 'headers': result.keys(), + 'sql': format_sql(stmt), + 'duration': float(query_dict['duration']), + } @view_config( route_name='debugtoolbar.sql_explain', @@ -261,15 +262,16 @@ def sql_explain(self): else: query = 'EXPLAIN %s' % stmt - result = engine.execute(query, params) + with engine.connect() as connection: + result = connection.exec_driver_sql(query, params) - return { - 'result': result.fetchall(), - 'headers': result.keys(), - 'sql': format_sql(stmt), - 'str': str, - 'duration': float(query_dict['duration']), - } + return { + 'result': result.fetchall(), + 'headers': result.keys(), + 'sql': format_sql(stmt), + 'str': str, + 'duration': float(query_dict['duration']), + } def includeme(config): From 81b2429e7a6bd1b8c4cf4b9d5e22e925d6065b5d Mon Sep 17 00:00:00 2001 From: Nick Pilon Date: Fri, 5 Jan 2024 15:10:58 -0400 Subject: [PATCH 2/6] Nick P - sign CONTRIBUTORS.txt per contributing.md --- CONTRIBUTORS.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 279f065..5e05869 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -125,3 +125,4 @@ Contributors - Marcin Lulek, 2018-02-19 - Wim De Clercq, 2018-11-23 - Holger Peters, 2020-02-06 +- Nicholas Pilon, 2024-01-05 From 8f871cd3b5467a41d615016eb6b19f5f6e963c0f Mon Sep 17 00:00:00 2001 From: Nick Pilon Date: Mon, 8 Jan 2024 12:00:38 -0400 Subject: [PATCH 3/6] Add specific sqlalchemy test envs --- .github/workflows/ci-tests.yml | 13 +++++++++++++ tox.ini | 2 ++ 2 files changed, 15 insertions(+) diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index ffabdd3..9973ad3 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -77,6 +77,19 @@ jobs: - run: pip install tox - name: Running tox run: tox -e py37-pyramid19 + test-sqlalchemy14: + runs-on: ubuntu-latest + name: "Python: 3.11-x64 on ubuntu-latest with SQLAlchemy 1.4" + steps: + - uses: actions/checkout@v4 + - name: Setup python + uses: actions/setup-python@v4 + with: + python-version: 3.11 + architecture: x64 + - run: pip install tox + - name: Running tox + run: tox -e py311-sqlalchemy14 coverage: runs-on: ubuntu-latest name: Validate coverage diff --git a/tox.ini b/tox.ini index becb65a..932c1b3 100644 --- a/tox.ini +++ b/tox.ini @@ -16,6 +16,8 @@ deps = pyramid18: pyramid <= 1.8.99 pyramid19: pyramid <= 1.9.99 pyramid110: pyramid <= 1.10.99 + sqlalchemy14: sqlalchemy <= 1.4 + sqlalchemy20: sqlalchemy >= 2.0, <2.1 commands = py.test --cov --cov-report= {posargs:} From 0cf5c7c7bea3d94a9045bf1d605fd48b601bd7b9 Mon Sep 17 00:00:00 2001 From: Nick Pilon Date: Mon, 8 Jan 2024 12:03:59 -0400 Subject: [PATCH 4/6] Help linting pass --- tests/test_panels/_utils.py | 1 - tests/test_panels/test_request_vars.py | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/test_panels/_utils.py b/tests/test_panels/_utils.py index 2d8c523..368a0ea 100644 --- a/tests/test_panels/_utils.py +++ b/tests/test_panels/_utils.py @@ -18,7 +18,6 @@ def ok_response_factory(): class _TestDebugtoolbarPanel(unittest.TestCase): - re_toolbar_link = re_toolbar_link def setUp(self): diff --git a/tests/test_panels/test_request_vars.py b/tests/test_panels/test_request_vars.py index 38d5c34..c97c74d 100644 --- a/tests/test_panels/test_request_vars.py +++ b/tests/test_panels/test_request_vars.py @@ -35,7 +35,6 @@ def templated_escaped(input, expect_saferepr=None): class _TestPanel_RequestVars(_TestDebugtoolbarPanel): def _makeOne(self, query_args=None, post_body=None, content_type=None): - # make a request query_args = ("?=%s" % urlencode(query_args)) if query_args else "" kwargs = {} From e3af7db7ede8aee495c4754999b0f568b50d18c7 Mon Sep 17 00:00:00 2001 From: Nick Pilon Date: Thu, 18 Jan 2024 13:33:34 -0400 Subject: [PATCH 5/6] Correct sqlalchemy14 environment --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 932c1b3..7cf97a0 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,7 @@ deps = pyramid18: pyramid <= 1.8.99 pyramid19: pyramid <= 1.9.99 pyramid110: pyramid <= 1.10.99 - sqlalchemy14: sqlalchemy <= 1.4 + sqlalchemy14: sqlalchemy < 2.0 sqlalchemy20: sqlalchemy >= 2.0, <2.1 commands = From f03dee32c6758d16b2cb8ff6791cbd3b6f7f3d7f Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sat, 27 Jan 2024 13:03:52 -0700 Subject: [PATCH 6/6] more sqla 1.4+ updates to use context managers --- demo/sqla.py | 35 ++++++++++++------------- src/pyramid_debugtoolbar/panels/sqla.py | 12 ++++----- tests/test_panels/test_sqla.py | 12 ++++----- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/demo/sqla.py b/demo/sqla.py index 8cf8276..385225d 100644 --- a/demo/sqla.py +++ b/demo/sqla.py @@ -1,7 +1,6 @@ from pyramid.view import view_config -from sqlalchemy import create_engine -from sqlalchemy import MetaData +from sqlalchemy import MetaData, create_engine, text from sqlalchemy import Table, Column, Integer, String from sqlalchemy.pool import StaticPool @@ -14,32 +13,32 @@ ) def initialize_sql(settings): - engine = create_engine('sqlite://', - connect_args={'check_same_thread':False}, - poolclass=StaticPool, - echo=True) + engine = create_engine( + 'sqlite://', + connect_args={'check_same_thread':False}, + poolclass=StaticPool, + echo=True, + ) settings['engine'] = engine - - try: - populate_db(engine) - except: - pass + populate_db(engine) def populate_db(engine): meta.create_all(bind=engine) - users = ('blaflamme', 'mcdonc', 'mmerickel') - try: + with engine.connect() as conn: + users = ('blaflamme', 'mcdonc', 'mmerickel') for i, user in enumerate(users): - engine.execute('insert into users (id, name) values (:id, :name)', - id=i, name=user) - except: - pass + conn.execute( + text('insert into users (id, name) values (:id, :name)'), + {'id': i, 'name': user}, + ) + conn.commit() @view_config(route_name='test_sqla', renderer='__main__:templates/sqla.mako') def test_sqla(request): engine = request.registry.settings['engine'] - users = engine.execute('select * from users') + with engine.connect() as conn: + users = conn.execute(text('select * from users')).all() return { 'title':'Test SQLAlchemy logging', 'users':users, diff --git a/src/pyramid_debugtoolbar/panels/sqla.py b/src/pyramid_debugtoolbar/panels/sqla.py index 421726c..8b01c95 100644 --- a/src/pyramid_debugtoolbar/panels/sqla.py +++ b/src/pyramid_debugtoolbar/panels/sqla.py @@ -229,11 +229,11 @@ def sql_select(self): engines = self.request.registry.parent_registry.pdtb_sqla_engines engine = engines[int(engine_id)]() - with engine.connect() as connection: - result = connection.exec_driver_sql(stmt, params) + with engine.connect() as conn: + result = conn.exec_driver_sql(stmt, params) return { - 'result': result.fetchall(), + 'result': result.all(), 'headers': result.keys(), 'sql': format_sql(stmt), 'duration': float(query_dict['duration']), @@ -262,11 +262,11 @@ def sql_explain(self): else: query = 'EXPLAIN %s' % stmt - with engine.connect() as connection: - result = connection.exec_driver_sql(query, params) + with engine.connect() as conn: + result = conn.exec_driver_sql(query, params) return { - 'result': result.fetchall(), + 'result': result.all(), 'headers': result.keys(), 'sql': format_sql(stmt), 'str': str, diff --git a/tests/test_panels/test_sqla.py b/tests/test_panels/test_sqla.py index a19d8ab..a8258c3 100644 --- a/tests/test_panels/test_sqla.py +++ b/tests/test_panels/test_sqla.py @@ -138,9 +138,9 @@ class TestSimpleSelect(_TestSQLAlchemyPanel): def _sqlalchemy_view(self, context, request): engine = sqlalchemy.create_engine("sqlite://", isolation_level=None) - conn = engine.connect() - stmt = sqla_text("SELECT NULL;") - conn.execute(stmt) # noqa + with engine.begin() as conn: + stmt = sqla_text("SELECT NULL;") + conn.execute(stmt) # noqa return ok_response_factory() def test_panel(self): @@ -157,8 +157,7 @@ class TestTransactionCommit(_TestSQLAlchemyPanel): def _sqlalchemy_view(self, context, request): engine = sqlalchemy.create_engine("sqlite://", isolation_level=None) - conn = engine.connect() - with conn.begin(): + with engine.begin() as conn: stmt = sqla_text("SELECT NULL;") conn.execute(stmt) # noqa return ok_response_factory() @@ -174,9 +173,8 @@ def test_panel(self): class TestTransactionRollback(_TestSQLAlchemyPanel): def _sqlalchemy_view(self, context, request): engine = sqlalchemy.create_engine("sqlite://", isolation_level=None) - conn = engine.connect() try: - with conn.begin(): + with engine.begin() as conn: stmt = sqla_text("SELECT NULL;") conn.execute(stmt) # noqa raise ValueError("EXPECTED")