Skip to content

Commit c3e3fe6

Browse files
committed
Add schema filter to index drop query
1 parent e8e69c7 commit c3e3fe6

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

dbt/include/sqlserver/macros/adapter/indexes.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
inner join sys.tables {{ information_schema_hints() }}
109109
on sys.indexes.object_id = sys.tables.object_id
110110
where sys.indexes.[name] is not null
111+
and SCHEMA_NAME(sys.tables.schema_id) = '{{ this.schema }}'
111112
and sys.tables.[name] = '{{ this.table }}'
112113
for xml path('')
113114
); exec sp_executesql @drop_remaining_indexes_last;

tests/functional/adapter/mssql/test_index.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@
5454
select * from {{ ref('raw_data') }}
5555
"""
5656

57+
drop_schema_model = """
58+
{{
59+
config({
60+
"materialized": 'table',
61+
"post-hook": [
62+
"{{ drop_all_indexes_on_table() }}",
63+
]
64+
})
65+
}}
66+
select * from {{ ref('raw_data') }}
67+
"""
68+
5769
base_validation = """
5870
with base_query AS (
5971
select i.[name] as index_name,
@@ -107,6 +119,21 @@
107119
"""
108120
)
109121

122+
other_index_count = (
123+
base_validation
124+
+ """
125+
SELECT
126+
*
127+
FROM
128+
base_query
129+
WHERE
130+
schema_name='{schema_name}'
131+
AND
132+
table_view='{schema_name}.{table_name}'
133+
134+
"""
135+
)
136+
110137

111138
class TestIndex:
112139
@pytest.fixture(scope="class")
@@ -143,3 +170,77 @@ def test_create_index(self, project):
143170
"Nonclustered unique index": 4,
144171
}
145172
assert schema_dict == expected
173+
174+
175+
class TestIndexDropsOnlySchema:
176+
@pytest.fixture(scope="class")
177+
def project_config_update(self):
178+
return {"name": "generic_tests"}
179+
180+
@pytest.fixture(scope="class")
181+
def seeds(self):
182+
return {
183+
"raw_data.csv": index_seed_csv,
184+
"schema.yml": index_schema_base_yml,
185+
}
186+
187+
@pytest.fixture(scope="class")
188+
def models(self):
189+
return {
190+
"index_model.sql": drop_schema_model,
191+
"index_ccs_model.sql": model_sql_ccs,
192+
"schema.yml": model_yml,
193+
}
194+
195+
def create_table_and_index_other_schema(self, project):
196+
_schema = project.test_schema + "other"
197+
create_sql = f"""
198+
USE [{project.database}];
199+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{_schema}')
200+
BEGIN
201+
EXEC('CREATE SCHEMA [{_schema}]')
202+
END
203+
"""
204+
205+
create_table = f"""
206+
CREATE TABLE {_schema}.index_model (
207+
IDCOL BIGINT
208+
)
209+
"""
210+
211+
create_index = f"""
212+
CREATE INDEX sample_schema ON {_schema}.index_model (IDCOL)
213+
"""
214+
with get_connection(project.adapter):
215+
project.adapter.execute(create_sql, fetch=True)
216+
project.adapter.execute(create_table)
217+
project.adapter.execute(create_index)
218+
219+
def drop_schema_artifacts(self, project):
220+
_schema = project.test_schema + "other"
221+
drop_index = f"DROP INDEX IF EXISTS sample_schema ON {_schema}.index_model"
222+
drop_table = f"DROP TABLE IF EXISTS {_schema}.index_model"
223+
drop_schema = f"DROP SCHEMA IF EXISTS {_schema}"
224+
225+
with get_connection(project.adapter):
226+
project.adapter.execute(drop_index, fetch=True)
227+
project.adapter.execute(drop_table)
228+
project.adapter.execute(drop_schema)
229+
230+
def validate_other_schema(self, project):
231+
with get_connection(project.adapter):
232+
result, table = project.adapter.execute(
233+
other_index_count.format(
234+
schema_name=project.test_schema + "other", table_name="index_model"
235+
),
236+
fetch=True,
237+
)
238+
239+
assert len(table.rows) == 1
240+
241+
def test_create_index(self, project):
242+
self.create_table_and_index_other_schema(project)
243+
run_dbt(["seed"])
244+
run_dbt(["run"])
245+
self.validate_other_schema(project)
246+
self.drop_schema_artifacts(project)

0 commit comments

Comments
 (0)