Skip to content

Commit 4574ece

Browse files
author
Manuel J. Morillo
authored
Merge pull request #395 from CartoDB/pg12_compatibility
Make cartodb-postgresql python3 compatible
2 parents 076cacc + e37cdb4 commit 4574ece

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.36.0 (2020-XX-XX)
2+
* Make `_CDB_Group_API_Auth` python3 compatible by passing bytes representation instead of a string.
3+
* Make `_CDB_Group_API_Request` python3 compatible by adapting the function signature of `HTTPConnection`.
4+
15
0.35.0 (2019-12-30)
26
* Reapply the changes in 0.33.0 (the issue we were looking for was unrelated)
37
* Reapply `Make PG12 depend on plpython3u instead of plpythonu`

scripts-available/CDB_Groups_API.sql

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,30 @@ CREATE OR REPLACE
197197
FUNCTION @extschema@._CDB_Group_API_Auth(username text, password text)
198198
RETURNS TEXT AS
199199
$$
200+
import sys
200201
import base64
201-
return base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
202+
203+
data_to_encode = '%s:%s' % (username, password)
204+
if sys.version_info[0] < 3:
205+
data_encoded = base64.encodestring(data_to_encode)
206+
else:
207+
data_encoded = base64.b64encode(data_to_encode.encode()).decode()
208+
209+
data_encoded = data_encoded.replace('\n', '')
210+
return data_encoded
202211
$$ LANGUAGE '@@plpythonu@@' VOLATILE PARALLEL UNSAFE;
203212

204213
-- url must contain a '%s' placeholder that will be replaced by current_database, for security reasons.
205214
CREATE OR REPLACE
206215
FUNCTION @extschema@._CDB_Group_API_Request(method text, url text, body text, valid_return_codes int[])
207216
RETURNS int AS
208217
$$
218+
python_v2 = True
209219
try:
210-
import httplib as client
220+
import httplib as client
211221
except:
212-
from http import client
222+
from http import client
223+
python_v2 = False
213224

214225
params = plpy.execute("select c.host, c.port, c.timeout, c.auth from @extschema@._CDB_Group_API_Conf() c;")[0]
215226
if params['host'] is None:
@@ -222,7 +233,10 @@ $$
222233
last_err = None
223234
while retry > 0:
224235
try:
225-
conn = SD['groups_api_client'] = client.HTTPConnection(params['host'], params['port'], False, params['timeout'])
236+
if python_v2:
237+
conn = SD['groups_api_client'] = client.HTTPConnection(params['host'], params['port'], False, params['timeout'])
238+
else:
239+
conn = SD['groups_api_client'] = client.HTTPConnection(params['host'], port=params['port'], timeout=params['timeout'])
226240
database_name = plpy.execute("select current_database();")[0]['current_database']
227241
conn.request(method, url.format(database_name), body, headers)
228242
response = conn.getresponse()

0 commit comments

Comments
 (0)