Skip to content

Commit

Permalink
Psycopg2cffi support (#447)
Browse files Browse the repository at this point in the history
* support drop_database

* support create_database

* add test case
  • Loading branch information
DominicBurkart authored May 24, 2020
1 parent 041ab71 commit 3090944
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_version():
'flexmock>=0.9.7',
'mock==2.0.0',
'psycopg2>=2.5.1',
'psycopg2cffi>=2.8.1',
'pg8000>=1.12.4',
'pytz>=2014.2',
'python-dateutil>=2.6',
Expand Down
38 changes: 26 additions & 12 deletions sqlalchemy_utils/functions/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,6 @@ def create_database(url, encoding='utf8', template=None):
result_proxy = None

if engine.dialect.name == 'postgresql':
if engine.driver == 'psycopg2':
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
engine.raw_connection().set_isolation_level(
ISOLATION_LEVEL_AUTOCOMMIT
)

if not template:
template = 'template1'

Expand All @@ -560,7 +554,19 @@ def create_database(url, encoding='utf8', template=None):
encoding,
quote(engine, template)
)
result_proxy = engine.execute(text)

if engine.driver == 'psycopg2cffi':
connection = engine.connect()
connection.connection.set_session(autocommit=True)
connection.execute(text)
else:
if engine.driver == 'psycopg2':
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
engine.raw_connection().set_isolation_level(
ISOLATION_LEVEL_AUTOCOMMIT
)

result_proxy = engine.execute(text)

elif engine.dialect.name == 'mysql':
text = "CREATE DATABASE {0} CHARACTER SET = '{1}'".format(
Expand Down Expand Up @@ -619,11 +625,19 @@ def drop_database(url):
if database:
os.remove(database)

elif engine.dialect.name == 'postgresql' and engine.driver == 'psycopg2':
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

connection = engine.connect()
connection.connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
elif (
engine.dialect.name == 'postgresql' and
engine.driver in {'psycopg2', 'psycopg2cffi'}
):
if engine.driver == 'psycopg2':
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
connection = engine.connect()
connection.connection.set_isolation_level(
ISOLATION_LEVEL_AUTOCOMMIT
)
else:
connection = engine.connect()
connection.connection.set_session(autocommit=True)

# Disconnect all users from the database we are dropping.
version = connection.dialect.server_version_info
Expand Down
10 changes: 10 additions & 0 deletions tests/functions/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ def dsn(self, postgresql_db_user):
)


class TestDatabasePostgresPsycoPG2CFFI(DatabaseTest):

@pytest.fixture
def dsn(self, postgresql_db_user):
return 'postgresql+psycopg2cffi://{0}@localhost/{1}'.format(
postgresql_db_user,
'db_to_test_create_and_drop_via_psycopg2cffi_driver'
)


@pytest.mark.usefixtures('postgresql_dsn')
class TestDatabasePostgresWithQuotedName(DatabaseTest):

Expand Down

0 comments on commit 3090944

Please sign in to comment.