From 0d0d02ea3c700bd48bcd7de976419c720b03ed1f Mon Sep 17 00:00:00 2001 From: LerikP Date: Fri, 7 Apr 2023 16:56:51 +0300 Subject: [PATCH 1/3] Add psycopg3 support for database functions --- sqlalchemy_utils/functions/database.py | 4 ++-- tests/functions/test_database.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/sqlalchemy_utils/functions/database.py b/sqlalchemy_utils/functions/database.py index 322526e8..a615f040 100644 --- a/sqlalchemy_utils/functions/database.py +++ b/sqlalchemy_utils/functions/database.py @@ -556,7 +556,7 @@ def create_database(url, encoding='utf8', template=None): if (dialect_name == 'mssql' and dialect_driver in {'pymssql', 'pyodbc'}) \ or (dialect_name == 'postgresql' and dialect_driver in { - 'asyncpg', 'pg8000', 'psycopg2', 'psycopg2cffi'}): + 'asyncpg', 'pg8000', 'psycopg', 'psycopg2', 'psycopg2cffi'}): engine = sa.create_engine(url, isolation_level='AUTOCOMMIT') else: engine = sa.create_engine(url) @@ -625,7 +625,7 @@ def drop_database(url): if dialect_name == 'mssql' and dialect_driver in {'pymssql', 'pyodbc'}: engine = sa.create_engine(url, connect_args={'autocommit': True}) elif dialect_name == 'postgresql' and dialect_driver in { - 'asyncpg', 'pg8000', 'psycopg2', 'psycopg2cffi'}: + 'asyncpg', 'pg8000', 'psycopg', 'psycopg2', 'psycopg2cffi'}: engine = sa.create_engine(url, isolation_level='AUTOCOMMIT') else: engine = sa.create_engine(url) diff --git a/tests/functions/test_database.py b/tests/functions/test_database.py index 0ae64064..5f81ca50 100644 --- a/tests/functions/test_database.py +++ b/tests/functions/test_database.py @@ -98,6 +98,17 @@ def dsn(self, postgresql_db_user, postgresql_db_password): ) +class TestDatabasePostgresPsycoPG3(DatabaseTest): + + @pytest.fixture + def dsn(self, postgresql_db_user, postgresql_db_password): + return 'postgresql+psycopg://{}:{}@localhost/{}'.format( + postgresql_db_user, + postgresql_db_password, + 'db_to_test_create_and_drop_via_psycopg3_driver' + ) + + @pytest.mark.usefixtures('postgresql_dsn') class TestDatabasePostgresWithQuotedName(DatabaseTest): From 63754a92097506ab1350c3bd8eefd610c94afc29 Mon Sep 17 00:00:00 2001 From: LerikP Date: Mon, 10 Apr 2023 10:23:54 +0300 Subject: [PATCH 2/3] Add psycopg3 to test dependencies --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 9074f793..4ca0a540 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,7 @@ def get_version(): 'Jinja2>=2.3', 'docutils>=0.10', 'flexmock>=0.9.7', + 'psycopg>=3.1.8', 'psycopg2>=2.5.1', 'psycopg2cffi>=2.8.1', 'pg8000>=1.12.4', From 4fa31bc2052f82724a9281765b142d4499eb5fc1 Mon Sep 17 00:00:00 2001 From: LerikP Date: Mon, 10 Apr 2023 10:45:26 +0300 Subject: [PATCH 3/3] Skip psycopg3 tests for sqlalchemy < 2.0.0 --- tests/functions/test_database.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/functions/test_database.py b/tests/functions/test_database.py index 5f81ca50..cdd631ad 100644 --- a/tests/functions/test_database.py +++ b/tests/functions/test_database.py @@ -2,6 +2,7 @@ import sqlalchemy as sa from sqlalchemy_utils import create_database, database_exists, drop_database +from sqlalchemy_utils.compat import get_sqlalchemy_version pymysql = None try: @@ -9,6 +10,8 @@ except ImportError: pass +sqlalchemy_version = get_sqlalchemy_version() + class DatabaseTest: def test_create_and_drop(self, dsn): @@ -98,6 +101,7 @@ def dsn(self, postgresql_db_user, postgresql_db_password): ) +@pytest.mark.skipif('sqlalchemy_version < (2, 0, 0)') class TestDatabasePostgresPsycoPG3(DatabaseTest): @pytest.fixture