Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass schema in credentials to Postgresql #1476

Merged
merged 9 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ target/

# Vim
*.sw*

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this? you can create a .gitignore locally without including it in this repo's .gitignore. Instructions here: https://help.github.com/en/articles/ignoring-files#create-a-global-gitignore

# PyCharm
.idea/*
11 changes: 10 additions & 1 deletion plugins/postgres/dbt/adapters/postgres/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
'schema': {
'type': 'string',
},
'search_path': {
'type': 'string',
},
'keepalives_idle': {
'type': 'integer',
},
Expand All @@ -53,7 +56,7 @@ def type(self):
return 'postgres'

def _connection_keys(self):
return ('host', 'port', 'user', 'database', 'schema')
return ('host', 'port', 'user', 'database', 'schema', 'search_path')


class PostgresConnectionManager(SQLConnectionManager):
Expand Down Expand Up @@ -105,6 +108,12 @@ def open(cls, connection):
if keepalives_idle:
kwargs['keepalives_idle'] = keepalives_idle

# psycopg2 doesn't support search_path officially, see https:/psycopg/psycopg2/issues/465
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugins/postgres/dbt/adapters/postgres/connections.py:111:80: E501 line too long (109 > 79 characters)

search_path = credentials.get('search_path')
if search_path is not None and search_path != '':
# see https://www.postgresql.org/docs/9.5/libpq-connect.html#LIBPQ-CONNECT-OPTIONS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugins/postgres/dbt/adapters/postgres/connections.py:114:80: E501 line too long (94 > 79 characters)

kwargs['options'] = '-c search_path={}'.format(search_path.replace(' ', '\\ '))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugins/postgres/dbt/adapters/postgres/connections.py:115:80: E501 line too long (91 > 79 characters)


try:
handle = psycopg2.connect(
dbname=credentials.database,
Expand Down
5 changes: 4 additions & 1 deletion plugins/redshift/dbt/adapters/redshift/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
'If using IAM auth, the ttl for the temporary credentials'
)
},
'search_path': {
'type': 'string',
},
'keepalives_idle': {
'type': 'integer',
},
Expand All @@ -75,7 +78,7 @@ def type(self):
return 'redshift'

def _connection_keys(self):
return ('host', 'port', 'user', 'database', 'schema', 'method')
return ('host', 'port', 'user', 'database', 'schema', 'method', 'search_path')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plugins/redshift/dbt/adapters/redshift/connections.py:81:80: E501 line too long (86 > 79 characters)



class RedshiftConnectionManager(PostgresConnectionManager):
Expand Down
32 changes: 32 additions & 0 deletions test/unit/test_postgres_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,38 @@ def test_changed_keepalive(self, psycopg2):
connect_timeout=10,
keepalives_idle=256)

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_search_path(self, psycopg2):
self.config.credentials = self.config.credentials.incorporate(
search_path="test"
)
connection = self.adapter.acquire_connection('dummy')

psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='thishostshouldnotexist',
password='password',
port=5432,
connect_timeout=10,
options="-c search_path=test")

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_schema_with_space(self, psycopg2):
self.config.credentials = self.config.credentials.incorporate(
search_path="test test"
)
connection = self.adapter.acquire_connection('dummy')

psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='thishostshouldnotexist',
password='password',
port=5432,
connect_timeout=10,
options="-c search_path=test\ test")

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_set_zero_keepalive(self, psycopg2):
self.config.credentials = self.config.credentials.incorporate(
Expand Down
34 changes: 34 additions & 0 deletions test/unit/test_redshift_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ def test_changed_keepalive(self, psycopg2):
connect_timeout=10,
keepalives_idle=256)

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_search_path(self, psycopg2):
self.config.credentials = self.config.credentials.incorporate(
search_path="test"
)
connection = self.adapter.acquire_connection('dummy')

psycopg2.connect.assert_called_once_with(
dbname='redshift',
user='root',
host='thishostshouldnotexist',
password='password',
port=5439,
connect_timeout=10,
options="-c search_path=test",
keepalives_idle=RedshiftAdapter.ConnectionManager.DEFAULT_TCP_KEEPALIVE)

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_search_path_with_space(self, psycopg2):
self.config.credentials = self.config.credentials.incorporate(
search_path="test test"
)
connection = self.adapter.acquire_connection('dummy')

psycopg2.connect.assert_called_once_with(
dbname='redshift',
user='root',
host='thishostshouldnotexist',
password='password',
port=5439,
connect_timeout=10,
options="-c search_path=test\ test",
keepalives_idle=RedshiftAdapter.ConnectionManager.DEFAULT_TCP_KEEPALIVE)

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_set_zero_keepalive(self, psycopg2):
self.config.credentials = self.config.credentials.incorporate(
Expand Down