Skip to content

Commit 17976f6

Browse files
authored
Merge pull request #2044 from lonvia/fix-use-with-psycopg3
Fix running osm2pgsql-replication with psycopg3
2 parents 4be6ec8 + f8451e7 commit 17976f6

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

.github/actions/ubuntu-prerequisites/action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ runs:
3030
postgresql-${POSTGRESQL_VERSION}-postgis-${POSTGIS_VERSION} \
3131
postgresql-${POSTGRESQL_VERSION}-postgis-${POSTGIS_VERSION}-scripts \
3232
postgresql-client postgresql-contrib-${POSTGRESQL_VERSION} \
33-
python3-psycopg2 \
3433
python3-setuptools \
3534
zlib1g-dev
3635
pip3 install behave osmium
3736
if [ "$CC" = clang-8 ]; then sudo apt-get install -yq --no-install-suggests --no-install-recommends clang-8; fi
37+
if [ "$PSYCOPG" = "2"]; then
38+
sudo apt-get install -yq --no-install-suggests --no-install-recommends python3-psycopg2
39+
else
40+
pip3 install psycopg
41+
fi
3842
shell: bash
3943

4044
- name: Install Lua

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
POSTGRESQL_VERSION: 9.6
5959
POSTGIS_VERSION: 2.5
6060
BUILD_TYPE: Release
61+
PSYCOPG: 2
6162

6263
steps:
6364
- uses: actions/checkout@v3
@@ -75,6 +76,7 @@ jobs:
7576
POSTGRESQL_VERSION: 9.6
7677
POSTGIS_VERSION: 2.5
7778
BUILD_TYPE: Release
79+
PSYCOPG: 2
7880

7981
steps:
8082
- uses: actions/checkout@v3
@@ -93,6 +95,7 @@ jobs:
9395
POSTGRESQL_VERSION: 10
9496
POSTGIS_VERSION: 3
9597
BUILD_TYPE: Debug
98+
PSYCOPG: 2
9699

97100
steps:
98101
- uses: actions/checkout@v3
@@ -111,6 +114,7 @@ jobs:
111114
POSTGRESQL_VERSION: 11
112115
POSTGIS_VERSION: 2.5
113116
BUILD_TYPE: Debug
117+
PSYCOPG: 2
114118

115119
steps:
116120
- uses: actions/checkout@v3
@@ -128,6 +132,7 @@ jobs:
128132
POSTGRESQL_VERSION: 12
129133
POSTGIS_VERSION: 2.5
130134
BUILD_TYPE: Debug
135+
PSYCOPG: 2
131136

132137
steps:
133138
- uses: actions/checkout@v3
@@ -146,6 +151,7 @@ jobs:
146151
POSTGRESQL_VERSION: 13
147152
POSTGIS_VERSION: 3
148153
BUILD_TYPE: Debug
154+
PSYCOPG: 2
149155

150156
steps:
151157
- uses: actions/checkout@v3
@@ -164,6 +170,7 @@ jobs:
164170
POSTGIS_VERSION: 3
165171
USE_PROJ_LIB: 6
166172
BUILD_TYPE: Debug
173+
PSYCOPG: 2
167174

168175
steps:
169176
- uses: actions/checkout@v3
@@ -182,6 +189,7 @@ jobs:
182189
POSTGIS_VERSION: 3
183190
USE_PROJ_LIB: off
184191
BUILD_TYPE: Debug
192+
PSYCOPG: 2
185193

186194
steps:
187195
- uses: actions/checkout@v3
@@ -200,6 +208,7 @@ jobs:
200208
POSTGIS_VERSION: 3
201209
USE_PROJ_LIB: 6
202210
BUILD_TYPE: Debug
211+
PSYCOPG: 2
203212

204213
steps:
205214
- uses: actions/checkout@v3
@@ -217,6 +226,7 @@ jobs:
217226
POSTGRESQL_VERSION: 13
218227
POSTGIS_VERSION: 2.5
219228
BUILD_TYPE: Release
229+
PSYCOPG: 2
220230

221231
steps:
222232
- uses: actions/checkout@v3
@@ -232,6 +242,7 @@ jobs:
232242
POSTGRESQL_VERSION: 13
233243
POSTGIS_VERSION: 2.5
234244
BUILD_TYPE: Release
245+
PSYCOPG: 2
235246

236247
steps:
237248
- uses: actions/checkout@v3
@@ -249,6 +260,7 @@ jobs:
249260
POSTGRESQL_VERSION: 14
250261
POSTGIS_VERSION: 3
251262
BUILD_TYPE: Debug
263+
PSYCOPG: 2
252264

253265
steps:
254266
- uses: actions/checkout@v3
@@ -267,6 +279,7 @@ jobs:
267279
POSTGIS_VERSION: 3
268280
USE_PROJ_LIB: 6
269281
BUILD_TYPE: Debug
282+
PSYCOPG: 2
270283

271284
steps:
272285
- uses: actions/checkout@v3
@@ -285,6 +298,7 @@ jobs:
285298
POSTGIS_VERSION: 3
286299
USE_PROJ_LIB: off
287300
BUILD_TYPE: Debug
301+
PSYCOPG: 2
288302

289303
steps:
290304
- uses: actions/checkout@v3
@@ -303,6 +317,7 @@ jobs:
303317
POSTGIS_VERSION: 3
304318
USE_PROJ_LIB: 6
305319
BUILD_TYPE: Debug
320+
PSYCOPG: 2
306321

307322
steps:
308323
- uses: actions/checkout@v3
@@ -321,6 +336,7 @@ jobs:
321336
POSTGRESQL_VERSION: 14
322337
POSTGIS_VERSION: 3
323338
BUILD_TYPE: Release
339+
PSYCOPG: 2
324340

325341
steps:
326342
- uses: actions/checkout@v3
@@ -337,6 +353,7 @@ jobs:
337353
POSTGRESQL_VERSION: 14
338354
POSTGIS_VERSION: 3
339355
BUILD_TYPE: Release
356+
PSYCOPG: 2
340357

341358
steps:
342359
- uses: actions/checkout@v3
@@ -355,6 +372,7 @@ jobs:
355372
POSTGIS_VERSION: 3
356373
CPP_VERSION: 20
357374
BUILD_TYPE: Debug
375+
PSYCOPG: 3
358376

359377
steps:
360378
- uses: actions/checkout@v3
@@ -373,6 +391,7 @@ jobs:
373391
POSTGIS_VERSION: 3
374392
CPP_VERSION: 20
375393
BUILD_TYPE: Debug
394+
PSYCOPG: 3
376395

377396
steps:
378397
- uses: actions/checkout@v3

scripts/osm2pgsql-replication

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class DBConnection:
122122
host=args.host, port=args.port,
123123
fallback_application_name="osm2pgsql-replication")
124124

125-
self.name = self.conn.get_dsn_parameters()['dbname']
125+
self.name = self.conn.info.dbname
126126

127127
def __enter__(self):
128128
return self

tests/bdd/environment.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
from importlib.machinery import SourceFileLoader
1414

1515
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+
1823

1924
from steps.geometry_factory import GeometryFactory
2025
from steps.replication_server_mock import ReplicationServerMock
@@ -44,14 +49,18 @@ def _connect_db(context, dbname):
4449
object as a context manager that automatically closes.
4550
Note that the connection does not commit automatically.
4651
"""
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)
4858

4959

5060
def _drop_db(context, dbname, recreate_immediately=False):
5161
""" Drop the database with the given name if it exists.
5262
"""
5363
with _connect_db(context, 'postgres') as conn:
54-
conn.set_isolation_level(0)
5564
with conn.cursor() as cur:
5665
db = sql.Identifier(dbname)
5766
cur.execute(sql.SQL('DROP DATABASE IF EXISTS {}').format(db))
@@ -136,7 +145,6 @@ def test_db(context, **kwargs):
136145
_drop_db(context, dbname, recreate_immediately=True)
137146

138147
with _connect_db(context, dbname) as conn:
139-
conn.autocommit = True
140148

141149
with conn.cursor() as cur:
142150
cur.execute('CREATE EXTENSION postgis')

tests/bdd/steps/steps_db.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import re
1212
from typing import Iterable
1313

14-
from psycopg2 import sql
14+
try:
15+
from psycopg2 import sql
16+
except ImportError:
17+
from psycopg import sql
18+
1519

1620
@given("the database schema (?P<schema>.+)")
1721
def create_db_schema(context, schema):

0 commit comments

Comments
 (0)