Fix drop_database version comparison

This commit is contained in:
Konsta Vesterinen
2017-03-27 16:49:04 +03:00
parent b87a83557c
commit e53f48ea98
3 changed files with 15 additions and 13 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release. 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) 0.32.13 (2017-03-12)
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^

View File

@@ -95,4 +95,4 @@ from .types import ( # noqa
WeekDaysType WeekDaysType
) )
__version__ = '0.32.13' __version__ = '0.32.14'

View File

@@ -585,17 +585,14 @@ def drop_database(url):
elif engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2': elif engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2':
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT 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. # Disconnect all users from the database we are dropping.
version = list( version = connection.dialect.server_version_info
map(
int,
engine.execute('SHOW server_version').first()[0].split('.')
)
)
pid_column = ( pid_column = (
'pid' if (version[0] >= 9 and version[1] >= 2) else 'procpid' 'pid' if (version >= (9, 2)) else 'procpid'
) )
text = ''' text = '''
SELECT pg_terminate_backend(pg_stat_activity.%(pid_column)s) 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' WHERE pg_stat_activity.datname = '%(database)s'
AND %(pid_column)s <> pg_backend_pid(); AND %(pid_column)s <> pg_backend_pid();
''' % {'pid_column': pid_column, 'database': database} ''' % {'pid_column': pid_column, 'database': database}
engine.execute(text) connection.execute(text)
# Drop the database. # Drop the database.
text = 'DROP DATABASE {0}'.format(quote(engine, database)) text = 'DROP DATABASE {0}'.format(quote(connection, database))
engine.execute(text) connection.execute(text)
else: else:
text = 'DROP DATABASE {0}'.format(quote(engine, database)) text = 'DROP DATABASE {0}'.format(quote(engine, database))
engine.execute(text) engine.execute(text)