|
54 | 54 | select * from {{ ref('raw_data') }}
|
55 | 55 | """
|
56 | 56 |
|
| 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 | + |
57 | 69 | base_validation = """
|
58 | 70 | with base_query AS (
|
59 | 71 | select i.[name] as index_name,
|
|
107 | 119 | """
|
108 | 120 | )
|
109 | 121 |
|
| 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 | + |
110 | 137 |
|
111 | 138 | class TestIndex:
|
112 | 139 | @pytest.fixture(scope="class")
|
@@ -143,3 +170,77 @@ def test_create_index(self, project):
|
143 | 170 | "Nonclustered unique index": 4,
|
144 | 171 | }
|
145 | 172 | 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