Skip to content

Commit

Permalink
PR feedback from #1202
Browse files Browse the repository at this point in the history
Postgres/Redshift permissions issues:
 - use pg_views/pg_tables to list relations
 - use pg_namespace to list schemas and check schema existence
Catalog:
 - Redshift: fix error, add database check
 - Snowflake: Use the information_schema_name macro
Snowflake/default: Quote the database properly in information_schema_name
  • Loading branch information
Jacob Beck committed Jan 4, 2019
1 parent 0f9c4a8 commit def23d8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/dbt/include/global_project/macros/adapters/common.sql
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

{% macro default__information_schema_name(database) -%}
{%- if database -%}
"{{ database }}".information_schema
{{ adapter.quote_as_configured(database, 'database') }}.information_schema
{%- else -%}
information_schema
{%- endif -%}
Expand Down
40 changes: 30 additions & 10 deletions plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@
{% macro postgres__list_relations_without_caching(database, schema) %}
{% call statement('list_relations_without_caching', fetch_result=True) -%}
select
table_catalog as database,
table_name as name,
table_schema as schema,
case when table_type = 'BASE TABLE' then 'table'
when table_type = 'VIEW' then 'view'
else table_type
end as table_type
from {{ information_schema_name(database) }}.tables
where table_schema ilike '{{ schema }}'
and table_catalog ilike '{{ database }}'
'{{ database }}' as database,
tablename as name,
schemaname as schema,
'table' as type
from pg_tables
where schemaname ilike '{{ schema }}'
union all
select
'{{ database }}' as database,
viewname as name,
schemaname as schema,
'view' as type
from pg_views
where schemaname ilike '{{ schema }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{% endmacro %}
Expand All @@ -62,3 +66,19 @@
{%- endif -%}
information_schema
{%- endmacro %}

{% macro postgres__list_schemas(database) %}
{% if database -%}
{{ adapter.verify_database(database) }}
{%- endif -%}
"select distinct nspname from pg_namespace"
{% endmacro %}

{% macro postgres__check_schema_exists(database, schema) -%}
{% if database -%}
{{ adapter.verify_database(database) }}
{%- endif -%}
{% call statement('check_schema_exists', fetch_result=True, auto_begin=False) %}
select count(*) from pg_namespace where nspname = '{{ schema }}'
{% endcall %}
{% endmacro %}
12 changes: 12 additions & 0 deletions plugins/redshift/dbt/include/redshift/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
{{ postgres__create_schema(database_name, schema_name) }}
{% endmacro %}


{% macro redshift__drop_schema(database_name, schema_name) -%}
{{ postgres__drop_schema(database_name, schema_name) }}
{% endmacro %}
Expand Down Expand Up @@ -160,3 +161,14 @@
{% macro redshift__information_schema_name(database) -%}
{{ return(postgres__information_schema_name(database)) }}
{%- endmacro %}


{% macro redshift__list_schemas(database) -%}
{{ return(postgres__list_schemas(database)) }}
{%- endmacro %}


{% macro redshift__check_schema_exists(database, schema) -%}
{{ return(postgres__check_schema_exists(database, schema)) }}
{%- endmacro %}
list_schemas
4 changes: 3 additions & 1 deletion plugins/redshift/dbt/include/redshift/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{% macro redshift__get_base_catalog() -%}
{%- call statement('base_catalog', fetch_result=True) -%}
{% if (databases | length) != 1 %}
exceptions.raise_compiler_error('postgres get_catalog requires exactly one database')
exceptions.raise_compiler_error('redshift get_catalog requires exactly one database')
{% endif %}
{% set database = databases[0] %}
{{ adapter.verify_database(database) }}

with late_binding as (
select
'{{ database }}'::varchar as table_database,
Expand Down
2 changes: 1 addition & 1 deletion plugins/snowflake/dbt/include/snowflake/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'Approximate size of the table as reported by Snowflake' as "stats:bytes:description",
(bytes is not null) as "stats:bytes:include"

from {{ adapter.quote_as_configured(database, "database") }}.information_schema.tables
from {{ information_schema_name(database) }}.tables

),

Expand Down

0 comments on commit def23d8

Please sign in to comment.