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

Fix errors on null schema (#980) #992

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 19 additions & 6 deletions dbt/adapters/default/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@
connections_available = []


def _filter_schemas(manifest):
"""Return a function that takes a row and decides if the row should be
included in the catalog output.
"""
schemas = frozenset({
node.schema.lower()
for node in manifest.nodes.values()
})

def test(row):
# the schema may be present but None
table_schema = row.get('table_schema')
if table_schema is None:
return False
return table_schema.lower() in schemas
return test


class DefaultAdapter(object):
DEFAULT_QUOTE = True

Expand Down Expand Up @@ -830,10 +848,5 @@ def get_catalog(cls, profile, project_cfg, manifest):
finally:
cls.release_connection(profile, GET_CATALOG_OPERATION_NAME)

schemas = list({
node.schema.lower()
for node in manifest.nodes.values()
})

results = table.where(lambda r: r['table_schema'].lower() in schemas)
results = table.where(_filter_schemas(manifest))
return results
29 changes: 29 additions & 0 deletions test/unit/test_postgres_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dbt.adapters.postgres import PostgresAdapter
from dbt.exceptions import ValidationException
from dbt.logger import GLOBAL_LOGGER as logger # noqa
import agate


class TestPostgresAdapter(unittest.TestCase):
Expand Down Expand Up @@ -79,3 +80,31 @@ def test_set_zero_keepalive(self, psycopg2):
port=5432,
connect_timeout=10)

@mock.patch.object(PostgresAdapter, 'run_operation')
def test_get_catalog_various_schemas(self, mock_run):
column_names = ['table_schema', 'table_name']
rows = [
('foo', 'bar'),
('FOO', 'baz'),
(None, 'bar'),
('quux', 'bar'),
('skip', 'bar')
]
mock_run.return_value = agate.Table(rows=rows,
column_names=column_names)

# we should accept the lowercase matching 'foo's only.
mock_nodes = [
mock.MagicMock(spec_set=['schema'], schema='foo')
for k in range(2)
]
mock_nodes.append(mock.MagicMock(spec_set=['schema'], schema='quux'))
nodes = {str(idx): n for idx, n in enumerate(mock_nodes)}
# give manifest the dict it wants
mock_manifest = mock.MagicMock(spec_set=['nodes'], nodes=nodes)

catalog = PostgresAdapter.get_catalog({}, {}, mock_manifest)
self.assertEqual(
set(map(tuple, catalog)),
{('foo', 'bar'), ('FOO', 'baz'), ('quux', 'bar')}
)