Skip to content

Commit 462e3bc

Browse files
author
Claire Carroll
authored
Merge pull request #323 from fishtown-analytics/add-relation-type
Add relation type to get_relations_by_pattern and get_relations_by_prefix
2 parents dfacb59 + bc1e653 commit 462e3bc

5 files changed

+75
-29
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Fix `get_query_results_as_dict` integration test with consistent ordering ([#322](https://github.com/fishtown-analytics/dbt-utils/pull/322))
1010
- All macros are now properly dispatched, making it possible for non-core adapters to implement a shim package for dbt-utils ([#312](https://github.com/fishtown-analytics/dbt-utils/pull/312)) Thanks [@chaerinlee1](https://github.com/chaerinlee1) and [@swanderz](https://github.com/swanderz)
1111
- Small, non-breaking changes to accomdate TSQL (can't group by column number references, no real TRUE/FALSE values, aggregation CTEs need named columns) ([#310](https://github.com/fishtown-analytics/dbt-utils/pull/310)) Thanks [@swanderz](https://github.com/swanderz)
12+
- Make `get_relations_by_pattern` and `get_relations_by_prefix` more powerful by returning `relation.type`
1213

1314
# dbt-utils v0.6.3
1415

README.md

+49-24
Original file line numberDiff line numberDiff line change
@@ -453,56 +453,81 @@ Usage:
453453

454454
...
455455
```
456-
#### get_relations_by_prefix
456+
457+
458+
#### get_relations_by_pattern ([source](macros/sql/get_relations_by_pattern.sql))
459+
457460
Returns a list of [Relations](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation)
458-
that match a given prefix, with an optional exclusion pattern. It's particularly
459-
handy paired with `union_relations`.
461+
that match a given schema- or table-name pattern.
462+
463+
This macro is particularly handy when paired with `union_relations`.
464+
460465
**Usage:**
461466
```
462-
-- Returns a list of relations that match schema.prefix%
463-
{% set relations = dbt_utils.get_relations_by_prefix('my_schema', 'my_prefix') %}
467+
-- Returns a list of relations that match schema_pattern%.table
468+
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern%', 'table_pattern') %}
469+
470+
-- Returns a list of relations that match schema_pattern.table_pattern%
471+
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%') %}
464472
465473
-- Returns a list of relations as above, excluding any that end in `deprecated`
466-
{% set relations = dbt_utils.get_relations_by_prefix('my_schema', 'my_prefix', '%deprecated') %}
474+
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%', '%deprecated') %}
467475
468476
-- Example using the union_relations macro
469-
{% set event_relations = dbt_utils.get_relations_by_prefix('events', 'event_') %}
477+
{% set event_relations = dbt_utils.get_relations_by_pattern('venue%', 'clicks') %}
470478
{{ dbt_utils.union_relations(relations = event_relations) }}
471479
```
472480

473481
**Args:**
474-
* `schema` (required): The schema to inspect for relations.
475-
* `prefix` (required): The prefix of the table/view (case insensitive)
476-
* `exclude` (optional): Exclude any relations that match this pattern.
482+
* `schema_pattern` (required): The schema pattern to inspect for relations.
483+
* `table_pattern` (required): The name of the table/view (case insensitive).
484+
* `exclude` (optional): Exclude any relations that match this table pattern.
477485
* `database` (optional, default = `target.database`): The database to inspect
478486
for relations.
479487

480-
#### get_relations_by_pattern
481-
> This was built from the get_relations_by_prefix macro.
488+
**Examples:**
489+
Generate drop statements for all Relations that match a naming pattern:
490+
```sql
491+
{% set relations_to_drop = dbt_utils.get_relations_by_pattern(
492+
schema_pattern='public',
493+
table_pattern='dbt\_%'
494+
) %}
482495

496+
{% set sql_to_execute = [] %}
497+
498+
{{ log('Statements to run:', info=True) }}
499+
500+
{% for relation in relations_to_drop %}
501+
{% set drop_command -%}
502+
-- drop {{ relation.type }} {{ relation }} cascade;
503+
{%- endset %}
504+
{% do log(drop_command, info=True) %}
505+
{% do sql_to_execute.append(drop_command) %}
506+
{% endfor %}
507+
```
508+
509+
#### get_relations_by_prefix ([source](macros/sql/get_relations_by_prefix.sql))
510+
> This macro will soon be deprecated in favor of the more flexible `get_relations_by_pattern` macro (above)
483511
Returns a list of [Relations](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation)
484-
that match a given schema or table pattern and table/view name with an optional exclusion pattern. Like its cousin
485-
get_relations_by_prefix, it's particularly handy paired with `union_relations`.
512+
that match a given prefix, with an optional exclusion pattern. It's particularly
513+
handy paired with `union_relations`.
486514
**Usage:**
487515
```
488-
-- Returns a list of relations that match schema%.table
489-
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern%', 'table_pattern') %}
490-
491-
-- Returns a list of relations that match schema.table%
492-
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%') %}
516+
-- Returns a list of relations that match schema.prefix%
517+
{% set relations = dbt_utils.get_relations_by_prefix('my_schema', 'my_prefix') %}
493518
494519
-- Returns a list of relations as above, excluding any that end in `deprecated`
495-
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%', '%deprecated') %}
520+
{% set relations = dbt_utils.get_relations_by_prefix('my_schema', 'my_prefix', '%deprecated') %}
496521
497522
-- Example using the union_relations macro
498-
{% set event_relations = dbt_utils.get_relations_by_pattern('venue%', 'clicks') %}
523+
{% set event_relations = dbt_utils.get_relations_by_prefix('events', 'event_') %}
499524
{{ dbt_utils.union_relations(relations = event_relations) }}
500525
```
501526

502527
**Args:**
503-
* `schema_pattern` (required): The schema pattern to inspect for relations.
504-
* `table_pattern` (required): The name of the table/view (case insensitive).
505-
* `exclude` (optional): Exclude any relations that match this table pattern.
528+
* `schema` (required): The schema to inspect for relations.
529+
* `prefix` (required): The prefix of the table/view (case insensitive)
530+
* `exclude` (optional): Exclude any relations that match this pattern.
506531
* `database` (optional, default = `target.database`): The database to inspect
507532
for relations.
508533

macros/sql/get_relations_by_pattern.sql

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
{%- if table_list and table_list['table'] -%}
1616
{%- set tbl_relations = [] -%}
1717
{%- for row in table_list['table'] -%}
18-
{%- set tbl_relation = api.Relation.create(database, row.table_schema, row.table_name) -%}
18+
{%- set tbl_relation = api.Relation.create(
19+
database=database,
20+
schema=row.table_schema,
21+
identifier=row.table_name,
22+
type=row.table_type
23+
) -%}
1924
{%- do tbl_relations.append(tbl_relation) -%}
2025
{%- endfor -%}
2126

macros/sql/get_relations_by_prefix.sql

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
{%- if table_list and table_list['table'] -%}
1616
{%- set tbl_relations = [] -%}
1717
{%- for row in table_list['table'] -%}
18-
{%- set tbl_relation = api.Relation.create(database, row.table_schema, row.table_name) -%}
18+
{%- set tbl_relation = api.Relation.create(
19+
database=database,
20+
schema=row.table_schema,
21+
identifier=row.table_name,
22+
type=row.table_type
23+
) -%}
1924
{%- do tbl_relations.append(tbl_relation) -%}
2025
{%- endfor -%}
2126

macros/sql/get_tables_by_pattern_sql.sql

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
{% macro default__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}
77

88
select distinct
9-
table_schema as "table_schema", table_name as "table_name"
10-
from {{database}}.information_schema.tables
9+
table_schema as "table_schema",
10+
table_name as "table_name",
11+
case table_type
12+
when 'BASE TABLE' then 'table'
13+
else lower(table_type)
14+
end as "table_type"
15+
from {{ database }}.information_schema.tables
1116
where table_schema ilike '{{ schema_pattern }}'
1217
and table_name ilike '{{ table_pattern }}'
1318
and table_name not ilike '{{ exclude }}'
@@ -26,7 +31,12 @@
2631
{% set sql %}
2732
{% for schema in schemata %}
2833
select distinct
29-
table_schema, table_name
34+
table_schema,
35+
table_name,
36+
case table_type
37+
when 'BASE TABLE' then 'table'
38+
else lower(table_type)
39+
end as table_type
3040

3141
from {{ adapter.quote(database) }}.{{ schema }}.INFORMATION_SCHEMA.TABLES
3242
where lower(table_name) like lower ('{{ table_pattern }}')

0 commit comments

Comments
 (0)