Skip to content

Commit 2012ee8

Browse files
Feat/generate sources add database and schema (#124)
* adding database and schema property to generate source macro * add optional arguments to include database and schema properties in generate_source * updated README for new generate_source arguments * updated argument names and cleaned up spacing * cleaned up spacing * fix spacing
1 parent e24e5dc commit 2012ee8

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
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.
1717
- Add `include_data_types` flag to `generate_source` macro ([#76](https://github.com/dbt-labs/dbt-codegen/pull/76))
1818
- 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.
19+
- 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))
1920

2021
## Fixes
2122
- 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))

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ types to your source columns definitions.
5656
want to subselect from all available tables within a given schema.
5757
* `exclude` (optional, default=''): A string you want to exclude from the selection criteria
5858
* `name` (optional, default=schema_name): The name of your source
59+
* `include_database` (optional, default=False): Whether you want to add
60+
the database to your source definition
61+
* `include_schema` (optional, default=False): Whether you want to add
62+
the schema to your source definition
5963

6064
### Usage:
6165
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
9195
sources:
9296
- name: raw_jaffle_shop
9397
database: raw
98+
schema: raw_jaffle_shop
9499
tables:
95100
- name: customers
96101
description: ""

integration_tests/tests/test_generate_source_all_args.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
generate_columns=True,
1010
include_descriptions=True,
1111
include_data_types=True,
12-
name=raw_schema
12+
name=raw_schema,
13+
table_names=None,
14+
include_database=True,
15+
include_schema=True
1316
) %}
1417

1518

@@ -20,6 +23,8 @@ version: 2
2023
sources:
2124
- name: {{ raw_schema | trim | lower }}
2225
description: ""
26+
database: analytics
27+
schema: codegen_integration_tests_snowflake_raw_data
2328
tables:
2429
- name: data__a_relation
2530
description: ""
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
{% set raw_schema = generate_schema_name('raw_data') %}
3+
4+
{% set actual_source_yaml = codegen.generate_source(raw_schema, include_database=True) %}
5+
6+
{% set expected_source_yaml %}
7+
version: 2
8+
9+
sources:
10+
- name: {{ raw_schema | trim | lower }}
11+
database: analytics
12+
tables:
13+
- name: data__a_relation
14+
- name: data__b_relation
15+
- name: data__campaign_analytics
16+
{% endset %}
17+
18+
19+
{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
{% set raw_schema = generate_schema_name('raw_data') %}
3+
4+
{% set actual_source_yaml = codegen.generate_source(raw_schema, include_schema=True) %}
5+
6+
{% set expected_source_yaml %}
7+
version: 2
8+
9+
sources:
10+
- name: {{ raw_schema | trim | lower }}
11+
schema: {{ raw_schema | trim | lower }}
12+
tables:
13+
- name: data__a_relation
14+
- name: data__b_relation
15+
- name: data__campaign_analytics
16+
{% endset %}
17+
18+
19+
{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }}

macros/generate_source.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
---
18-
{% 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) %}
18+
{% 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) %}
1919

2020
{% set sources_yaml=[] %}
2121
{% do sources_yaml.append('version: 2') %}
@@ -27,11 +27,11 @@
2727
{% do sources_yaml.append(' description: ""' ) %}
2828
{% endif %}
2929

30-
{% if database_name != target.database %}
30+
{% if database_name != target.database or include_database %}
3131
{% do sources_yaml.append(' database: ' ~ database_name | lower) %}
3232
{% endif %}
3333

34-
{% if schema_name != name %}
34+
{% if schema_name != name or include_schema %}
3535
{% do sources_yaml.append(' schema: ' ~ schema_name | lower) %}
3636
{% endif %}
3737

0 commit comments

Comments
 (0)