-
Notifications
You must be signed in to change notification settings - Fork 111
/
generate_source.sql
87 lines (65 loc) · 3.55 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
83
84
85
86
87
{% 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, include_data_types=True, table_pattern='%', exclude='', name=schema_name, table_names=None, include_database=False, include_schema=False, case_sensitive_databases=False, case_sensitive_schemas=False, case_sensitive_tables=False, case_sensitive_cols=False) %}
{{ return(adapter.dispatch('generate_source', 'codegen')(schema_name, database_name, generate_columns, include_descriptions, include_data_types, table_pattern, exclude, name, table_names, include_database, include_schema, case_sensitive_databases, case_sensitive_schemas, case_sensitive_tables, case_sensitive_cols)) }}
{% endmacro %}
{% macro default__generate_source(schema_name, database_name, generate_columns, include_descriptions, include_data_types, table_pattern, exclude, name, table_names, include_database, include_schema, case_sensitive_databases, case_sensitive_schemas, case_sensitive_tables, case_sensitive_cols) %}
{% 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 or include_database %}
{% do sources_yaml.append(' database: ' ~ (database_name if case_sensitive_databases else database_name | lower)) %}
{% endif %}
{% if schema_name != name or include_schema %}
{% do sources_yaml.append(' schema: ' ~ (schema_name if case_sensitive_schemas else 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 if case_sensitive_tables else 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 if case_sensitive_cols else column.name | lower)) %}
{% if include_data_types %}
{% do sources_yaml.append(' data_type: ' ~ codegen.data_type_format_source(column)) %}
{% endif %}
{% if include_descriptions %}
{% do sources_yaml.append(' description: ""' ) %}
{% endif %}
{% endfor %}
{% do sources_yaml.append('') %}
{% endif %}
{% endfor %}
{% if execute %}
{% set joined = sources_yaml | join ('\n') %}
{{ print(joined) }}
{% do return(joined) %}
{% endif %}
{% endmacro %}