|
25 | 25 | WHERE |
26 | 26 | TABLE_SCHEMA = '{schema}' |
27 | 27 | AND |
28 | | - TABLE_NAME LIKE '%__dbt_tmp_vw' |
| 28 | + TABLE_NAME LIKE '%__dbt_tmp%' |
| 29 | +""" |
| 30 | + |
| 31 | +seed_schema_yml = """ |
| 32 | +version: 2 |
| 33 | +seeds: |
| 34 | + - name: raw_data |
| 35 | + config: |
| 36 | + column_types: |
| 37 | + id: integer |
| 38 | + value_col: nvarchar(20) |
| 39 | + date_col: datetime2(6) |
| 40 | +""" |
| 41 | + |
| 42 | +seed_csv = """id,data,date_col |
| 43 | +1,1,2024-01-01 |
| 44 | +2,1,2024-01-01 |
| 45 | +3,1,2024-01-01""" |
| 46 | + |
| 47 | +incremental_sql = """ |
| 48 | +{{ |
| 49 | + config( |
| 50 | + materialized='incremental', |
| 51 | + unique_key='id', |
| 52 | + on_schema_change='sync_all_columns' |
| 53 | + ) |
| 54 | +}} |
| 55 | +
|
| 56 | +WITH source_data AS (SELECT * FROM {{ ref('raw_data') }} ) |
| 57 | +
|
| 58 | +{% if is_incremental() %} |
| 59 | +
|
| 60 | +SELECT id, |
| 61 | + data, |
| 62 | + date_col |
| 63 | +FROM source_data WHERE id NOT IN (SELECT id from {{ this }} ) |
| 64 | +
|
| 65 | +{% else %} |
| 66 | +
|
| 67 | +SELECT id, |
| 68 | + data, |
| 69 | + date_col |
| 70 | +FROM source_data where id <= 1 |
| 71 | +
|
| 72 | +{% endif %} |
29 | 73 | """ |
30 | 74 |
|
31 | 75 |
|
32 | | -class TestTempRelationCleanup: |
| 76 | +class BaseTempRelationCleanup: |
| 77 | + view_name = "__dbt_tmp_vw" |
| 78 | + |
| 79 | + def validate_temp_objects(self, project): |
| 80 | + with get_connection(project.adapter): |
| 81 | + result, table = project.adapter.execute( |
| 82 | + validation_sql.format( |
| 83 | + database=project.database, schema=project.created_schemas[0] |
| 84 | + ), |
| 85 | + fetch=True, |
| 86 | + ) |
| 87 | + assert len(table.rows) == 0 |
| 88 | + |
| 89 | + |
| 90 | +class TestTempRelationCleanup(BaseTempRelationCleanup): |
33 | 91 | """ |
34 | 92 | This tests to validate that the temporary relations, |
35 | 93 | created by the `create_table` statement is cleaned up after a set of runs. |
36 | 94 | """ |
37 | 95 |
|
38 | | - view_name = "__dbt_tmp_vw" |
39 | | - |
40 | 96 | @pytest.fixture(scope="class") |
41 | 97 | def models(self): |
42 | 98 | return { |
43 | 99 | "table_model.sql": table_model, |
44 | 100 | "schema.yml": model_yml, |
45 | 101 | } |
46 | 102 |
|
| 103 | + def test_drops_temp_view_object(self, project): |
| 104 | + run_dbt(["run"]) |
| 105 | + |
| 106 | + self.validate_temp_objects(project) |
| 107 | + |
| 108 | + |
| 109 | +class TestIncrementalTempCleanup(BaseTempRelationCleanup): |
| 110 | + """Tests if the `dbt_tmp` views are properly cleaned up in an incremental model""" |
| 111 | + |
| 112 | + @pytest.fixture(scope="class") |
| 113 | + def seeds(self): |
| 114 | + return { |
| 115 | + "raw_data.csv": seed_csv, |
| 116 | + "schema.yml": seed_schema_yml, |
| 117 | + } |
| 118 | + |
| 119 | + @pytest.fixture(scope="class") |
| 120 | + def models(self): |
| 121 | + return { |
| 122 | + "table_model.sql": incremental_sql, |
| 123 | + "schema.yml": model_yml, |
| 124 | + } |
| 125 | + |
47 | 126 | def test_drops_temp_view_object(self, project): |
48 | 127 | run_dbt(["seed"]) |
49 | 128 | run_dbt(["run"]) |
| 129 | + run_dbt(["run"]) |
50 | 130 |
|
51 | | - with get_connection(project.adapter): |
52 | | - result, table = project.adapter.execute( |
53 | | - validation_sql.format( |
54 | | - database=project.database, schema=project.created_schemas[0] |
55 | | - ), |
56 | | - fetch=True, |
57 | | - ) |
58 | | - assert len(table.rows) == 0 |
| 131 | + self.validate_temp_objects(project) |
0 commit comments