diff --git a/.changes/unreleased/Under the Hood-20230821-134801.yaml b/.changes/unreleased/Under the Hood-20230821-134801.yaml new file mode 100644 index 00000000000..eb2c09a2486 --- /dev/null +++ b/.changes/unreleased/Under the Hood-20230821-134801.yaml @@ -0,0 +1,7 @@ +kind: Under the Hood +body: 'Re-organize jinja macros: relation-specific in /macros/adapters/relations/, + relation agnostic in /macros/relations' +time: 2023-08-21T13:48:01.474731-04:00 +custom: + Author: mikealfare + Issue: "8449" diff --git a/core/dbt/include/global_project/macros/adapters/drop_relation.sql b/core/dbt/include/global_project/macros/adapters/drop_relation.sql deleted file mode 100644 index bd254c78d51..00000000000 --- a/core/dbt/include/global_project/macros/adapters/drop_relation.sql +++ /dev/null @@ -1,44 +0,0 @@ -{% macro drop_relation(relation) -%} - {{ return(adapter.dispatch('drop_relation', 'dbt')(relation)) }} -{% endmacro %} - -{% macro default__drop_relation(relation) -%} - {% call statement('drop_relation', auto_begin=False) -%} - {%- if relation.is_table -%} - {{- drop_table(relation) -}} - {%- elif relation.is_view -%} - {{- drop_view(relation) -}} - {%- elif relation.is_materialized_view -%} - {{- drop_materialized_view(relation) -}} - {%- else -%} - drop {{ relation.type }} if exists {{ relation }} cascade - {%- endif -%} - {%- endcall %} -{% endmacro %} - - -{% macro drop_table(relation) -%} - {{ return(adapter.dispatch('drop_table', 'dbt')(relation)) }} -{%- endmacro %} - -{% macro default__drop_table(relation) -%} - drop table if exists {{ relation }} cascade -{%- endmacro %} - - -{% macro drop_view(relation) -%} - {{ return(adapter.dispatch('drop_view', 'dbt')(relation)) }} -{%- endmacro %} - -{% macro default__drop_view(relation) -%} - drop view if exists {{ relation }} cascade -{%- endmacro %} - - -{% macro drop_materialized_view(relation) -%} - {{ return(adapter.dispatch('drop_materialized_view', 'dbt')(relation)) }} -{%- endmacro %} - -{% macro default__drop_materialized_view(relation) -%} - drop materialized view if exists {{ relation }} cascade -{%- endmacro %} diff --git a/core/dbt/include/global_project/macros/adapters/relation.sql b/core/dbt/include/global_project/macros/adapters/relation.sql index f0dde7f20f0..228bb03dcc4 100644 --- a/core/dbt/include/global_project/macros/adapters/relation.sql +++ b/core/dbt/include/global_project/macros/adapters/relation.sql @@ -43,18 +43,6 @@ {% endmacro %} -{% macro rename_relation(from_relation, to_relation) -%} - {{ return(adapter.dispatch('rename_relation', 'dbt')(from_relation, to_relation)) }} -{% endmacro %} - -{% macro default__rename_relation(from_relation, to_relation) -%} - {% set target_name = adapter.quote_as_configured(to_relation.identifier, 'identifier') %} - {% call statement('rename_relation') -%} - alter table {{ from_relation }} rename to {{ target_name }} - {%- endcall %} -{% endmacro %} - - {% macro get_or_create_relation(database, schema, identifier, type) -%} {{ return(adapter.dispatch('get_or_create_relation', 'dbt')(database, schema, identifier, type)) }} {% endmacro %} diff --git a/core/dbt/include/global_project/macros/materializations/models/materialized_view/replace_materialized_view.sql b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/_replace.sql similarity index 72% rename from core/dbt/include/global_project/macros/materializations/models/materialized_view/replace_materialized_view.sql rename to core/dbt/include/global_project/macros/adapters/relations/materialized_view/_replace.sql index 43319c5cc1b..5f227b864e2 100644 --- a/core/dbt/include/global_project/macros/materializations/models/materialized_view/replace_materialized_view.sql +++ b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/_replace.sql @@ -1,3 +1,8 @@ +{# /* +This only exists for backwards compatibility for 1.6.0. In later versions, the general `get_replace_sql` +macro is called as replace is inherently not limited to a single relation (it takes in two relations). +*/ #} + {% macro get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %} {{- log('Applying REPLACE to: ' ~ relation) -}} {{- adapter.dispatch('get_replace_materialized_view_as_sql', 'dbt')(relation, sql, existing_relation, backup_relation, intermediate_relation) -}} diff --git a/core/dbt/include/global_project/macros/materializations/models/materialized_view/alter_materialized_view.sql b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/alter.sql similarity index 100% rename from core/dbt/include/global_project/macros/materializations/models/materialized_view/alter_materialized_view.sql rename to core/dbt/include/global_project/macros/adapters/relations/materialized_view/alter.sql diff --git a/core/dbt/include/global_project/macros/materializations/models/materialized_view/create_materialized_view.sql b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/create.sql similarity index 68% rename from core/dbt/include/global_project/macros/materializations/models/materialized_view/create_materialized_view.sql rename to core/dbt/include/global_project/macros/adapters/relations/materialized_view/create.sql index 4b2ebeb3aa1..8b479964e6f 100644 --- a/core/dbt/include/global_project/macros/materializations/models/materialized_view/create_materialized_view.sql +++ b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/create.sql @@ -5,5 +5,7 @@ {% macro default__get_create_materialized_view_as_sql(relation, sql) -%} - {{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }} + {{ exceptions.raise_compiler_error( + "`get_create_materialized_view_as_sql` has not been implemented for this adapter." + ) }} {% endmacro %} diff --git a/core/dbt/include/global_project/macros/adapters/relations/materialized_view/drop.sql b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/drop.sql new file mode 100644 index 00000000000..d176d2b759d --- /dev/null +++ b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/drop.sql @@ -0,0 +1,13 @@ +{# /* +This was already implemented. Instead of creating a new macro that aligns with the standard, +this was reused and the default was maintained. This gets called by `drop_relation`, which +actually executes the drop, and `get_drop_sql`, which returns the template. +*/ #} + +{% macro drop_materialized_view(relation) -%} + {{ return(adapter.dispatch('drop_materialized_view', 'dbt')(relation)) }} +{%- endmacro %} + +{% macro default__drop_materialized_view(relation) -%} + drop materialized view if exists {{ relation }} cascade +{%- endmacro %} diff --git a/core/dbt/include/global_project/macros/materializations/models/materialized_view/refresh_materialized_view.sql b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/refresh.sql similarity index 69% rename from core/dbt/include/global_project/macros/materializations/models/materialized_view/refresh_materialized_view.sql rename to core/dbt/include/global_project/macros/adapters/relations/materialized_view/refresh.sql index 16345138593..d6b2732107a 100644 --- a/core/dbt/include/global_project/macros/materializations/models/materialized_view/refresh_materialized_view.sql +++ b/core/dbt/include/global_project/macros/adapters/relations/materialized_view/refresh.sql @@ -5,5 +5,5 @@ {% macro default__refresh_materialized_view(relation) %} - {{ exceptions.raise_compiler_error("Materialized views have not been implemented for this adapter.") }} + {{ exceptions.raise_compiler_error("`refresh_materialized_view` has not been implemented for this adapter.") }} {% endmacro %} diff --git a/core/dbt/include/global_project/macros/adapters/relations/table/drop.sql b/core/dbt/include/global_project/macros/adapters/relations/table/drop.sql new file mode 100644 index 00000000000..152676e3caa --- /dev/null +++ b/core/dbt/include/global_project/macros/adapters/relations/table/drop.sql @@ -0,0 +1,13 @@ +{# /* +This was already implemented. Instead of creating a new macro that aligns with the standard, +this was reused and the default was maintained. This gets called by `drop_relation`, which +actually executes the drop, and `get_drop_sql`, which returns the template. +*/ #} + +{% macro drop_table(relation) -%} + {{ return(adapter.dispatch('drop_table', 'dbt')(relation)) }} +{%- endmacro %} + +{% macro default__drop_table(relation) -%} + drop table if exists {{ relation }} cascade +{%- endmacro %} diff --git a/core/dbt/include/global_project/macros/adapters/relations/view/drop.sql b/core/dbt/include/global_project/macros/adapters/relations/view/drop.sql new file mode 100644 index 00000000000..46691b9e429 --- /dev/null +++ b/core/dbt/include/global_project/macros/adapters/relations/view/drop.sql @@ -0,0 +1,13 @@ +{# /* +This was already implemented. Instead of creating a new macro that aligns with the standard, +this was reused and the default was maintained. This gets called by `drop_relation`, which +actually executes the drop, and `get_drop_sql`, which returns the template. +*/ #} + +{% macro drop_view(relation) -%} + {{ return(adapter.dispatch('drop_view', 'dbt')(relation)) }} +{%- endmacro %} + +{% macro default__drop_view(relation) -%} + drop view if exists {{ relation }} cascade +{%- endmacro %} diff --git a/core/dbt/include/global_project/macros/relations/drop.sql b/core/dbt/include/global_project/macros/relations/drop.sql new file mode 100644 index 00000000000..d02ad9327d2 --- /dev/null +++ b/core/dbt/include/global_project/macros/relations/drop.sql @@ -0,0 +1,17 @@ +{% macro drop_relation(relation) -%} + {{ return(adapter.dispatch('drop_relation', 'dbt')(relation)) }} +{% endmacro %} + +{% macro default__drop_relation(relation) -%} + {% call statement('drop_relation', auto_begin=False) -%} + {%- if relation.is_table -%} + {{- drop_table(relation) -}} + {%- elif relation.is_view -%} + {{- drop_view(relation) -}} + {%- elif relation.is_materialized_view -%} + {{- drop_materialized_view(relation) -}} + {%- else -%} + drop {{ relation.type }} if exists {{ relation }} cascade + {%- endif -%} + {%- endcall %} +{% endmacro %} diff --git a/core/dbt/include/global_project/macros/relations/rename.sql b/core/dbt/include/global_project/macros/relations/rename.sql new file mode 100644 index 00000000000..7d6c979cfc1 --- /dev/null +++ b/core/dbt/include/global_project/macros/relations/rename.sql @@ -0,0 +1,10 @@ +{% macro rename_relation(from_relation, to_relation) -%} + {{ return(adapter.dispatch('rename_relation', 'dbt')(from_relation, to_relation)) }} +{% endmacro %} + +{% macro default__rename_relation(from_relation, to_relation) -%} + {% set target_name = adapter.quote_as_configured(to_relation.identifier, 'identifier') %} + {% call statement('rename_relation') -%} + alter table {{ from_relation }} rename to {{ target_name }} + {%- endcall %} +{% endmacro %} diff --git a/plugins/postgres/dbt/include/postgres/macros/materializations/materialized_view.sql b/plugins/postgres/dbt/include/postgres/macros/materializations/materialized_view.sql index 6e66e4bcd2c..77457d1f7e9 100644 --- a/plugins/postgres/dbt/include/postgres/macros/materializations/materialized_view.sql +++ b/plugins/postgres/dbt/include/postgres/macros/materializations/materialized_view.sql @@ -1,84 +1,5 @@ -{% macro postgres__get_alter_materialized_view_as_sql( - relation, - configuration_changes, - sql, - existing_relation, - backup_relation, - intermediate_relation -) %} - - -- apply a full refresh immediately if needed - {% if configuration_changes.requires_full_refresh %} - - {{ get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) }} - - -- otherwise apply individual changes as needed - {% else %} - - {{ postgres__update_indexes_on_materialized_view(relation, configuration_changes.indexes) }} - - {%- endif -%} - -{% endmacro %} - - -{% macro postgres__get_create_materialized_view_as_sql(relation, sql) %} - create materialized view if not exists {{ relation }} as {{ sql }}; - - {% for _index_dict in config.get('indexes', []) -%} - {{- get_create_index_sql(relation, _index_dict) -}} - {%- endfor -%} - -{% endmacro %} - - -{% macro postgres__get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %} - {{- get_create_materialized_view_as_sql(intermediate_relation, sql) -}} - - {% if existing_relation is not none %} - alter materialized view {{ existing_relation }} rename to {{ backup_relation.include(database=False, schema=False) }}; - {% endif %} - - alter materialized view {{ intermediate_relation }} rename to {{ relation.include(database=False, schema=False) }}; - -{% endmacro %} - - {% macro postgres__get_materialized_view_configuration_changes(existing_relation, new_config) %} {% set _existing_materialized_view = postgres__describe_materialized_view(existing_relation) %} {% set _configuration_changes = existing_relation.get_materialized_view_config_change_collection(_existing_materialized_view, new_config) %} {% do return(_configuration_changes) %} {% endmacro %} - - -{% macro postgres__refresh_materialized_view(relation) %} - refresh materialized view {{ relation }} -{% endmacro %} - - -{%- macro postgres__update_indexes_on_materialized_view(relation, index_changes) -%} - {{- log("Applying UPDATE INDEXES to: " ~ relation) -}} - - {%- for _index_change in index_changes -%} - {%- set _index = _index_change.context -%} - - {%- if _index_change.action == "drop" -%} - - {{ postgres__get_drop_index_sql(relation, _index.name) }}; - - {%- elif _index_change.action == "create" -%} - - {{ postgres__get_create_index_sql(relation, _index.as_node_config) }} - - {%- endif -%} - - {%- endfor -%} - -{%- endmacro -%} - - -{% macro postgres__describe_materialized_view(relation) %} - -- for now just get the indexes, we don't need the name or the query yet - {% set _indexes = run_query(get_show_indexes_sql(relation)) %} - {% do return({'indexes': _indexes}) %} -{% endmacro %} diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/_replace.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/_replace.sql new file mode 100644 index 00000000000..8e536756604 --- /dev/null +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/_replace.sql @@ -0,0 +1,16 @@ +{# /* +This only exists for backwards compatibility for 1.6.0. In later versions, the general `get_replace_sql` +macro is called as replace is inherently not limited to a single relation (it takes in two relations). +*/ #} + + +{% macro postgres__get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %} + {{- get_create_materialized_view_as_sql(intermediate_relation, sql) -}} + + {% if existing_relation is not none %} + alter materialized view {{ existing_relation }} rename to {{ backup_relation.include(database=False, schema=False) }}; + {% endif %} + + alter materialized view {{ intermediate_relation }} rename to {{ relation.include(database=False, schema=False) }}; + +{% endmacro %} diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql new file mode 100644 index 00000000000..0dd5189783d --- /dev/null +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql @@ -0,0 +1,43 @@ +{% macro postgres__get_alter_materialized_view_as_sql( + relation, + configuration_changes, + sql, + existing_relation, + backup_relation, + intermediate_relation +) %} + + -- apply a full refresh immediately if needed + {% if configuration_changes.requires_full_refresh %} + + {{ get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) }} + + -- otherwise apply individual changes as needed + {% else %} + + {{ postgres__update_indexes_on_materialized_view(relation, configuration_changes.indexes) }} + + {%- endif -%} + +{% endmacro %} + + +{%- macro postgres__update_indexes_on_materialized_view(relation, index_changes) -%} + {{- log("Applying UPDATE INDEXES to: " ~ relation) -}} + + {%- for _index_change in index_changes -%} + {%- set _index = _index_change.context -%} + + {%- if _index_change.action == "drop" -%} + + {{ postgres__get_drop_index_sql(relation, _index.name) }}; + + {%- elif _index_change.action == "create" -%} + + {{ postgres__get_create_index_sql(relation, _index.as_node_config) }} + + {%- endif -%} + + {%- endfor -%} + +{%- endmacro -%} diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/create.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/create.sql new file mode 100644 index 00000000000..17e5cb06434 --- /dev/null +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/create.sql @@ -0,0 +1,8 @@ +{% macro postgres__get_create_materialized_view_as_sql(relation, sql) %} + create materialized view if not exists {{ relation }} as {{ sql }}; + + {% for _index_dict in config.get('indexes', []) -%} + {{- get_create_index_sql(relation, _index_dict) -}} + {%- endfor -%} + +{% endmacro %} diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/describe.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/describe.sql new file mode 100644 index 00000000000..cb133b6a8b5 --- /dev/null +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/describe.sql @@ -0,0 +1,5 @@ +{% macro postgres__describe_materialized_view(relation) %} + -- for now just get the indexes, we don't need the name or the query yet + {% set _indexes = run_query(get_show_indexes_sql(relation)) %} + {% do return({'indexes': _indexes}) %} +{% endmacro %} diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/refresh.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/refresh.sql new file mode 100644 index 00000000000..48b863e519b --- /dev/null +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/refresh.sql @@ -0,0 +1,3 @@ +{% macro postgres__refresh_materialized_view(relation) %} + refresh materialized view {{ relation }} +{% endmacro %} diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/rename.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/rename.sql new file mode 100644 index 00000000000..293ec9d1e12 --- /dev/null +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/rename.sql @@ -0,0 +1,3 @@ +{% macro postgres__get_rename_materialized_view_sql(relation, new_name) %} + alter materialized view {{ relation }} rename to {{ new_name }} +{% endmacro %}