entechlog / dbt-snow-utils

GNU General Public License v3.0
6 stars 0 forks source link

Overview

This dbt package contains Snowflake macros and models that can be (re)used across dbt projects with snowflake as target database.

Installation Instructions

✅ Packages can be added to your project using either of above options
✅ Please refer to the release version of this repo/dbt hub for the latest version. The version number mentioned above may not be the updated version number.

Models

Snowpipe

snowpipe__copy_history

snowpipe__usage_history

Arguments

Usage

Snowflake

snowflake__query_history

Arguments

Usage

Macros

dbt_snow_utils.clone_schemas

This macro clones the source schema/schemas into the destination database.

Arguments

Usage

run-operation
dbt run-operation dbt_snow_utils.clone_schemas --args "{'source_database': 'demo_db', 'source_schemas': ['dim', 'fact', 'utils'], 'destination_database': 'demo_db', 'destination_postfix': '_20220323_01'}"
pre_hook/post_hook
pre_hook="{{ dbt_snow_utils.clone_schemas(['dim', 'fact', 'utils'], '_backup', 'demo_db', this.database) }}"

dbt_snow_utils.clone_table

This macro clones the source table into the destination database/schema.

Arguments

Usage

run-operation
dbt run-operation clone_table --args '{"source_table": "COUNTRY_CODE", "destination_table": "COUNTRY_CODE_BKP"}'
pre_hook/post_hook
post_hook="{{ dbt_snow_utils.clone_table(this.identifier,this.identifier~'_temp', this.database, this.schema, this.database, this.schema ) }}"

dbt_snow_utils.clone_tables

This macro clones all the tables from source database/schema into the destination database/schema. This also provides an option to truncate the tables after cloning if you just need the table structure and not data.

Arguments

Usage

run-operation
dbt run-operation clone_tables --args "{'source_database': 'DEV_ENTECHLOG_DW_DB', 'source_schemas': ['dim', 'fact', 'utils'], 'destination_database': 'DEV_ENTECHLOG_DEMO_DB', 'truncate_table_flag': 'True'}"
pre_hook/post_hook
pre_hook="{{ dbt_snow_utils.clone_tables(['dim', 'fact', 'utils'], 'DEV_ENTECHLOG_DW_DB', 'DEV_ENTECHLOG_DEMO_DB', 'True') }}"

dbt_snow_utils.delete_records_by_column

This macro deletes data from a table based on a where clause. Often used as pre-hook in incremental loads to delete the data.

Arguments

Usage

run-operation
dbt run-operation delete_records_by_column --args '{"del_key": "payment_date", "del_value": "2005-05-25", "database": "DBT_DEMO", "schema": "MARTS", "table": "tmp_store_revenue"}'
pre_hook/post_hook
post_hook="{{ dbt_snow_utils.delete_records_by_column('payment_date', '2005-05-24') }}"

post_hook="{{ dbt_snow_utils.delete_records_by_column('payment_date', var('start_date')) }}"

dbt_snow_utils.delete_orphaned_tables

This macro deletes data from a table based on a where clause. Often used as pre-hook in incremental loads to delete the data.

Arguments

Usage

run-operation
pre_hook/post_hook

Materialization

stage2table

This materialization strategy loads data from a configured external stage (cloud storage location) directly into a Snowflake table using the COPY INTO command or an INSERT statement.

Configuration

Usage

Configure this materialization in your model's configuration block in dbt_project.yml or within the model SQL file itself using {{ config(materialized = 'stage2table', ...) }}.

{{
    config(
        materialized="stage2table",
        url="s3://" ~ var("s3_bucket") ~ "/cricsheet/all_player_data/",
        file_format="(TYPE = CSV SKIP_HEADER = 1 TRIM_SPACE = TRUE ESCAPE_UNENCLOSED_FIELD = NONE)",
        mode="INSERT",
        tags=["source", "cricsheet"],
        pre_hook="{{ delete_data('FILE_LAST_MODIFIED_DT', var('batch_cycle_date'), this) }}",
    )
}}

Contributions

Contributions to this package are welcomed. Please create issues for bugs or feature requests for enhancement ideas or PRs for any enhancement contributions.