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

escape the wildcard underscore in postgres "like" queries (#1960) #2003

Merged
merged 2 commits into from
Dec 12, 2019
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
2 changes: 1 addition & 1 deletion plugins/postgres/dbt/include/postgres/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
join pg_catalog.pg_attribute col on col.attrelid = tbl.oid

where sch.nspname != 'information_schema'
and sch.nspname not like 'pg_%' -- avoid postgres system schemas
and sch.nspname not like 'pg\_%' -- avoid postgres system schemas, '_' is a wildcard so escape it
and not pg_is_other_temp_schema(sch.oid) -- not a temporary schema belonging to another session
and tbl.relpersistence = 'p' -- [p]ermanent table. Other values are [u]nlogged table, [t]emporary table
and tbl.relkind in ('r', 'v', 'f', 'p') -- o[r]dinary table, [v]iew, [f]oreign table, [p]artitioned table. Other values are [i]ndex, [S]equence, [c]omposite type, [t]OAST table, [m]aterialized view
Expand Down
2 changes: 1 addition & 1 deletion plugins/postgres/dbt/include/postgres/macros/relations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
pg_namespace.oid as id,
pg_namespace.nspname as name
from pg_namespace
where nspname != 'information_schema' and nspname not like 'pg_%'
where nspname != 'information_schema' and nspname not like 'pg\_%'
),
referenced as (
select
Expand Down
2 changes: 1 addition & 1 deletion plugins/redshift/dbt/include/redshift/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
join table_owners using (table_database, table_schema, table_name)

where table_schema != 'information_schema'
and table_schema not like 'pg_%'
and table_schema not like 'pg\_%'

order by "column_index"
{%- endcall -%}
Expand Down
34 changes: 34 additions & 0 deletions test/integration/029_docs_generate_tests/test_docs_generate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import hashlib
import json
import os
import random
import time
from datetime import datetime
from unittest.mock import ANY, patch

Expand Down Expand Up @@ -53,6 +55,38 @@ def walk_files(path):
yield os.path.join(root, basename)


class TestDocsGenerateEscapes(DBTIntegrationTest):
prefix = "pgtest{}{:04}".format(int(time.time()), random.randint(0, 9999))

@property
def schema(self):
return 'docs_generate_029'

@staticmethod
def dir(path):
return normalize(path)

@property
def models(self):
return self.dir("trivial_models")

def run_and_generate(self):
self.assertEqual(len(self.run_dbt(['run'])), 1)
os.remove(normalize('target/manifest.json'))
os.remove(normalize('target/run_results.json'))
self.run_dbt(['docs', 'generate'])

@use_profile('postgres')
def test_postgres_include_schema(self):
self.run_and_generate()
manifest = _read_json('./target/manifest.json')
self.assertIn('nodes', manifest)
self.assertEqual(len(manifest['nodes']), 1)
self.assertIn('model.test.model', manifest['nodes'])
self.assertIn('schema', manifest['nodes']['model.test.model'])
self.assertEqual('pg', manifest['nodes']['model.test.model']['schema'][:2])


class TestDocsGenerate(DBTIntegrationTest):
setup_alternate_db = True

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as id