Skip to content

Instantly share code, notes, and snippets.

@sungchun12
Last active August 22, 2024 15:50
Show Gist options
  • Save sungchun12/1124d7d7a7708ab02241d8a27e021762 to your computer and use it in GitHub Desktop.
Save sungchun12/1124d7d7a7708ab02241d8a27e021762 to your computer and use it in GitHub Desktop.
dbt staging to production gatekeeper macro to prevent problems happening in production after it's too late. Demo Video: https://www.loom.com/share/bcfd2cf3b4b5471683bfc5b24587db3d
{% macro clone_from_to(from, to) %}
{% set sql -%}
create schema if not exists {{ target.database }}.{{ to }} clone {{ from }};
{%- endset %}
{{ dbt_utils.log_info("Cloning tables/views from schema [" ~ from ~ "] into target schema [" ~ to ~ "]") }}
{% do run_query(sql) %}
{{ dbt_utils.log_info("Cloned tables/views from schema [" ~ from ~ "] into target schema [" ~ to ~ "]") }}
{% endmacro %}
{% macro destroy_to_env(to) %}
{% set sql -%}
drop schema if exists {{ target.database }}.{{ to }} cascade;
{%- endset %}
{{ dbt_utils.log_info("Dropping tables/views in target schema [" ~ to ~ "]") }}
{% do run_query(sql) %}
{{ dbt_utils.log_info("Dropped tables/views in target schema [" ~ to ~ "]") }}
{% endmacro %}
{% macro zero_copy_clone_pre_prod(from, to) %}
{#-
This macro destroys your current development environment, and recreates it by cloning from prod.
To run it:
$ dbt run-operation zero_copy_clone_pre_prod --args '{from: dbt_sung_staging, to: dbt_sung_production}'
-#}
{% if target.name == 'default' %}
{{ destroy_to_env(to) }}
{{ clone_from_to(from, to) }}
{% else %}
{{ dbt_utils.log_info("No-op: your current target is " ~ target.name ~ ". This macro only works for a default target.") }}
{% endif %}
{% endmacro %}
@sungchun12
Copy link
Author

Put this in your macros/ folder!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment