forked from dbt-labs/dbt-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_source.sql
82 lines (60 loc) · 2.38 KB
/
generate_source.sql
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
{% macro get_tables_in_schema(schema_name, database_name=target.database, table_pattern='%', exclude='') %}
{% set tables=dbt_utils.get_relations_by_pattern(
schema_pattern=schema_name,
database=database_name,
table_pattern=table_pattern,
exclude=exclude
) %}
{% set table_list= tables | map(attribute='identifier') %}
{{ return(table_list | sort) }}
{% endmacro %}
---
{% macro generate_source(schema_name, database_name=target.database, generate_columns=False, include_descriptions=False, table_pattern='%', exclude='', name=schema_name, table_names=None) %}
{% set sources_yaml=[] %}
{% do sources_yaml.append('version: 2') %}
{% do sources_yaml.append('') %}
{% do sources_yaml.append('sources:') %}
{% do sources_yaml.append(' - name: ' ~ name | lower) %}
{% if include_descriptions %}
{% do sources_yaml.append(' description: ""' ) %}
{% endif %}
{% if database_name != target.database %}
{% do sources_yaml.append(' database: ' ~ database_name | lower) %}
{% endif %}
{% if schema_name != name %}
{% do sources_yaml.append(' schema: ' ~ schema_name | lower) %}
{% endif %}
{% do sources_yaml.append(' tables:') %}
{% if table_names is none %}
{% set tables=codegen.get_tables_in_schema(schema_name, database_name, table_pattern, exclude) %}
{% else %}
{% set tables = table_names %}
{% endif %}
{% for table in tables %}
{% do sources_yaml.append(' - name: ' ~ table | lower ) %}
{% if include_descriptions %}
{% do sources_yaml.append(' description: ""' ) %}
{% endif %}
{% if generate_columns %}
{% do sources_yaml.append(' columns:') %}
{% set table_relation=api.Relation.create(
database=database_name,
schema=schema_name,
identifier=table
) %}
{% set columns=adapter.get_columns_in_relation(table_relation) %}
{% for column in columns %}
{% do sources_yaml.append(' - name: ' ~ column.name | lower ) %}
{% if include_descriptions %}
{% do sources_yaml.append(' description: ""' ) %}
{% endif %}
{% endfor %}
{% do sources_yaml.append('') %}
{% endif %}
{% endfor %}
{% if execute %}
{% set joined = sources_yaml | join ('\n') %}
{{ log(joined, info=True) }}
{% do return(joined) %}
{% endif %}
{% endmacro %}