Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/generate sources add database and schema #124

Merged
merged 6 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +95,7 @@ version: 2
sources:
- name: raw_jaffle_shop
database: raw
schema: raw_jaffle_shop
tables:
- name: customers
description: ""
Expand Down
7 changes: 6 additions & 1 deletion integration_tests/tests/test_generate_source_all_args.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
) %}


Expand All @@ -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: ""
Expand Down
Original file line number Diff line number Diff line change
@@ -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) }}
Original file line number Diff line number Diff line change
@@ -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) }}
6 changes: 3 additions & 3 deletions macros/generate_source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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') %}
Expand All @@ -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 %}

Expand Down