Skip to content

Commit

Permalink
Merge pull request #1 from GeneralMills/add_macros
Browse files Browse the repository at this point in the history
Add macros
  • Loading branch information
pinakidas authored Jun 14, 2022
2 parents 19a892e + bb7a33f commit 39a5bb0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @pinakidas
68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
# gmi_common_dbt_utils
# gmi_common_dbt_utils

This dbt package contains macros which can reused across General Mills dbt projects

## Installation Instructions

To add this package into your dbt project you need to make an entry in the packages.yml file if its not already present

```yml
- git: "https://github.com/GeneralMills/gmi_common_dbt_utils.git"
revision: main # use a branch or a tag name
```
## Contents
### Macros
- [generate_schema_name](#generate_schema_name) [(source)](./macros/generate_schema_name.sql)
- [smart_source](#smart_source) [(source)](./macros/smart_source.sql)
### Usage
#### generate_schema_name
This overwrites the default implementation of generate_schema_name from the core package
```text
{{generate_schema_name('input')}}
```

#### smart_source
This macro helps use keep a check on the bigquery costs and at the same time validate sql queries end to end.
You can use the codegen package to have the script generated and then replace `source` with `smart_source`.
[Link](https://github.com/dbt-labs/dbt-codegen#usage-1) on usage of codegen for generating SQL for a base model

Snippets to generate base mode code for reference

Executing using scratchpad/statement tab in dbtCloud IDE
```
{{ codegen.generate_base_model(
source_name='<source_name>',
table_name='<table_name>'
) }}
```

Executing the macro as an operation
```
dbt run-operation generate_base_model --args '{"source_name": "<source_name>", "table_name": "<table_name>"}'
```

On the generated sql, replace `source` with `smart_source`

```sql
with source as (
select * from {{ smart_source('<source_name>', '<table_name>') }}
),

renamed as (
select
.
.
.
from source
),

select * from renamed
```
11 changes: 11 additions & 0 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: 'gmi_common_dbt_utils'
version: '0.1.0'

require-dbt-version: [">=1.0.0", "<2.0.0"]

config-version: 2

target-path: "target"
clean-targets: ["target", "dbt_modules", "dbt_packages"]
macro-paths: ["macros"]
log-path: "logs"
10 changes: 10 additions & 0 deletions macros/generate_schema_name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% macro generate_schema_name(custom_schema_name, node) -%}
{%- set developer_dataset_number = env_var('DBT_DEVELOPER_DATASET_NUMBER') -%}
{%- set use_zdev = env_var('DBT_USE_ZDEV_DATASET') -%}
{%- set default_schema = target.schema -%}
{%- if env_var('DBT_USE_ZDEV_DATASET') | upper == 'TRUE' and custom_schema_name is not none -%}
{{ default_schema }}{{ developer_dataset_number }}_{{ custom_schema_name | trim }}
{%- else -%}
{{ custom_schema_name | trim }}
{%- endif -%}
{%- endmacro %}
7 changes: 7 additions & 0 deletions macros/smart_source.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% macro smart_source(source_name, table_name) %}
{%- if env_var('DBT_RUN_ENV') == 'DEV' -%}
(select * from {{source(source_name,table_name) }} where 1 = 0) as __dbt_source_{{ table_name }}
{% else %}
{{source(source_name, table_name)}}
{% endif %}
{% endmacro %}

0 comments on commit 39a5bb0

Please sign in to comment.