From 6871fc46b50d33cf3279a579506a843fd0f8850e Mon Sep 17 00:00:00 2001 From: colin-rogers-dbt <111200756+colin-rogers-dbt@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:50:25 -0800 Subject: [PATCH] Add drop_schema_named macro (#8868) * add drop_schema_named * give drop_schema_named a default --- .../unreleased/Features-20231017-143620.yaml | 6 ++++ .../macros/relations/schema.sql | 8 +++++ .../relations/test_dropping_schema_named.py | 35 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 .changes/unreleased/Features-20231017-143620.yaml create mode 100644 core/dbt/include/global_project/macros/relations/schema.sql create mode 100644 tests/adapter/dbt/tests/adapter/relations/test_dropping_schema_named.py diff --git a/.changes/unreleased/Features-20231017-143620.yaml b/.changes/unreleased/Features-20231017-143620.yaml new file mode 100644 index 00000000000..dfdd2b6f4b2 --- /dev/null +++ b/.changes/unreleased/Features-20231017-143620.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Add drop_schema_named macro +time: 2023-10-17T14:36:20.612289-07:00 +custom: + Author: colin-rogers-dbt + Issue: "8025" diff --git a/core/dbt/include/global_project/macros/relations/schema.sql b/core/dbt/include/global_project/macros/relations/schema.sql new file mode 100644 index 00000000000..55aa596ca9d --- /dev/null +++ b/core/dbt/include/global_project/macros/relations/schema.sql @@ -0,0 +1,8 @@ +{% macro drop_schema_named(schema_name) %} + {{ return(adapter.dispatch('drop_schema_named', 'dbt') (schema_name)) }} +{% endmacro %} + +{% macro default__drop_schema_named(schema_name) %} + {% set schema_relation = api.Relation.create(schema=schema_name) %} + {{ adapter.drop_schema(schema_relation) }} +{% endmacro %} diff --git a/tests/adapter/dbt/tests/adapter/relations/test_dropping_schema_named.py b/tests/adapter/dbt/tests/adapter/relations/test_dropping_schema_named.py new file mode 100644 index 00000000000..2999d43fec6 --- /dev/null +++ b/tests/adapter/dbt/tests/adapter/relations/test_dropping_schema_named.py @@ -0,0 +1,35 @@ +import pytest + +from dbt.tests.util import run_dbt, get_connection + + +class BaseDropSchemaNamed: + @pytest.fixture(scope="class") + def models(self): + return { + "model_a.sql": "select 1 as id", + } + + def test_dropped_schema_named_drops_expected_schema(self, project): + + results = run_dbt(["run"]) + assert len(results) == 1 + + run_dbt( + [ + "run-operation", + "drop_schema_named", + "--args", + f"{{schema_name: {project.test_schema} }}", + ] + ) + + adapter = project.adapter + with get_connection(adapter): + schemas = adapter.list_schemas(project.database) + + assert project.test_schema not in schemas + + +class TestDropSchemaNamed(BaseDropSchemaNamed): + pass