-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add relation macros and add relation renameable/replaceable config (#603
) * add relation macros and add relation renameable/replaceable config * formatting cleanup * PR fixes * make tables irreplaceable
- Loading branch information
1 parent
9bb3883
commit 56af912
Showing
12 changed files
with
189 additions
and
25 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
kind: Fixes | ||
body: use get_replace_sql in redshift__get_alter_materialized_view_as_sql | ||
kind: Features | ||
body: use get_replace_sql in redshift__get_alter_materialized_view_as_sql, avoid renaming materialized views with custom table.sql and view.sql | ||
time: 2023-09-12T13:33:27.451042-07:00 | ||
custom: | ||
Author: colin-rogers-dbt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{% materialization table, adapter='redshift' %} | ||
|
||
{%- set existing_relation = load_cached_relation(this) -%} | ||
{%- set target_relation = this.incorporate(type='table') %} | ||
{%- set intermediate_relation = make_intermediate_relation(target_relation) -%} | ||
-- the intermediate_relation should not already exist in the database; get_relation | ||
-- will return None in that case. Otherwise, we get a relation that we can drop | ||
-- later, before we try to use this name for the current operation | ||
{%- set preexisting_intermediate_relation = load_cached_relation(intermediate_relation) -%} | ||
/* | ||
See ../view/view.sql for more information about this relation. | ||
*/ | ||
{%- set backup_relation_type = 'table' if existing_relation is none else existing_relation.type -%} | ||
{%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%} | ||
-- as above, the backup_relation should not already exist | ||
{%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%} | ||
-- grab current tables grants config for comparision later on | ||
{% set grant_config = config.get('grants') %} | ||
|
||
-- drop the temp relations if they exist already in the database | ||
{{ drop_relation_if_exists(preexisting_intermediate_relation) }} | ||
{{ drop_relation_if_exists(preexisting_backup_relation) }} | ||
|
||
{{ run_hooks(pre_hooks, inside_transaction=False) }} | ||
|
||
-- `BEGIN` happens here: | ||
{{ run_hooks(pre_hooks, inside_transaction=True) }} | ||
|
||
-- build model | ||
{% call statement('main') -%} | ||
{{ get_create_table_as_sql(False, intermediate_relation, sql) }} | ||
{%- endcall %} | ||
|
||
-- cleanup | ||
{% if existing_relation is not none %} | ||
/* Do the equivalent of rename_if_exists. 'existing_relation' could have been dropped | ||
since the variable was first set. */ | ||
{% set existing_relation = load_cached_relation(existing_relation) %} | ||
{% if existing_relation is not none %} | ||
{% if existing_relation.can_be_renamed %} | ||
{{ adapter.rename_relation(existing_relation, backup_relation) }} | ||
{% else %} | ||
{{ drop_relation_if_exists(existing_relation) }} | ||
{% endif %} | ||
{% endif %} | ||
{% endif %} | ||
|
||
|
||
{{ adapter.rename_relation(intermediate_relation, target_relation) }} | ||
|
||
{% do create_indexes(target_relation) %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %} | ||
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} | ||
|
||
{% do persist_docs(target_relation, model) %} | ||
|
||
-- `COMMIT` happens here | ||
{{ adapter.commit() }} | ||
|
||
-- finally, drop the existing/backup relation after the commit | ||
{{ drop_relation_if_exists(backup_relation) }} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
{% endmaterialization %} |
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,77 @@ | ||
{%- materialization view, adapter='redshift' -%} | ||
|
||
{%- set existing_relation = load_cached_relation(this) -%} | ||
{%- set target_relation = this.incorporate(type='view') -%} | ||
{%- set intermediate_relation = make_intermediate_relation(target_relation) -%} | ||
|
||
-- the intermediate_relation should not already exist in the database; get_relation | ||
-- will return None in that case. Otherwise, we get a relation that we can drop | ||
-- later, before we try to use this name for the current operation | ||
{%- set preexisting_intermediate_relation = load_cached_relation(intermediate_relation) -%} | ||
/* | ||
This relation (probably) doesn't exist yet. If it does exist, it's a leftover from | ||
a previous run, and we're going to try to drop it immediately. At the end of this | ||
materialization, we're going to rename the "existing_relation" to this identifier, | ||
and then we're going to drop it. In order to make sure we run the correct one of: | ||
- drop view ... | ||
- drop table ... | ||
We need to set the type of this relation to be the type of the existing_relation, if it exists, | ||
or else "view" as a sane default if it does not. Note that if the existing_relation does not | ||
exist, then there is nothing to move out of the way and subsequentally drop. In that case, | ||
this relation will be effectively unused. | ||
*/ | ||
{%- set backup_relation_type = 'view' if existing_relation is none else existing_relation.type -%} | ||
{%- set backup_relation = make_backup_relation(target_relation, backup_relation_type) -%} | ||
-- as above, the backup_relation should not already exist | ||
{%- set preexisting_backup_relation = load_cached_relation(backup_relation) -%} | ||
-- grab current tables grants config for comparision later on | ||
{% set grant_config = config.get('grants') %} | ||
|
||
{{ run_hooks(pre_hooks, inside_transaction=False) }} | ||
|
||
-- drop the temp relations if they exist already in the database | ||
{{ drop_relation_if_exists(preexisting_intermediate_relation) }} | ||
{{ drop_relation_if_exists(preexisting_backup_relation) }} | ||
|
||
-- `BEGIN` happens here: | ||
{{ run_hooks(pre_hooks, inside_transaction=True) }} | ||
|
||
-- build model | ||
{% call statement('main') -%} | ||
{{ get_create_view_as_sql(intermediate_relation, sql) }} | ||
{%- endcall %} | ||
|
||
-- cleanup | ||
-- move the existing view out of the way | ||
{% if existing_relation is not none %} | ||
/* Do the equivalent of rename_if_exists. 'existing_relation' could have been dropped | ||
since the variable was first set. */ | ||
{% set existing_relation = load_cached_relation(existing_relation) %} | ||
{% if existing_relation is not none %} | ||
{% if existing_relation.can_be_renamed %} | ||
{{ adapter.rename_relation(existing_relation, backup_relation) }} | ||
{% else %} | ||
{{ drop_relation_if_exists(existing_relation) }} | ||
{% endif %} | ||
{% endif %} | ||
{% endif %} | ||
|
||
{{ adapter.rename_relation(intermediate_relation, target_relation) }} | ||
|
||
{% set should_revoke = should_revoke(existing_relation, full_refresh_mode=True) %} | ||
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} | ||
|
||
{% do persist_docs(target_relation, model) %} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=True) }} | ||
|
||
{{ adapter.commit() }} | ||
|
||
{{ drop_relation_if_exists(backup_relation) }} | ||
|
||
{{ run_hooks(post_hooks, inside_transaction=False) }} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{%- endmaterialization -%} |
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
dbt/include/redshift/macros/relations/materialized_view/_replace.sql
This file was deleted.
Oops, something went wrong.
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 redshift__drop_table(relation) -%} | ||
drop table if exists {{ relation }} cascade | ||
{%- 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro redshift__get_rename_table_sql(relation, new_name) %} | ||
alter table {{ relation }} rename to {{ new_name }} | ||
{% 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{%- macro redshift__drop_view(relation) -%} | ||
drop view if exists {{ relation }} cascade | ||
{%- 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro redshift__get_rename_view_sql(relation, new_name) %} | ||
alter view {{ relation }} rename to {{ new_name }} | ||
{% 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% macro redshift__get_replace_view_sql(relation, sql) -%} | ||
|
||
{%- set binding = config.get('bind', default=True) -%} | ||
|
||
{% set bind_qualifier = '' if binding else 'with no schema binding' %} | ||
{%- set sql_header = config.get('sql_header', none) -%} | ||
|
||
{{ sql_header if sql_header is not none }} | ||
|
||
create or replace view {{ relation }} | ||
{%- set contract_config = config.get('contract') -%} | ||
{%- if contract_config.enforced -%} | ||
{{ get_assert_columns_equivalent(sql) }} | ||
{%- endif %} as ( | ||
{{ sql }} | ||
) {{ bind_qualifier }}; | ||
|
||
{%- 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