From f9e8b16e69cda14244f98a480a7f3d81cd666bdf Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 22 May 2024 16:04:59 -0400 Subject: [PATCH 01/10] update rename macro to include fully qualified target name --- dbt/include/snowflake/macros/relations/table/rename.sql | 2 +- dbt/include/snowflake/macros/relations/view/rename.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/include/snowflake/macros/relations/table/rename.sql b/dbt/include/snowflake/macros/relations/table/rename.sql index 7b363e03d..990fe2c4b 100644 --- a/dbt/include/snowflake/macros/relations/table/rename.sql +++ b/dbt/include/snowflake/macros/relations/table/rename.sql @@ -1,3 +1,3 @@ {%- macro snowflake__get_rename_table_sql(relation, new_name) -%} - alter table {{ relation }} rename to {{ new_name }} + alter table {{ relation }} rename to {{ relation.incorporate(identifier=new_name}) }} {%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/view/rename.sql b/dbt/include/snowflake/macros/relations/view/rename.sql index 4cfd410a4..ae0b06c58 100644 --- a/dbt/include/snowflake/macros/relations/view/rename.sql +++ b/dbt/include/snowflake/macros/relations/view/rename.sql @@ -1,3 +1,3 @@ {%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - alter view {{ relation }} rename to {{ new_name }} + alter view {{ relation }} rename to {{ relation.incorporate(identifier=new_name}) }} {%- endmacro -%} From 948f21f644ccfc4b371ef29ce5bfb9ca39cf90d3 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 22 May 2024 16:06:05 -0400 Subject: [PATCH 02/10] changelog --- .changes/unreleased/Fixes-20240522-160538.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240522-160538.yaml diff --git a/.changes/unreleased/Fixes-20240522-160538.yaml b/.changes/unreleased/Fixes-20240522-160538.yaml new file mode 100644 index 000000000..4921706a9 --- /dev/null +++ b/.changes/unreleased/Fixes-20240522-160538.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: 'Rename targets for tables and views use fully qualified names' +time: 2024-05-22T16:05:38.602074-04:00 +custom: + Author: mikealfare + Issue: "1031" From f39276cc18b30c29bf9830c7b2ee549d2ee141c6 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 22 May 2024 17:36:35 -0400 Subject: [PATCH 03/10] jinja typo --- dbt/include/snowflake/macros/relations/table/rename.sql | 2 +- dbt/include/snowflake/macros/relations/view/rename.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/include/snowflake/macros/relations/table/rename.sql b/dbt/include/snowflake/macros/relations/table/rename.sql index 990fe2c4b..6287c6cab 100644 --- a/dbt/include/snowflake/macros/relations/table/rename.sql +++ b/dbt/include/snowflake/macros/relations/table/rename.sql @@ -1,3 +1,3 @@ {%- macro snowflake__get_rename_table_sql(relation, new_name) -%} - alter table {{ relation }} rename to {{ relation.incorporate(identifier=new_name}) }} + alter table {{ relation }} rename to {{ relation.incorporate(identifier=new_name) }} {%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/view/rename.sql b/dbt/include/snowflake/macros/relations/view/rename.sql index ae0b06c58..97a592cf1 100644 --- a/dbt/include/snowflake/macros/relations/view/rename.sql +++ b/dbt/include/snowflake/macros/relations/view/rename.sql @@ -1,3 +1,3 @@ {%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - alter view {{ relation }} rename to {{ relation.incorporate(identifier=new_name}) }} + alter view {{ relation }} rename to {{ relation.incorporate(identifier=new_name) }} {%- endmacro -%} From de6ab1c456b363b5643d73f4c088d38410b0560c Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 22 May 2024 18:47:55 -0400 Subject: [PATCH 04/10] update incorporate to use path dict --- dbt/include/snowflake/macros/relations/table/rename.sql | 2 +- dbt/include/snowflake/macros/relations/view/rename.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/include/snowflake/macros/relations/table/rename.sql b/dbt/include/snowflake/macros/relations/table/rename.sql index 6287c6cab..1ba5bc2ba 100644 --- a/dbt/include/snowflake/macros/relations/table/rename.sql +++ b/dbt/include/snowflake/macros/relations/table/rename.sql @@ -1,3 +1,3 @@ {%- macro snowflake__get_rename_table_sql(relation, new_name) -%} - alter table {{ relation }} rename to {{ relation.incorporate(identifier=new_name) }} + alter table {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} {%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/view/rename.sql b/dbt/include/snowflake/macros/relations/view/rename.sql index 97a592cf1..f56bb9971 100644 --- a/dbt/include/snowflake/macros/relations/view/rename.sql +++ b/dbt/include/snowflake/macros/relations/view/rename.sql @@ -1,3 +1,3 @@ {%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - alter view {{ relation }} rename to {{ relation.incorporate(identifier=new_name) }} + alter view {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} {%- endmacro -%} From 062679ffddd67da0190112cb61101921b9b1ecef Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Thu, 23 May 2024 12:29:24 -0400 Subject: [PATCH 05/10] add test cases for expected rename statements --- tests/functional/relation_tests/base.py | 61 +++++++++++++++++++ tests/functional/relation_tests/test_table.py | 18 ++++++ tests/functional/relation_tests/test_view.py | 18 ++++++ 3 files changed, 97 insertions(+) create mode 100644 tests/functional/relation_tests/base.py create mode 100644 tests/functional/relation_tests/test_table.py create mode 100644 tests/functional/relation_tests/test_view.py diff --git a/tests/functional/relation_tests/base.py b/tests/functional/relation_tests/base.py new file mode 100644 index 000000000..887bc7324 --- /dev/null +++ b/tests/functional/relation_tests/base.py @@ -0,0 +1,61 @@ +import pytest + +from dbt.tests.util import run_dbt, run_dbt_and_capture + + +SEED = """ +id +0 +1 +2 +""".strip() + + +TABLE = """ +{{ config(materialized="table") }} +select * from {{ ref('my_seed') }} +""" + + +VIEW = """ +{{ config(materialized="view") }} +select * from {{ ref('my_seed') }} +""" + + +MACRO__GET_RENAME_SQL = """ +{% macro test__get_rename_sql(database, schema, identifier, relation_type, new_name) -%} + {%- set relation = adapter.Relation.create(database=database, schema=schema, identifier=identifier, type=relation_type) -%} + {% call statement('test__get_rename_sql') -%} + {{ get_rename_sql(relation, new_name) }} + {%- endcall %} +{% endmacro %}""" + + +class RelationOperation: + @pytest.fixture(scope="class") + def seeds(self): + yield {"my_seed.csv": SEED} + + @pytest.fixture(scope="class") + def models(self): + yield { + "my_table.sql": TABLE, + "my_view.sql": VIEW, + } + + @pytest.fixture(scope="class") + def macros(self): + yield {"test__get_rename_sql.sql": MACRO__GET_RENAME_SQL} + + @pytest.fixture(scope="class", autouse=True) + def setup(self, project): + run_dbt(["seed"]) + run_dbt(["run"]) + + def assert_operation(self, project, operation, args, expected_statement): + results, logs = run_dbt_and_capture( + ["--debug", "run-operation", operation, "--args", str(args)] + ) + assert len(results) == 1 + assert expected_statement in logs diff --git a/tests/functional/relation_tests/test_table.py b/tests/functional/relation_tests/test_table.py new file mode 100644 index 000000000..20ce8e305 --- /dev/null +++ b/tests/functional/relation_tests/test_table.py @@ -0,0 +1,18 @@ +from tests.functional.relation_tests.base import RelationOperation + + +class TestTable(RelationOperation): + + def test_get_rename_table_sql(self, project): + args = { + "database": project.database, + "schema": project.test_schema, + "identifier": "my_table", + "relation_type": "table", + "new_name": "my_new_table", + } + expected_statement = ( + f"alter table {project.database}.{project.test_schema}.my_table " + f"rename to {project.database}.{project.test_schema}.my_new_table" + ) + self.assert_operation(project, "test__get_rename_sql", args, expected_statement) diff --git a/tests/functional/relation_tests/test_view.py b/tests/functional/relation_tests/test_view.py new file mode 100644 index 000000000..0f338365e --- /dev/null +++ b/tests/functional/relation_tests/test_view.py @@ -0,0 +1,18 @@ +from tests.functional.relation_tests.base import RelationOperation + + +class TestView(RelationOperation): + + def test_get_rename_view_sql(self, project): + args = { + "database": project.database, + "schema": project.test_schema, + "identifier": "my_view", + "relation_type": "view", + "new_name": "my_new_view", + } + expected_statement = ( + f"alter view {project.database}.{project.test_schema}.my_view " + f"rename to {project.database}.{project.test_schema}.my_new_view" + ) + self.assert_operation(project, "test__get_rename_sql", args, expected_statement) From 3be57166e7e9943024d4825c75ab4522c76de918 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Fri, 31 May 2024 19:40:45 -0400 Subject: [PATCH 06/10] provide implementation options --- .../macros/relations/_rename_approach_0.sql | 59 +++++++++++++++++ .../macros/relations/_rename_approach_1.sql | 56 +++++++++++++++++ .../macros/relations/_rename_approach_2.sql | 58 +++++++++++++++++ .../macros/relations/_rename_approach_3.sql | 63 +++++++++++++++++++ .../macros/relations/_rename_usage.sql | 39 ++++++++++++ 5 files changed, 275 insertions(+) create mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_0.sql create mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_1.sql create mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_2.sql create mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_3.sql create mode 100644 dbt/include/snowflake/macros/relations/_rename_usage.sql diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_0.sql b/dbt/include/snowflake/macros/relations/_rename_approach_0.sql new file mode 100644 index 000000000..3793c2e5a --- /dev/null +++ b/dbt/include/snowflake/macros/relations/_rename_approach_0.sql @@ -0,0 +1,59 @@ +/* +This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. + +Docstrings have been added to communicate _intended_ usage. They do not cover all supported usage however, as is the issue we're facing here. + +This represents the current logic flow for views (tables would be the same): +- supports fully qualified names as strings +- supports relations +- supports moving relations to another database/schema +- unintentionally moves `relation` for `dbt-snowflake` if a `use schema` or `use database` statement runs first + and `new_name` is an identifier, e.g. when calling `get_create_backup_sql()` +*/ + +------------------------------------------ dbt-adapters ------------------------------------------ +{%- macro get_rename_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` + Returns: templated string + */ + {{- log('Applying RENAME to: ' ~ relation) -}} + {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} +{%- endmacro -%} + + +{%- macro default__get_rename_sql(relation, new_name) -%} + {%- if relation.is_view -%} + {{ get_rename_view_sql(relation, new_name) }} + + {%- elif relation.is_table -%} + {{ get_rename_table_sql(relation, new_name) }} + + {%- elif relation.is_materialized_view -%} + {{ get_rename_materialized_view_sql(relation, new_name) }} + + {%- else -%} + {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} + + {%- endif -%} + +{%- endmacro -%} + + +{% macro get_rename_view_sql(relation, new_name) %} + {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} +{% endmacro %} + + +------------------------------------------ dbt-snowflake ------------------------------------------ +{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` + Returns: templated string + */ + alter view {{ relation }} rename to {{ new_name }} +{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_1.sql b/dbt/include/snowflake/macros/relations/_rename_approach_1.sql new file mode 100644 index 000000000..ee2e126b4 --- /dev/null +++ b/dbt/include/snowflake/macros/relations/_rename_approach_1.sql @@ -0,0 +1,56 @@ +/* +This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. + +Implement at the `dbt-snowflake` level in `snowflake__get_rename_x_sql()`: +- only affects `dbt-snowflake` +- removes support for fully qualified names +- removes support for relations +- removes support for moving relations to another database and/or schema +*/ + +------------------------------------------ dbt-adapters ------------------------------------------ +{%- macro get_rename_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` + Returns: templated string + */ + {{- log('Applying RENAME to: ' ~ relation) -}} + {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} +{%- endmacro -%} + + +{%- macro default__get_rename_sql(relation, new_name) -%} + {%- if relation.is_view -%} + {{ get_rename_view_sql(relation, new_name) }} + + {%- elif relation.is_table -%} + {{ get_rename_table_sql(relation, new_name) }} + + {%- elif relation.is_materialized_view -%} + {{ get_rename_materialized_view_sql(relation, new_name) }} + + {%- else -%} + {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} + + {%- endif -%} + +{%- endmacro -%} + + +{% macro get_rename_view_sql(relation, new_name) %} + {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} +{% endmacro %} + + +------------------------------------------ dbt-snowflake ------------------------------------------ +{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` + Returns: templated string + */ + alter view {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} +{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_2.sql b/dbt/include/snowflake/macros/relations/_rename_approach_2.sql new file mode 100644 index 000000000..aa0be43a0 --- /dev/null +++ b/dbt/include/snowflake/macros/relations/_rename_approach_2.sql @@ -0,0 +1,58 @@ +/* +This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. + +Implement at the `dbt-adapters` level in `get_rename_sql()`: +- affects all adapters, may require fixes for platforms that don't support renaming with a fully qualified name (unclear if this is a concern) +- removes support for fully qualified names +- still supports relations +- still supports moving relations to another database and/or schema + however this requires using a relation instead of a fully qualified name +*/ + +------------------------------------------ dbt-adapters ------------------------------------------ +{%- macro get_rename_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: Union[str, BaseRelation] - new identifier or BaseRelation instance for `relation` + Returns: templated string + */ + {{- log('Applying RENAME to: ' ~ relation) -}} + {% set new_name = relation.incorporate(path={"identifier": new_name}).render() if new_name is string else new_name.render() %} + {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} +{%- endmacro -%} + + +{%- macro default__get_rename_sql(relation, new_name) -%} + {%- if relation.is_view -%} + {{ get_rename_view_sql(relation, new_name) }} + + {%- elif relation.is_table -%} + {{ get_rename_table_sql(relation, new_name) }} + + {%- elif relation.is_materialized_view -%} + {{ get_rename_materialized_view_sql(relation, new_name) }} + + {%- else -%} + {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} + + {%- endif -%} + +{%- endmacro -%} + + +{% macro get_rename_view_sql(relation, new_name) %} + {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} +{% endmacro %} + + +------------------------------------------ dbt-snowflake ------------------------------------------ +{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new fully qualified path, for `relation` + Returns: templated string + */ + alter view {{ relation }} rename to {{ new_name }} +{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_3.sql b/dbt/include/snowflake/macros/relations/_rename_approach_3.sql new file mode 100644 index 000000000..76e120352 --- /dev/null +++ b/dbt/include/snowflake/macros/relations/_rename_approach_3.sql @@ -0,0 +1,63 @@ +/* +This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. + +Implement at the `dbt-snowflake` level by overriding `default__get_rename_sql()`: +- only affects `dbt-snowflake` +- removes support for fully qualified names +- still supports relations +- still supports moving relations to another database and/or schema + however this requires using a relation instead of a fully qualified name +*/ + +------------------------------------------ dbt-adapters ------------------------------------------ +{%- macro get_rename_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` (intended usage) + Returns: templated string + */ + {{- log('Applying RENAME to: ' ~ relation) -}} + {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} +{%- endmacro -%} + + +{%- macro default__get_rename_sql(relation, new_name) -%} + {%- if relation.is_view -%} + {{ get_rename_view_sql(relation, new_name) }} + + {%- elif relation.is_table -%} + {{ get_rename_table_sql(relation, new_name) }} + + {%- elif relation.is_materialized_view -%} + {{ get_rename_materialized_view_sql(relation, new_name) }} + + {%- else -%} + {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} + + {%- endif -%} + +{%- endmacro -%} + + +{% macro get_rename_view_sql(relation, new_name) %} + {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} +{% endmacro %} + + +------------------------------------------ dbt-snowflake ------------------------------------------ +{%- macro snowflake__get_rename_sql(relation, new_name) -%} + {% set new_name = relation.incorporate(path={"identifier": new_name}).render() if new_name is string else new_name.render() %} + {{ default__get_rename_sql(relation, new_name) }} +{%- endmacro -%} + + +{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` (intended usage) + Returns: templated string + */ + alter view {{ relation }} rename to {{ new_name }} +{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_usage.sql b/dbt/include/snowflake/macros/relations/_rename_usage.sql new file mode 100644 index 000000000..d0e93f25d --- /dev/null +++ b/dbt/include/snowflake/macros/relations/_rename_usage.sql @@ -0,0 +1,39 @@ +/* +This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. + +These are examples of the current usage. +*/ +------------------------------------------ dbt-adapters ------------------------------------------ +{%- 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 -%} + + +{%- 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 -%} From 807ac26c74752b52fe36ea4160fe1be6fe1296be Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Fri, 31 May 2024 19:51:24 -0400 Subject: [PATCH 07/10] highlighted the changed lines --- dbt/include/snowflake/macros/relations/_rename_approach_1.sql | 1 + dbt/include/snowflake/macros/relations/_rename_approach_2.sql | 1 + dbt/include/snowflake/macros/relations/_rename_approach_3.sql | 1 + 3 files changed, 3 insertions(+) diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_1.sql b/dbt/include/snowflake/macros/relations/_rename_approach_1.sql index ee2e126b4..406a765a0 100644 --- a/dbt/include/snowflake/macros/relations/_rename_approach_1.sql +++ b/dbt/include/snowflake/macros/relations/_rename_approach_1.sql @@ -52,5 +52,6 @@ Implement at the `dbt-snowflake` level in `snowflake__get_rename_x_sql()`: new_name: str - new identifier for `relation` Returns: templated string */ + ------------ CHANGE: updated the line below ------------ alter view {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} {%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_2.sql b/dbt/include/snowflake/macros/relations/_rename_approach_2.sql index aa0be43a0..fd0aee8b7 100644 --- a/dbt/include/snowflake/macros/relations/_rename_approach_2.sql +++ b/dbt/include/snowflake/macros/relations/_rename_approach_2.sql @@ -18,6 +18,7 @@ Implement at the `dbt-adapters` level in `get_rename_sql()`: Returns: templated string */ {{- log('Applying RENAME to: ' ~ relation) -}} + ------------ CHANGE: added the line below ------------ {% set new_name = relation.incorporate(path={"identifier": new_name}).render() if new_name is string else new_name.render() %} {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} {%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_3.sql b/dbt/include/snowflake/macros/relations/_rename_approach_3.sql index 76e120352..9c870c862 100644 --- a/dbt/include/snowflake/macros/relations/_rename_approach_3.sql +++ b/dbt/include/snowflake/macros/relations/_rename_approach_3.sql @@ -46,6 +46,7 @@ Implement at the `dbt-snowflake` level by overriding `default__get_rename_sql()` ------------------------------------------ dbt-snowflake ------------------------------------------ +------------ CHANGE: added the entire macro below ------------ {%- macro snowflake__get_rename_sql(relation, new_name) -%} {% set new_name = relation.incorporate(path={"identifier": new_name}).render() if new_name is string else new_name.render() %} {{ default__get_rename_sql(relation, new_name) }} From 3f9ea8040ec854ba3f8c5bc70e67e9fb7ffeb020 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Mon, 3 Jun 2024 13:20:03 -0400 Subject: [PATCH 08/10] add a fourth approach --- .../macros/relations/_rename_approach_4.sql | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_4.sql diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_4.sql b/dbt/include/snowflake/macros/relations/_rename_approach_4.sql new file mode 100644 index 000000000..3304d7cdd --- /dev/null +++ b/dbt/include/snowflake/macros/relations/_rename_approach_4.sql @@ -0,0 +1,83 @@ +/* +This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. + +Implement at the `dbt-snowflake` level by overriding `snowflake__get_create_backup_sql()` and `snowflake__get_rename_intermediate_sql()`: +- only affects `dbt-snowflake` +- only affects backup/rename, which is where we're seeing the issue +- still supports fully qualified names +- still supports relations +- still supports moving relations to another database and/or schema +- rename macro behavior matches Snowflake rename behavior +*/ + +------------------------------------------ dbt-adapters ------------------------------------------ +{%- macro get_rename_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` (intended usage) + Returns: templated string + */ + {{- log('Applying RENAME to: ' ~ relation) -}} + {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} +{%- endmacro -%} + + +{%- macro default__get_rename_sql(relation, new_name) -%} + {%- if relation.is_view -%} + {{ get_rename_view_sql(relation, new_name) }} + + {%- elif relation.is_table -%} + {{ get_rename_table_sql(relation, new_name) }} + + {%- elif relation.is_materialized_view -%} + {{ get_rename_materialized_view_sql(relation, new_name) }} + + {%- else -%} + {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} + + {%- endif -%} + +{%- endmacro -%} + + +{% macro get_rename_view_sql(relation, new_name) %} + {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} +{% endmacro %} + + +------------------------------------------ dbt-snowflake ------------------------------------------ +------------ CHANGE: added the entire macro below ------------ +{%- macro snowflake__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.render()) }} + +{%- endmacro -%} + + +------------ CHANGE: added the entire macro below ------------ +{%- macro snowflake__get_rename_intermediate_sql(relation) -%} + + -- get the standard intermediate name + {% set intermediate_relation = make_intermediate_relation(relation) %} + + {{ get_rename_sql(intermediate_relation, relation.render()) }} + +{%- endmacro -%} + + +{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} + /* + Args: + relation: BaseRelation - relation to be renamed + new_name: str - new identifier for `relation` (intended usage) + Returns: templated string + */ + alter view {{ relation }} rename to {{ new_name }} +{%- endmacro -%} From 81ff4de2b3fd1587a982782691e2ee1c075c4899 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Tue, 4 Jun 2024 13:27:03 -0400 Subject: [PATCH 09/10] override create_backup and rename_intermediate to use fully qualified names --- .../macros/relations/_rename_approach_0.sql | 59 ------------- .../macros/relations/_rename_approach_1.sql | 57 ------------- .../macros/relations/_rename_approach_2.sql | 59 ------------- .../macros/relations/_rename_approach_3.sql | 64 -------------- .../macros/relations/_rename_approach_4.sql | 83 ------------------- .../macros/relations/_rename_usage.sql | 39 --------- .../macros/relations/create_backup.sql | 12 +++ .../macros/relations/rename_intermediate.sql | 9 ++ .../macros/relations/table/rename.sql | 12 ++- .../macros/relations/view/rename.sql | 12 ++- 10 files changed, 43 insertions(+), 363 deletions(-) delete mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_0.sql delete mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_1.sql delete mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_2.sql delete mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_3.sql delete mode 100644 dbt/include/snowflake/macros/relations/_rename_approach_4.sql delete mode 100644 dbt/include/snowflake/macros/relations/_rename_usage.sql create mode 100644 dbt/include/snowflake/macros/relations/create_backup.sql create mode 100644 dbt/include/snowflake/macros/relations/rename_intermediate.sql diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_0.sql b/dbt/include/snowflake/macros/relations/_rename_approach_0.sql deleted file mode 100644 index 3793c2e5a..000000000 --- a/dbt/include/snowflake/macros/relations/_rename_approach_0.sql +++ /dev/null @@ -1,59 +0,0 @@ -/* -This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. - -Docstrings have been added to communicate _intended_ usage. They do not cover all supported usage however, as is the issue we're facing here. - -This represents the current logic flow for views (tables would be the same): -- supports fully qualified names as strings -- supports relations -- supports moving relations to another database/schema -- unintentionally moves `relation` for `dbt-snowflake` if a `use schema` or `use database` statement runs first - and `new_name` is an identifier, e.g. when calling `get_create_backup_sql()` -*/ - ------------------------------------------- dbt-adapters ------------------------------------------ -{%- macro get_rename_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` - Returns: templated string - */ - {{- log('Applying RENAME to: ' ~ relation) -}} - {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} -{%- endmacro -%} - - -{%- macro default__get_rename_sql(relation, new_name) -%} - {%- if relation.is_view -%} - {{ get_rename_view_sql(relation, new_name) }} - - {%- elif relation.is_table -%} - {{ get_rename_table_sql(relation, new_name) }} - - {%- elif relation.is_materialized_view -%} - {{ get_rename_materialized_view_sql(relation, new_name) }} - - {%- else -%} - {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} - - {%- endif -%} - -{%- endmacro -%} - - -{% macro get_rename_view_sql(relation, new_name) %} - {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} -{% endmacro %} - - ------------------------------------------- dbt-snowflake ------------------------------------------ -{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` - Returns: templated string - */ - alter view {{ relation }} rename to {{ new_name }} -{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_1.sql b/dbt/include/snowflake/macros/relations/_rename_approach_1.sql deleted file mode 100644 index 406a765a0..000000000 --- a/dbt/include/snowflake/macros/relations/_rename_approach_1.sql +++ /dev/null @@ -1,57 +0,0 @@ -/* -This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. - -Implement at the `dbt-snowflake` level in `snowflake__get_rename_x_sql()`: -- only affects `dbt-snowflake` -- removes support for fully qualified names -- removes support for relations -- removes support for moving relations to another database and/or schema -*/ - ------------------------------------------- dbt-adapters ------------------------------------------ -{%- macro get_rename_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` - Returns: templated string - */ - {{- log('Applying RENAME to: ' ~ relation) -}} - {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} -{%- endmacro -%} - - -{%- macro default__get_rename_sql(relation, new_name) -%} - {%- if relation.is_view -%} - {{ get_rename_view_sql(relation, new_name) }} - - {%- elif relation.is_table -%} - {{ get_rename_table_sql(relation, new_name) }} - - {%- elif relation.is_materialized_view -%} - {{ get_rename_materialized_view_sql(relation, new_name) }} - - {%- else -%} - {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} - - {%- endif -%} - -{%- endmacro -%} - - -{% macro get_rename_view_sql(relation, new_name) %} - {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} -{% endmacro %} - - ------------------------------------------- dbt-snowflake ------------------------------------------ -{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` - Returns: templated string - */ - ------------ CHANGE: updated the line below ------------ - alter view {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} -{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_2.sql b/dbt/include/snowflake/macros/relations/_rename_approach_2.sql deleted file mode 100644 index fd0aee8b7..000000000 --- a/dbt/include/snowflake/macros/relations/_rename_approach_2.sql +++ /dev/null @@ -1,59 +0,0 @@ -/* -This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. - -Implement at the `dbt-adapters` level in `get_rename_sql()`: -- affects all adapters, may require fixes for platforms that don't support renaming with a fully qualified name (unclear if this is a concern) -- removes support for fully qualified names -- still supports relations -- still supports moving relations to another database and/or schema - however this requires using a relation instead of a fully qualified name -*/ - ------------------------------------------- dbt-adapters ------------------------------------------ -{%- macro get_rename_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: Union[str, BaseRelation] - new identifier or BaseRelation instance for `relation` - Returns: templated string - */ - {{- log('Applying RENAME to: ' ~ relation) -}} - ------------ CHANGE: added the line below ------------ - {% set new_name = relation.incorporate(path={"identifier": new_name}).render() if new_name is string else new_name.render() %} - {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} -{%- endmacro -%} - - -{%- macro default__get_rename_sql(relation, new_name) -%} - {%- if relation.is_view -%} - {{ get_rename_view_sql(relation, new_name) }} - - {%- elif relation.is_table -%} - {{ get_rename_table_sql(relation, new_name) }} - - {%- elif relation.is_materialized_view -%} - {{ get_rename_materialized_view_sql(relation, new_name) }} - - {%- else -%} - {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} - - {%- endif -%} - -{%- endmacro -%} - - -{% macro get_rename_view_sql(relation, new_name) %} - {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} -{% endmacro %} - - ------------------------------------------- dbt-snowflake ------------------------------------------ -{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new fully qualified path, for `relation` - Returns: templated string - */ - alter view {{ relation }} rename to {{ new_name }} -{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_3.sql b/dbt/include/snowflake/macros/relations/_rename_approach_3.sql deleted file mode 100644 index 9c870c862..000000000 --- a/dbt/include/snowflake/macros/relations/_rename_approach_3.sql +++ /dev/null @@ -1,64 +0,0 @@ -/* -This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. - -Implement at the `dbt-snowflake` level by overriding `default__get_rename_sql()`: -- only affects `dbt-snowflake` -- removes support for fully qualified names -- still supports relations -- still supports moving relations to another database and/or schema - however this requires using a relation instead of a fully qualified name -*/ - ------------------------------------------- dbt-adapters ------------------------------------------ -{%- macro get_rename_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` (intended usage) - Returns: templated string - */ - {{- log('Applying RENAME to: ' ~ relation) -}} - {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} -{%- endmacro -%} - - -{%- macro default__get_rename_sql(relation, new_name) -%} - {%- if relation.is_view -%} - {{ get_rename_view_sql(relation, new_name) }} - - {%- elif relation.is_table -%} - {{ get_rename_table_sql(relation, new_name) }} - - {%- elif relation.is_materialized_view -%} - {{ get_rename_materialized_view_sql(relation, new_name) }} - - {%- else -%} - {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} - - {%- endif -%} - -{%- endmacro -%} - - -{% macro get_rename_view_sql(relation, new_name) %} - {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} -{% endmacro %} - - ------------------------------------------- dbt-snowflake ------------------------------------------ ------------- CHANGE: added the entire macro below ------------ -{%- macro snowflake__get_rename_sql(relation, new_name) -%} - {% set new_name = relation.incorporate(path={"identifier": new_name}).render() if new_name is string else new_name.render() %} - {{ default__get_rename_sql(relation, new_name) }} -{%- endmacro -%} - - -{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` (intended usage) - Returns: templated string - */ - alter view {{ relation }} rename to {{ new_name }} -{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_approach_4.sql b/dbt/include/snowflake/macros/relations/_rename_approach_4.sql deleted file mode 100644 index 3304d7cdd..000000000 --- a/dbt/include/snowflake/macros/relations/_rename_approach_4.sql +++ /dev/null @@ -1,83 +0,0 @@ -/* -This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. - -Implement at the `dbt-snowflake` level by overriding `snowflake__get_create_backup_sql()` and `snowflake__get_rename_intermediate_sql()`: -- only affects `dbt-snowflake` -- only affects backup/rename, which is where we're seeing the issue -- still supports fully qualified names -- still supports relations -- still supports moving relations to another database and/or schema -- rename macro behavior matches Snowflake rename behavior -*/ - ------------------------------------------- dbt-adapters ------------------------------------------ -{%- macro get_rename_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` (intended usage) - Returns: templated string - */ - {{- log('Applying RENAME to: ' ~ relation) -}} - {{- adapter.dispatch('get_rename_sql', 'dbt')(relation, new_name) -}} -{%- endmacro -%} - - -{%- macro default__get_rename_sql(relation, new_name) -%} - {%- if relation.is_view -%} - {{ get_rename_view_sql(relation, new_name) }} - - {%- elif relation.is_table -%} - {{ get_rename_table_sql(relation, new_name) }} - - {%- elif relation.is_materialized_view -%} - {{ get_rename_materialized_view_sql(relation, new_name) }} - - {%- else -%} - {{- exceptions.raise_compiler_error("`get_rename_sql` has not been implemented for: " ~ relation.type ) -}} - - {%- endif -%} - -{%- endmacro -%} - - -{% macro get_rename_view_sql(relation, new_name) %} - {{- adapter.dispatch('get_rename_view_sql', 'dbt')(relation, new_name) -}} -{% endmacro %} - - ------------------------------------------- dbt-snowflake ------------------------------------------ ------------- CHANGE: added the entire macro below ------------ -{%- macro snowflake__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.render()) }} - -{%- endmacro -%} - - ------------- CHANGE: added the entire macro below ------------ -{%- macro snowflake__get_rename_intermediate_sql(relation) -%} - - -- get the standard intermediate name - {% set intermediate_relation = make_intermediate_relation(relation) %} - - {{ get_rename_sql(intermediate_relation, relation.render()) }} - -{%- endmacro -%} - - -{%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - /* - Args: - relation: BaseRelation - relation to be renamed - new_name: str - new identifier for `relation` (intended usage) - Returns: templated string - */ - alter view {{ relation }} rename to {{ new_name }} -{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/_rename_usage.sql b/dbt/include/snowflake/macros/relations/_rename_usage.sql deleted file mode 100644 index d0e93f25d..000000000 --- a/dbt/include/snowflake/macros/relations/_rename_usage.sql +++ /dev/null @@ -1,39 +0,0 @@ -/* -This file was created for the sake of reviewing the two implementation options. This will be deleted prior to merging the PR. - -These are examples of the current usage. -*/ ------------------------------------------- dbt-adapters ------------------------------------------ -{%- 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 -%} - - -{%- 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 -%} diff --git a/dbt/include/snowflake/macros/relations/create_backup.sql b/dbt/include/snowflake/macros/relations/create_backup.sql new file mode 100644 index 000000000..b5f347cd9 --- /dev/null +++ b/dbt/include/snowflake/macros/relations/create_backup.sql @@ -0,0 +1,12 @@ +{%- macro snowflake__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) }}; + + -- use `render` to ensure that the fully qualified name is used + {{ get_rename_sql(relation, backup_relation.render()) }} + +{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/rename_intermediate.sql b/dbt/include/snowflake/macros/relations/rename_intermediate.sql new file mode 100644 index 000000000..abd5fee92 --- /dev/null +++ b/dbt/include/snowflake/macros/relations/rename_intermediate.sql @@ -0,0 +1,9 @@ +{%- macro snowflake__get_rename_intermediate_sql(relation) -%} + + -- get the standard intermediate name + {% set intermediate_relation = make_intermediate_relation(relation) %} + + -- use `render` to ensure that the fully qualified name is used + {{ get_rename_sql(intermediate_relation, relation.render()) }} + +{%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/table/rename.sql b/dbt/include/snowflake/macros/relations/table/rename.sql index 1ba5bc2ba..699debf28 100644 --- a/dbt/include/snowflake/macros/relations/table/rename.sql +++ b/dbt/include/snowflake/macros/relations/table/rename.sql @@ -1,3 +1,13 @@ {%- macro snowflake__get_rename_table_sql(relation, new_name) -%} - alter table {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} + /* + Rename or move a table to the new name. + + Args: + relation: SnowflakeRelation - relation to be renamed + new_name: Union[str, SnowflakeRelation] - new name for `relation` + if providing a string, the default database/schema will be used if that string is just an identifier + if providing a SnowflakeRelation, `render` will be used to produce a fully qualified name + Returns: templated string + */ + alter table {{ relation }} rename to {{ new_name }} {%- endmacro -%} diff --git a/dbt/include/snowflake/macros/relations/view/rename.sql b/dbt/include/snowflake/macros/relations/view/rename.sql index f56bb9971..add2f49b9 100644 --- a/dbt/include/snowflake/macros/relations/view/rename.sql +++ b/dbt/include/snowflake/macros/relations/view/rename.sql @@ -1,3 +1,13 @@ {%- macro snowflake__get_rename_view_sql(relation, new_name) -%} - alter view {{ relation }} rename to {{ relation.incorporate(path={"identifier": new_name}) }} + /* + Rename or move a view to the new name. + + Args: + relation: SnowflakeRelation - relation to be renamed + new_name: Union[str, SnowflakeRelation] - new name for `relation` + if providing a string, the default database/schema will be used if that string is just an identifier + if providing a SnowflakeRelation, `render` will be used to produce a fully qualified name + Returns: templated string + */ + alter view {{ relation }} rename to {{ new_name }} {%- endmacro -%} From aaaf4230d821a6e0e86edb10cb8dcc6768466957 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Tue, 4 Jun 2024 15:45:24 -0400 Subject: [PATCH 10/10] update tests to only test backup and intermediate renames --- tests/functional/relation_tests/base.py | 24 +++++++++++++++---- tests/functional/relation_tests/test_table.py | 15 ++++++++---- tests/functional/relation_tests/test_view.py | 15 ++++++++---- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/tests/functional/relation_tests/base.py b/tests/functional/relation_tests/base.py index 887bc7324..d08a6945b 100644 --- a/tests/functional/relation_tests/base.py +++ b/tests/functional/relation_tests/base.py @@ -23,11 +23,20 @@ """ -MACRO__GET_RENAME_SQL = """ -{% macro test__get_rename_sql(database, schema, identifier, relation_type, new_name) -%} +MACRO__GET_CREATE_BACKUP_SQL = """ +{% macro test__get_create_backup_sql(database, schema, identifier, relation_type) -%} {%- set relation = adapter.Relation.create(database=database, schema=schema, identifier=identifier, type=relation_type) -%} - {% call statement('test__get_rename_sql') -%} - {{ get_rename_sql(relation, new_name) }} + {% call statement('test__get_create_backup_sql') -%} + {{ get_create_backup_sql(relation) }} + {%- endcall %} +{% endmacro %}""" + + +MACRO__GET_RENAME_INTERMEDIATE_SQL = """ +{% macro test__get_rename_intermediate_sql(database, schema, identifier, relation_type) -%} + {%- set relation = adapter.Relation.create(database=database, schema=schema, identifier=identifier, type=relation_type) -%} + {% call statement('test__get_rename_intermediate_sql') -%} + {{ get_rename_intermediate_sql(relation) }} {%- endcall %} {% endmacro %}""" @@ -41,12 +50,17 @@ def seeds(self): def models(self): yield { "my_table.sql": TABLE, + "my_table__dbt_tmp.sql": TABLE, "my_view.sql": VIEW, + "my_view__dbt_tmp.sql": VIEW, } @pytest.fixture(scope="class") def macros(self): - yield {"test__get_rename_sql.sql": MACRO__GET_RENAME_SQL} + yield { + "test__get_create_backup_sql.sql": MACRO__GET_CREATE_BACKUP_SQL, + "test__get_rename_intermediate_sql.sql": MACRO__GET_RENAME_INTERMEDIATE_SQL, + } @pytest.fixture(scope="class", autouse=True) def setup(self, project): diff --git a/tests/functional/relation_tests/test_table.py b/tests/functional/relation_tests/test_table.py index 20ce8e305..b4a8709ea 100644 --- a/tests/functional/relation_tests/test_table.py +++ b/tests/functional/relation_tests/test_table.py @@ -3,16 +3,23 @@ class TestTable(RelationOperation): - def test_get_rename_table_sql(self, project): + def test_get_create_backup_and_rename_intermediate_sql(self, project): args = { "database": project.database, "schema": project.test_schema, "identifier": "my_table", "relation_type": "table", - "new_name": "my_new_table", } expected_statement = ( f"alter table {project.database}.{project.test_schema}.my_table " - f"rename to {project.database}.{project.test_schema}.my_new_table" + f"rename to {project.database}.{project.test_schema}.my_table__dbt_backup" + ) + self.assert_operation(project, "test__get_create_backup_sql", args, expected_statement) + + expected_statement = ( + f"alter table {project.database}.{project.test_schema}.my_table__dbt_tmp " + f"rename to {project.database}.{project.test_schema}.my_table" + ) + self.assert_operation( + project, "test__get_rename_intermediate_sql", args, expected_statement ) - self.assert_operation(project, "test__get_rename_sql", args, expected_statement) diff --git a/tests/functional/relation_tests/test_view.py b/tests/functional/relation_tests/test_view.py index 0f338365e..721455da1 100644 --- a/tests/functional/relation_tests/test_view.py +++ b/tests/functional/relation_tests/test_view.py @@ -3,16 +3,23 @@ class TestView(RelationOperation): - def test_get_rename_view_sql(self, project): + def test_get_create_backup_and_rename_intermediate_sql(self, project): args = { "database": project.database, "schema": project.test_schema, "identifier": "my_view", "relation_type": "view", - "new_name": "my_new_view", } expected_statement = ( f"alter view {project.database}.{project.test_schema}.my_view " - f"rename to {project.database}.{project.test_schema}.my_new_view" + f"rename to {project.database}.{project.test_schema}.my_view__dbt_backup" + ) + self.assert_operation(project, "test__get_create_backup_sql", args, expected_statement) + + expected_statement = ( + f"alter view {project.database}.{project.test_schema}.my_view__dbt_tmp " + f"rename to {project.database}.{project.test_schema}.my_view" + ) + self.assert_operation( + project, "test__get_rename_intermediate_sql", args, expected_statement ) - self.assert_operation(project, "test__get_rename_sql", args, expected_statement)