Skip to content

Commit

Permalink
Merge pull request #2154 from dholleran-lendico/dev/barbara-gittings
Browse files Browse the repository at this point in the history
Feature: adding optional sslmode parameter to postgres connection
  • Loading branch information
beckjake authored Feb 27, 2020
2 parents 5d4d4f3 + 170b1d8 commit 735ffb3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Features
- Add a "docs" field to models, with a "show" subfield ([#1671](https://github.com/fishtown-analytics/dbt/issues/1671), [#2107](https://github.com/fishtown-analytics/dbt/pull/2107))
- Add a dbt-{dbt_version} user agent field to the bigquery connector ([#2121](https://github.com/fishtown-analytics/dbt/issues/2121), [#2146](https://github.com/fishtown-analytics/dbt/pull/2146))
- Adding optional "sslmode" parameter for postgres ([#2152](https://github.com/fishtown-analytics/dbt/issues/2152), [#2154](https://github.com/fishtown-analytics/dbt/pull/2154))
- Add support for `generate_database_name` macro ([#1695](https://github.com/fishtown-analytics/dbt/issues/1695), [#2143](https://github.com/fishtown-analytics/dbt/pull/2143))
- Expand the search path for schema.yml (and by extension, the default docs path) to include macro-paths and analysis-paths (in addition to source-paths, data-paths, and snapshot-paths) ([#2155](https://github.com/fishtown-analytics/dbt/issues/2155), [#2160](https://github.com/fishtown-analytics/dbt/pull/2160))

Expand All @@ -23,6 +24,7 @@
Contributors:
- [@bubbomb](https://github.com/bubbomb) ([#2080](https://github.com/fishtown-analytics/dbt/pull/2080))
- [@sonac](https://github.com/sonac) ([#2078](https://github.com/fishtown-analytics/dbt/pull/2078))
- [@dholleran-lendico](https://github.com/dholleran-lendico) ([#2154](https://github.com/fishtown-analytics/dbt/pull/2154))


## dbt 0.16.0b1 (February 11, 2020)
Expand Down
6 changes: 5 additions & 1 deletion plugins/postgres/dbt/adapters/postgres/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PostgresCredentials(Credentials):
password: str # on postgres the password is mandatory
search_path: Optional[str] = None
keepalives_idle: int = 0 # 0 means to use the default value
sslmode: Optional[str] = None

_ALIASES = {
'dbname': 'database',
Expand All @@ -33,7 +34,7 @@ def type(self):

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


class PostgresConnectionManager(SQLConnectionManager):
Expand Down Expand Up @@ -89,6 +90,9 @@ def open(cls, connection):
kwargs['options'] = '-c search_path={}'.format(
search_path.replace(' ', '\\ '))

if credentials.sslmode:
kwargs['sslmode'] = credentials.sslmode

try:
handle = psycopg2.connect(
dbname=credentials.database,
Expand Down
17 changes: 17 additions & 0 deletions test/unit/test_postgres_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def test_changed_keepalive(self, psycopg2):
connect_timeout=10,
keepalives_idle=256)


@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_role(self, psycopg2):
self.config.credentials = self.config.credentials.replace(role='somerole')
Expand All @@ -159,6 +160,22 @@ def test_search_path(self, psycopg2):
connect_timeout=10,
options="-c search_path=test")

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_sslmode(self, psycopg2):
self.config.credentials = self.config.credentials.replace(sslmode="require")
connection = self.adapter.acquire_connection('dummy')

psycopg2.connect.assert_not_called()
connection.handle
psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='thishostshouldnotexist',
password='password',
port=5432,
connect_timeout=10,
sslmode="require")

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_schema_with_space(self, psycopg2):
self.config.credentials = self.config.credentials.replace(search_path="test test")
Expand Down

0 comments on commit 735ffb3

Please sign in to comment.