Skip to content

Commit

Permalink
Feature/generate_source_descs_iss64 (#66)
Browse files Browse the repository at this point in the history
* update generate_source.sql to add descriptions for table/source and name arg

* add integration tests

* fix test

* update all args source test

* changelog

* Update macros/generate_source.sql

Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>

* Update README.md

Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>

* moved name to end up arguments block in readme

* Update integration_tests/tests/test_generate_source_all_args.sql

Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>

* move name to last arg in test_generate_source_all_args

Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com>
  • Loading branch information
kbrock91 and dbeatty10 authored Jun 22, 2022
1 parent 5d36cd6 commit 15c5ec3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unreleased
## New features
- Add support for importing descriptions from columns with the same names in upstream models. It is available by setting the parameter `upstream_descriptions` to `True` in `generate_model_yaml` ([#61](https://github.com/dbt-labs/dbt-codegen/pull/61))
- Add support for including description placeholders for the source and table, which changes the behavior of `generate_source` when `include_descriptions` is set to `True`. Previous logic only created description placeholders for the columns.
- Add optional `name` arg to `generate_source`

# dbt-codegen v0.6.0

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ description placeholders to your source definition.
* `table_pattern` (optional, default='%'): A table prefix / postfix that you
want to subselect from all available tables within a given schema.
* `exclude` (optional, default=''): A string you want to exclude from the selection criteria
* `name` (optional, default=schema_name): The name of your source

### Usage:
1. Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code
Expand Down
6 changes: 5 additions & 1 deletion integration_tests/tests/test_generate_source_all_args.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
exclude='',
database_name=target.database,
generate_columns=True,
include_descriptions=True
include_descriptions=True,
name=raw_schema
) %}


Expand All @@ -17,15 +18,18 @@ version: 2

sources:
- name: {{ raw_schema | trim | lower }}
description: ""
tables:
- name: data__a_relation
description: ""
columns:
- name: col_a
description: ""
- name: col_b
description: ""

- name: data__b_relation
description: ""
columns:
- name: col_a
description: ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

{% set raw_schema = generate_schema_name('raw_data') %}

-- test default args
{% set actual_source_yaml = codegen.generate_source(raw_schema, include_descriptions=True) %}

{% set expected_source_yaml %}
version: 2

sources:
- name: {{ raw_schema | trim }}
description: ""
tables:
- name: data__a_relation
description: ""
- name: data__b_relation
description: ""
{% endset %}


{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }}
20 changes: 20 additions & 0 deletions integration_tests/tests/test_generate_source_table_name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

{% set raw_schema = generate_schema_name('raw_data') %}

-- test default args
{% set actual_source_yaml = codegen.generate_source(raw_schema, name='raw') %}

{% set expected_source_yaml %}
version: 2

sources:
- name: raw
schema: {{ raw_schema | trim }}
tables:
- name: data__a_relation
- name: data__b_relation

{% endset %}


{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }}
17 changes: 13 additions & 4 deletions macros/generate_source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,35 @@


---
{% macro generate_source(schema_name, database_name=target.database, generate_columns=False, include_descriptions=False, table_pattern='%', exclude='') %}
{% macro generate_source(schema_name, database_name=target.database, generate_columns=False, include_descriptions=False, table_pattern='%', exclude='', name=schema_name) %}

{% set sources_yaml=[] %}

{% do sources_yaml.append('version: 2') %}
{% do sources_yaml.append('') %}
{% do sources_yaml.append('sources:') %}
{% do sources_yaml.append(' - name: ' ~ schema_name | lower) %}
{% 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:') %}

{% set tables=codegen.get_tables_in_schema(schema_name, database_name, table_pattern, exclude) %}

{% 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:') %}

Expand Down

0 comments on commit 15c5ec3

Please sign in to comment.