Patches for connecting to Azure PSQL (#42)

* * patches in code to handle user@host format in the Azure PSQL connection string
* adds code for handling new connection string format in prerun.py

* * fixes path in the postgres username patch
* cleans up prerun.py imports
This commit is contained in:
Blagoja Stojkoski 2021-02-18 15:02:23 +01:00 committed by GitHub
parent 08c10f2dc6
commit 21c891865d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 3 deletions

View File

@ -0,0 +1,11 @@
--- ckan/ckan/model/__init__.py 2021-02-16 14:47:06.168327441 +0100
+++ ckan/ckan/model/__init__.py 2021-02-16 14:48:00.740780218 +0100
@@ -266,7 +266,7 @@
self.reset_alembic_output()
alembic_config = AlembicConfig(self._alembic_ini)
alembic_config.set_main_option(
- "sqlalchemy.url", str(self.metadata.bind.url)
+ "sqlalchemy.url", str(self.metadata.bind.url).replace('%', '%%')
)
try:
sqlalchemy_migrate_version = self.metadata.bind.execute(

View File

@ -0,0 +1,11 @@
--- ckan/ckanext/datastore/backend/postgres.py 2021-02-18 11:01:56.692267462 +0100
+++ ckan/ckanext/datastore/backend/postgres-patch.py 2021-02-18 13:45:16.033193435 +0100
@@ -1690,7 +1690,7 @@
read only user.
'''
write_connection = self._get_write_engine().connect()
- read_connection_user = sa_url.make_url(self.read_url).username
+ read_connection_user = sa_url.make_url(self.read_url).username.split("@")[0]
drop_foo_sql = u'DROP TABLE IF EXISTS _foo'

View File

@ -2,6 +2,7 @@ import os
import sys
import subprocess
import psycopg2
from sqlalchemy.engine.url import make_url
import urllib.request, urllib.error, urllib.parse
import re
@ -23,7 +24,14 @@ def check_db_connection(retry=None):
conn_str = os.environ.get('CKAN_SQLALCHEMY_URL', '')
try:
connection = psycopg2.connect(conn_str)
db_user = make_url(conn_str).username
db_passwd = make_url(conn_str).password
db_host = make_url(conn_str).host
db_name = make_url(conn_str).database
connection = psycopg2.connect(user=db_user,
host=db_host,
password=db_passwd,
database=db_name)
except psycopg2.Error as e:
print((str(e)))
@ -96,7 +104,14 @@ def init_datastore():
datastore_perms_command = ['ckan', '-c', ckan_ini, 'datastore',
'set-permissions']
connection = psycopg2.connect(conn_str)
db_user = make_url(conn_str).username
db_passwd = make_url(conn_str).password
db_host = make_url(conn_str).host
db_name = make_url(conn_str).database
connection = psycopg2.connect(user=db_user,
host=db_host,
password=db_passwd,
database=db_name)
cursor = connection.cursor()
print('[prerun] Initializing datastore db - start')
@ -106,8 +121,10 @@ def init_datastore():
stdout=subprocess.PIPE)
perms_sql = datastore_perms.stdout.read()
perms_sql = perms_sql.decode('utf-8')
perms_sql = perms_sql.replace("@"+db_host, "")
# Remove internal pg command as psycopg2 does not like it
perms_sql = re.sub('\\\\connect \"(.*)\"', '', perms_sql.decode('utf-8'))
perms_sql = re.sub('\\\\connect \"(.*)\"', '', perms_sql)
cursor.execute(perms_sql)
for notice in connection.notices:
print(notice)