forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADAP-814: Add support for replacing materialized views with tables/vi…
…ews and vice versa (dbt-labs#8449) * first draft of adding in table - materialized view swap * table/view/materialized view can all replace each other * update renameable relations to a config * migrate relations macros from `macros/adapters/relations` to `macros/relations` so that generics are close to the relation specific macros that they reference; also aligns with adapter macro files structure, to look more familiar * move drop macro to drop macro file * align the behavior of get_drop_sql and drop_relation, adopt existing default from drop_relation * add explicit ddl for drop statements instead of inheriting the default from dbt-core * update replace macro dependent macros to align with naming standards * update type for mashumaro, update related test
- Loading branch information
1 parent
e5e1a27
commit adfa322
Showing
32 changed files
with
265 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: Add support for swapping materialized views with tables/views and vice versa | ||
time: 2023-08-17T18:57:39.01958-04:00 | ||
custom: | ||
Author: mikealfare | ||
Issue: "8449" |
This file contains 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
This file contains 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
This file contains 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
4 changes: 2 additions & 2 deletions
4
...dbt/include/global_project/macros/materializations/models/view/create_or_replace_view.sql
This file contains 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
17 changes: 17 additions & 0 deletions
17
core/dbt/include/global_project/macros/relations/create.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{%- macro get_create_sql(relation, sql) -%} | ||
{{- log('Applying CREATE to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_create_sql', 'dbt')(relation, sql) -}} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro default__get_create_sql(relation, sql) -%} | ||
|
||
{%- if relation.is_materialized_view -%} | ||
{{ get_create_materialized_view_as_sql(relation, sql) }} | ||
|
||
{%- else -%} | ||
{{- exceptions.raise_compiler_error("`get_create_sql` has not been implemented for: " ~ relation.type ) -}} | ||
|
||
{%- endif -%} | ||
|
||
{%- endmacro -%} |
17 changes: 17 additions & 0 deletions
17
core/dbt/include/global_project/macros/relations/create_backup.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{%- macro get_create_backup_sql(relation) -%} | ||
{{- log('Applying CREATE BACKUP to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_create_backup_sql', 'dbt')(relation) -}} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro default__get_create_backup_sql(relation) -%} | ||
|
||
-- get the standard backup name | ||
{% set backup_relation = make_backup_relation(relation, relation.type) %} | ||
|
||
-- drop any pre-existing backup | ||
{{ get_drop_sql(backup_relation) }}; | ||
|
||
{{ get_rename_sql(relation, backup_relation.identifier) }} | ||
|
||
{%- endmacro -%} |
17 changes: 17 additions & 0 deletions
17
core/dbt/include/global_project/macros/relations/create_intermediate.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{%- macro get_create_intermediate_sql(relation, sql) -%} | ||
{{- log('Applying CREATE INTERMEDIATE to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_create_intermediate_sql', 'dbt')(relation, sql) -}} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro default__get_create_intermediate_sql(relation, sql) -%} | ||
|
||
-- get the standard intermediate name | ||
{% set intermediate_relation = make_intermediate_relation(relation) %} | ||
|
||
-- drop any pre-existing intermediate | ||
{{ get_drop_sql(intermediate_relation) }}; | ||
|
||
{{ get_create_sql(intermediate_relation, sql) }} | ||
|
||
{%- endmacro -%} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
{%- macro get_drop_sql(relation) -%} | ||
{{- log('Applying DROP to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_drop_sql', 'dbt')(relation) -}} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro default__get_drop_sql(relation) -%} | ||
|
||
{%- if relation.is_view -%} | ||
{{ drop_view(relation) }} | ||
|
||
{%- elif relation.is_table -%} | ||
{{ drop_table(relation) }} | ||
|
||
{%- elif relation.is_materialized_view -%} | ||
{{ drop_materialized_view(relation) }} | ||
|
||
{%- else -%} | ||
drop {{ relation.type }} if exists {{ relation }} cascade | ||
|
||
{%- endif -%} | ||
|
||
{%- endmacro -%} | ||
|
||
|
||
{% 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 -%} | ||
{{ get_drop_sql(relation) }} | ||
{%- endcall %} | ||
{% endmacro %} | ||
|
||
|
||
{% macro drop_relation_if_exists(relation) %} | ||
{% if relation is not none %} | ||
{{ adapter.drop_relation(relation) }} | ||
{% endif %} | ||
{% endmacro %} |
14 changes: 14 additions & 0 deletions
14
core/dbt/include/global_project/macros/relations/drop_backup.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{%- macro get_drop_backup_sql(relation) -%} | ||
{{- log('Applying DROP BACKUP to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_drop_backup_sql', 'dbt')(relation) -}} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro default__get_drop_backup_sql(relation) -%} | ||
|
||
-- get the standard backup name | ||
{% set backup_relation = make_backup_relation(relation, relation.type) %} | ||
|
||
{{ get_drop_sql(backup_relation) }} | ||
|
||
{%- endmacro -%} |
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion
1
...rs/relations/materialized_view/create.sql → ...os/relations/materialized_view/create.sql
This file contains 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
This file contains 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
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
core/dbt/include/global_project/macros/relations/materialized_view/rename.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% macro get_rename_materialized_view_sql(relation, new_name) %} | ||
{{- adapter.dispatch('get_rename_materialized_view_sql', 'dbt')(relation, new_name) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_rename_materialized_view_sql(relation, new_name) %} | ||
{{ exceptions.raise_compiler_error( | ||
"`get_rename_materialized_view_sql` has not been implemented for this adapter." | ||
) }} | ||
{% endmacro %} |
25 changes: 25 additions & 0 deletions
25
core/dbt/include/global_project/macros/relations/rename.sql
This file contains 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
14 changes: 14 additions & 0 deletions
14
core/dbt/include/global_project/macros/relations/rename_intermediate.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{%- macro get_rename_intermediate_sql(relation) -%} | ||
{{- log('Applying RENAME INTERMEDIATE to: ' ~ relation) -}} | ||
{{- adapter.dispatch('get_rename_intermediate_sql', 'dbt')(relation) -}} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro default__get_rename_intermediate_sql(relation) -%} | ||
|
||
-- get the standard intermediate name | ||
{% set intermediate_relation = make_intermediate_relation(relation) %} | ||
|
||
{{ get_rename_sql(intermediate_relation, relation.identifier) }} | ||
|
||
{%- endmacro -%} |
35 changes: 35 additions & 0 deletions
35
core/dbt/include/global_project/macros/relations/replace.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{% macro get_replace_sql(existing_relation, target_relation, sql) %} | ||
{{- log('Applying REPLACE to: ' ~ existing_relation) -}} | ||
{{- adapter.dispatch('get_replace_sql', 'dbt')(existing_relation, target_relation, sql) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_replace_sql(existing_relation, target_relation, sql) %} | ||
|
||
{# /* create target_relation as an intermediate relation, then swap it out with the existing one using a backup */ #} | ||
{%- if target_relation.can_be_renamed and existing_relation.can_be_renamed -%} | ||
{{ get_create_intermediate_sql(target_relation, sql) }}; | ||
{{ get_create_backup_sql(existing_relation) }}; | ||
{{ get_rename_intermediate_sql(target_relation) }}; | ||
{{ get_drop_backup_sql(existing_relation) }} | ||
|
||
{# /* create target_relation as an intermediate relation, then swap it out with the existing one using drop */ #} | ||
{%- elif target_relation.can_be_renamed -%} | ||
{{ get_create_intermediate_sql(target_relation, sql) }}; | ||
{{ get_drop_sql(existing_relation) }}; | ||
{{ get_rename_intermediate_sql(target_relation) }} | ||
|
||
{# /* create target_relation in place by first backing up the existing relation */ #} | ||
{%- elif existing_relation.can_be_renamed -%} | ||
{{ get_create_backup_sql(existing_relation) }}; | ||
{{ get_create_sql(target_relation, sql) }}; | ||
{{ get_drop_backup_sql(existing_relation) }} | ||
|
||
{# /* no renaming is allowed, so just drop and create */ #} | ||
{%- else -%} | ||
{{ get_drop_sql(existing_relation) }}; | ||
{{ get_create_sql(target_relation, sql) }} | ||
|
||
{%- endif -%} | ||
|
||
{% endmacro %} |
This file contains 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
10 changes: 10 additions & 0 deletions
10
core/dbt/include/global_project/macros/relations/table/rename.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% macro get_rename_table_sql(relation, new_name) %} | ||
{{- adapter.dispatch('get_rename_table_sql', 'dbt')(relation, new_name) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_rename_table_sql(relation, new_name) %} | ||
{{ exceptions.raise_compiler_error( | ||
"`get_rename_table_sql` has not been implemented for this adapter." | ||
) }} | ||
{% endmacro %} |
This file contains 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
10 changes: 10 additions & 0 deletions
10
core/dbt/include/global_project/macros/relations/view/rename.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% macro get_rename_view_sql(relation, new_name) %} | ||
{{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} | ||
{% endmacro %} | ||
|
||
|
||
{% macro default__get_rename_view_sql(relation, new_name) %} | ||
{{ exceptions.raise_compiler_error( | ||
"`get_rename_view_sql` has not been implemented for this adapter." | ||
) }} | ||
{% endmacro %} |
This file contains 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
This file contains 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
3 changes: 3 additions & 0 deletions
3
plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/drop.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro postgres__drop_materialized_view(relation) -%} | ||
drop materialized view if exists {{ relation }} cascade | ||
{%- endmacro %} |
3 changes: 3 additions & 0 deletions
3
plugins/postgres/dbt/include/postgres/macros/relations/table/drop.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro postgres__drop_table(relation) -%} | ||
drop table if exists {{ relation }} cascade | ||
{%- endmacro %} |
3 changes: 3 additions & 0 deletions
3
plugins/postgres/dbt/include/postgres/macros/relations/table/rename.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro postgres__get_rename_table_sql(relation, new_name) %} | ||
alter table {{ relation }} rename to {{ new_name }} | ||
{% endmacro %} |
3 changes: 3 additions & 0 deletions
3
plugins/postgres/dbt/include/postgres/macros/relations/view/drop.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro postgres__drop_view(relation) -%} | ||
drop view if exists {{ relation }} cascade | ||
{%- endmacro %} |
3 changes: 3 additions & 0 deletions
3
plugins/postgres/dbt/include/postgres/macros/relations/view/rename.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro postgres__get_rename_view_sql(relation, new_name) %} | ||
alter view {{ relation }} rename to {{ new_name }} | ||
{% endmacro %} |
Oops, something went wrong.