-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathgithub_union_connections.sql
More file actions
94 lines (73 loc) · 3.85 KB
/
Copy pathgithub_union_connections.sql
File metadata and controls
94 lines (73 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{% macro github_union_connections(connection_dictionary, single_source_name, single_table_name, default_identifier=single_table_name) %}
{{ adapter.dispatch('github_union_connections', 'github') (connection_dictionary, single_source_name, single_table_name, default_identifier) }}
{%- endmacro %}
{% macro default__github_union_connections(connection_dictionary, single_source_name, single_table_name, default_identifier=single_table_name) %}
{%- set exception_warning = "\n\nPlease be aware: The " ~ single_source_name|upper ~ "." ~ single_table_name|upper ~ " table was not found in your schema(s). The Fivetran Data Model will create a completely empty staging model as to not break downstream transformations. To turn off these warnings, set the `fivetran__remove_empty_table_warnings` variable to TRUE (see https://github.com/fivetran/dbt_fivetran_utils/tree/releases/v0.4.latest#union_data-source for details).\n"%}
{%- set using_empty_table_warnings = (execute and not var('fivetran__remove_empty_table_warnings', false)) %}
{%- set connections = var(connection_dictionary, []) %}
{%- set using_unioning = connections | length > 0 %}
{%- if using_unioning %}
{# For unioning #}
{%- set relations = [] -%}
{%- for connection in connections -%}
{% if var('has_defined_sources', false) %}
{%- set database = source(connection.name, single_table_name).database %}
{%- set schema = source(connection.name, single_table_name).schema %}
{%- set identifier = source(connection.name, single_table_name).identifier %}
{%- else %}
{%- set database = connection.database if connection.database else target.database %}
{%- set schema = connection.schema if connection.schema else single_source_name %}
{%- set identifier = default_identifier %}
{%- endif %}
{%- set relation=adapter.get_relation(
database=database,
schema=schema,
identifier=identifier
)
-%}
{%- if relation is not none -%}
{%- do relations.append(relation) -%}
{%- endif -%}
-- ** Values passed to adapter.get_relation:
{{ '-- full-identifier_var: ' ~ identifier_var }}
{{ '-- database: ' ~ database }}
{{ '-- schema: ' ~ schema }}
{{ '-- identifier: ' ~ identifier ~ '\n' }}
{%- endfor -%}
{%- if relations != [] -%}
{{ github.github_union_relations(relations) }}
{%- else -%}
{{ exceptions.warn(exception_warning) if using_empty_table_warnings }}
select
cast(null as {{ dbt.type_string() }}) as _dbt_source_relation
limit {{ '0' if target.type != 'redshift' else '1' }}
{%- endif -%}
{% else %}
{# Not unioning #}
{% set identifier_var = single_source_name + "_" + single_table_name + "_identifier"%}
{%- set database = source(single_source_name, single_table_name).database %}
{%- set schema = source(single_source_name, single_table_name).schema %}
{%- set identifier = source(single_source_name, single_table_name).identifier %}
{%- set relation=adapter.get_relation(
database=database,
schema=schema,
identifier=identifier
)
-%}
-- ** Values passed to adapter.get_relation:
{{ '-- full-identifier_var: ' ~ identifier_var }}
{{ '-- database: ' ~ database }}
{{ '-- schema: ' ~ schema }}
{{ '-- identifier: ' ~ identifier ~ '\n' }}
{% if relation is not none -%}
select
{{ dbt_utils.star(from=source(single_source_name, single_table_name)) }}
from {{ source(single_source_name, single_table_name) }} as source_table
{% else %}
{{ exceptions.warn(exception_warning) if using_empty_table_warnings }}
select
cast(null as {{ dbt.type_string() }}) as _dbt_source_relation
limit {{ '0' if target.type != 'redshift' else '1' }}
{%- endif -%}
{% endif -%}
{%- endmacro %}