Created
October 30, 2023 17:15
-
-
Save Victoriapm/77e328c530c14a5984b733a521f7b7d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- dbt run-operation drop_pre_defer --args '{tables_to_drop: [inc_orders,dim_customers]}' | |
| {% macro drop_pre_defer(models_to_drop) %} | |
| {% if execute %} | |
| {% for model_to_drop in models_to_drop %} | |
| {%- for model in graph.nodes.values() | selectattr("name", "equalto", model_to_drop) -%} | |
| {% set query %} | |
| drop {{model.config.materialized}} if exists {{target.database}}.{{target.schema}}.{{model_to_drop}}; | |
| {% endset %} | |
| {% do run_query(query) %} | |
| {{ dbt_utils.log_info("Drop materialized model succesfully: " ~ model_to_drop) }} | |
| {% endfor %} | |
| {% endfor %} | |
| {% endif %} | |
| {% endmacro %} | |
| -- Aux macro to automatically select all inmediate parents of the model | |
| -- we want to run deferring to prod (model_to_run) | |
| {% macro select_drop_pre_defer(model_to_run) %} | |
| {% set models_to_drop = [] %} | |
| {%- for model in graph.nodes.values() | selectattr("name", "equalto", model_to_run) -%} | |
| {{ dbt_utils.log_info("Found selected model " ~ model.name ~ ", will look for refs") }} | |
| {%- for model_ref in model.refs -%} | |
| {{ dbt_utils.log_info("Found model ref: " ~ model_ref.name) }} | |
| {{ models_to_drop.append(model_ref.name) }} | |
| {%- endfor -%} | |
| {%- endfor -%} | |
| {{ return(models_to_drop) }} | |
| {% endmacro %} | |
| -- dbt run-operation automate_drop_pre_defer --args '{model_to_run: dim_customers}' | |
| -- Macro that from the model we want to run, deferring to prod, will search | |
| -- for all immediate parents and will drop those views/tables | |
| {% macro automate_drop_pre_defer(model_to_run) %} | |
| {% if execute %} | |
| {% set models_to_drop = select_drop_pre_defer(model_to_run) %} | |
| {{ dbt_utils.log_info("Found parent models to drop: " ~ models_to_drop) }} | |
| {{ drop_pre_defer(models_to_drop) }} | |
| {% endif %} | |
| {% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment