|
13 | 13 | from importlib.machinery import SourceFileLoader |
14 | 14 |
|
15 | 15 | from behave import * |
16 | | -import psycopg2 |
17 | | -from psycopg2 import sql |
| 16 | +try: |
| 17 | + import psycopg2 as psycopg |
| 18 | + from psycopg2 import sql |
| 19 | +except ImportError: |
| 20 | + import psycopg |
| 21 | + from psycopg import sql |
| 22 | + |
18 | 23 |
|
19 | 24 | from steps.geometry_factory import GeometryFactory |
20 | 25 | from steps.replication_server_mock import ReplicationServerMock |
@@ -44,14 +49,18 @@ def _connect_db(context, dbname): |
44 | 49 | object as a context manager that automatically closes. |
45 | 50 | Note that the connection does not commit automatically. |
46 | 51 | """ |
47 | | - return closing(psycopg2.connect(dbname=dbname)) |
| 52 | + if psycopg.__version__.startswith('2'): |
| 53 | + conn = psycopg.connect(dbname=dbname) |
| 54 | + conn.autocommit = True |
| 55 | + return closing(conn) |
| 56 | + |
| 57 | + return psycopg.connect(dbname=dbname, autocommit=True) |
48 | 58 |
|
49 | 59 |
|
50 | 60 | def _drop_db(context, dbname, recreate_immediately=False): |
51 | 61 | """ Drop the database with the given name if it exists. |
52 | 62 | """ |
53 | 63 | with _connect_db(context, 'postgres') as conn: |
54 | | - conn.set_isolation_level(0) |
55 | 64 | with conn.cursor() as cur: |
56 | 65 | db = sql.Identifier(dbname) |
57 | 66 | cur.execute(sql.SQL('DROP DATABASE IF EXISTS {}').format(db)) |
@@ -136,7 +145,6 @@ def test_db(context, **kwargs): |
136 | 145 | _drop_db(context, dbname, recreate_immediately=True) |
137 | 146 |
|
138 | 147 | with _connect_db(context, dbname) as conn: |
139 | | - conn.autocommit = True |
140 | 148 |
|
141 | 149 | with conn.cursor() as cur: |
142 | 150 | cur.execute('CREATE EXTENSION postgis') |
|
0 commit comments