diff --git a/CHANGES.rst b/CHANGES.rst index e17439a..b8ad489 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,12 @@ Changelog Here you can see the full list of changes between each SQLAlchemy-Utils release. +0.32.14 (2017-03-27) +^^^^^^^^^^^^^^^^^^^^ + +- Fixed drop_database version comparison + + 0.32.13 (2017-03-12) ^^^^^^^^^^^^^^^^^^^^ diff --git a/sqlalchemy_utils/__init__.py b/sqlalchemy_utils/__init__.py index 92341fa..8112aca 100644 --- a/sqlalchemy_utils/__init__.py +++ b/sqlalchemy_utils/__init__.py @@ -95,4 +95,4 @@ from .types import ( # noqa WeekDaysType ) -__version__ = '0.32.13' +__version__ = '0.32.14' diff --git a/sqlalchemy_utils/functions/database.py b/sqlalchemy_utils/functions/database.py index 2ca4a63..46e04c0 100644 --- a/sqlalchemy_utils/functions/database.py +++ b/sqlalchemy_utils/functions/database.py @@ -585,17 +585,14 @@ def drop_database(url): elif engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2': from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT - engine.raw_connection().set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) + + connection = engine.connect() + connection.connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # Disconnect all users from the database we are dropping. - version = list( - map( - int, - engine.execute('SHOW server_version').first()[0].split('.') - ) - ) + version = connection.dialect.server_version_info pid_column = ( - 'pid' if (version[0] >= 9 and version[1] >= 2) else 'procpid' + 'pid' if (version >= (9, 2)) else 'procpid' ) text = ''' SELECT pg_terminate_backend(pg_stat_activity.%(pid_column)s) @@ -603,12 +600,11 @@ def drop_database(url): WHERE pg_stat_activity.datname = '%(database)s' AND %(pid_column)s <> pg_backend_pid(); ''' % {'pid_column': pid_column, 'database': database} - engine.execute(text) + connection.execute(text) # Drop the database. - text = 'DROP DATABASE {0}'.format(quote(engine, database)) - engine.execute(text) - + text = 'DROP DATABASE {0}'.format(quote(connection, database)) + connection.execute(text) else: text = 'DROP DATABASE {0}'.format(quote(engine, database)) engine.execute(text)