Skip to content

Commit

Permalink
Merge pull request #79 from davesgonechina/feature/materialize-base-m…
Browse files Browse the repository at this point in the history
…odels-as-view

Suggestion to materialize base models as view or table
  • Loading branch information
joellabes authored Nov 23, 2022
2 parents 0a85cb2 + fd0830f commit 8e42e2c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ model.
* `table_name` (required): The source table you wish to generate base model SQL for.
* `leading_commas` (optional, default=False): Whether you want your commas to be leading (vs trailing).
* `case_sensitive_cols ` (optional, default=False): Whether your source table has case sensitive column names. If true, keeps the case of the column names from the source.
* `materialized` (optional, default=None): Set materialization style (e.g. table, view, incremental) inside of the model's `config` block. If not set, materialization style will be controlled by `dbt_project.yml`


### Usage:
Expand All @@ -101,7 +102,8 @@ model.
```
{{ codegen.generate_base_model(
source_name='raw_jaffle_shop',
table_name='customers'
table_name='customers',
materialized='table'
) }}
```

Expand Down
1 change: 1 addition & 0 deletions integration_tests/tests/test_generate_base_models.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
%}

{% set expected_base_model %}

with source as (

select * from {%raw%}{{ source('codegen_integration_tests__data_source_schema', 'codegen_integration_tests__data_source_table') }}{%endraw%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
source_name='codegen_integration_tests__data_source_schema',
table_name='codegen_integration_tests__data_source_table_case_sensitive',
leading_commas=True,
case_sensitive_cols=True
case_sensitive_cols=True,
materialized='table'
)
%}

{% set expected_base_model %}
{{ "{{ config(materialized='table') }}" }}

with source as (

select * from {%raw%}{{ source('codegen_integration_tests__data_source_schema', 'codegen_integration_tests__data_source_table_case_sensitive') }}{%endraw%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
%}

{% set expected_base_model %}

with source as (

select * from {%raw%}{{ source('codegen_integration_tests__data_source_schema', 'codegen_integration_tests__data_source_table_case_sensitive') }}{%endraw%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
%}

{% set expected_base_model %}

with source as (

select * from {%raw%}{{ source('codegen_integration_tests__data_source_schema', 'codegen_integration_tests__data_source_table') }}{%endraw%}
Expand Down
7 changes: 6 additions & 1 deletion macros/generate_base_model.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{% macro generate_base_model(source_name, table_name, leading_commas=False, case_sensitive_cols=False) %}
{% macro generate_base_model(source_name, table_name, leading_commas=False, case_sensitive_cols=False, materialized=None) %}

{%- set source_relation = source(source_name, table_name) -%}

{%- set columns = adapter.get_columns_in_relation(source_relation) -%}
{% set column_names=columns | map(attribute='name') %}
{% set base_model_sql %}

{%- if materialized is not none -%}
{{ "{{ config(materialized='" ~ materialized ~ "') }}" }}
{%- endif %}

with source as (

select * from {% raw %}{{ source({% endraw %}'{{ source_name }}', '{{ table_name }}'{% raw %}) }}{% endraw %}
Expand Down

0 comments on commit 8e42e2c

Please sign in to comment.