diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e37590..58de5f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This macro generates a series of terminal commands (appended w) bash script which creates a new file in your dbt project based off the results of the [generate_base_model](macros/generate_base_model.sql) macro. Therefore, instead of outputting in the terminal, it will create the file for you. - Add `include_data_types` flag to `generate_source` macro ([#76](https://github.com/dbt-labs/dbt-codegen/pull/76)) - Add `get_models` macro in helper macros. This macro retrieves a list of models with specified prefix at the specified directory. It is designed to make creating yamls for multiple models easier. +- Add optional arguments to include database and schema properties in `sources.yml` generated from `generate_source` ([#123](https://github.com/dbt-labs/dbt-codegen/issues/123)) ## Fixes - Fix handling of nested `STRUCT` fields in BigQuery ([#98](https://github.com/dbt-labs/dbt-codegen/issues/98), [#105](https://github.com/dbt-labs/dbt-codegen/pull/105)) diff --git a/README.md b/README.md index 472ae8a..481b07f 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,10 @@ types to your source columns definitions. 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 +* `include_database` (optional, default=False): Whether you want to add +the database to your source definition +* `include_schema` (optional, default=False): Whether you want to add +the schema to your source definition ### Usage: 1. Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code @@ -91,6 +95,7 @@ version: 2 sources: - name: raw_jaffle_shop database: raw + schema: raw_jaffle_shop tables: - name: customers description: "" diff --git a/integration_tests/tests/test_generate_source_all_args.sql b/integration_tests/tests/test_generate_source_all_args.sql index fb46970..b6ac69c 100644 --- a/integration_tests/tests/test_generate_source_all_args.sql +++ b/integration_tests/tests/test_generate_source_all_args.sql @@ -9,7 +9,10 @@ generate_columns=True, include_descriptions=True, include_data_types=True, - name=raw_schema + name=raw_schema, + table_names=None, + include_database=True, + include_schema=True ) %} @@ -20,6 +23,8 @@ version: 2 sources: - name: {{ raw_schema | trim | lower }} description: "" + database: analytics + schema: codegen_integration_tests_snowflake_raw_data tables: - name: data__a_relation description: "" diff --git a/integration_tests/tests/test_generate_source_include_database_property.sql b/integration_tests/tests/test_generate_source_include_database_property.sql new file mode 100644 index 0000000..e64e899 --- /dev/null +++ b/integration_tests/tests/test_generate_source_include_database_property.sql @@ -0,0 +1,19 @@ + +{% set raw_schema = generate_schema_name('raw_data') %} + +{% set actual_source_yaml = codegen.generate_source(raw_schema, include_database=True) %} + +{% set expected_source_yaml %} +version: 2 + +sources: + - name: {{ raw_schema | trim | lower }} + database: analytics + tables: + - name: data__a_relation + - name: data__b_relation + - name: data__campaign_analytics +{% endset %} + + +{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }} diff --git a/integration_tests/tests/test_generate_source_include_schema_property.sql b/integration_tests/tests/test_generate_source_include_schema_property.sql new file mode 100644 index 0000000..6fe08f7 --- /dev/null +++ b/integration_tests/tests/test_generate_source_include_schema_property.sql @@ -0,0 +1,19 @@ + +{% set raw_schema = generate_schema_name('raw_data') %} + +{% set actual_source_yaml = codegen.generate_source(raw_schema, include_schema=True) %} + +{% set expected_source_yaml %} +version: 2 + +sources: + - name: {{ raw_schema | trim | lower }} + schema: {{ raw_schema | trim | lower }} + tables: + - name: data__a_relation + - name: data__b_relation + - name: data__campaign_analytics +{% endset %} + + +{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }} diff --git a/macros/generate_source.sql b/macros/generate_source.sql index df81053..5097655 100644 --- a/macros/generate_source.sql +++ b/macros/generate_source.sql @@ -15,7 +15,7 @@ --- -{% macro generate_source(schema_name, database_name=target.database, generate_columns=False, include_descriptions=False, include_data_types=False, table_pattern='%', exclude='', name=schema_name, table_names=None) %} +{% macro generate_source(schema_name, database_name=target.database, generate_columns=False, include_descriptions=False, include_data_types=False, table_pattern='%', exclude='', name=schema_name, table_names=None, include_database=False, include_schema=False) %} {% set sources_yaml=[] %} {% do sources_yaml.append('version: 2') %} @@ -27,11 +27,11 @@ {% do sources_yaml.append(' description: ""' ) %} {% endif %} -{% if database_name != target.database %} +{% if database_name != target.database or include_database %} {% do sources_yaml.append(' database: ' ~ database_name | lower) %} {% endif %} -{% if schema_name != name %} +{% if schema_name != name or include_schema %} {% do sources_yaml.append(' schema: ' ~ schema_name | lower) %} {% endif %}