From 2c6ef66f356bfbcb068719a5a930bdf25d72a040 Mon Sep 17 00:00:00 2001 From: Ben Knight Date: Thu, 24 Oct 2024 14:56:18 +0100 Subject: [PATCH 1/3] Update the derivation of the test view name Use the local MD5 macro to create a more unique hash based on the SQL of the test itself --- dbt/include/sqlserver/macros/materializations/tests.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/include/sqlserver/macros/materializations/tests.sql b/dbt/include/sqlserver/macros/materializations/tests.sql index a0d79bb2..0bde2542 100644 --- a/dbt/include/sqlserver/macros/materializations/tests.sql +++ b/dbt/include/sqlserver/macros/materializations/tests.sql @@ -8,7 +8,7 @@ END {% set testview %} - [{{ target.schema }}.testview_{{ range(1300, 19000) | random }}] + [{{ target.schema }}.testview_{{ local_md5(main_sql) }}] {% endset %} {% set sql = main_sql.replace("'", "''")%} From 3eab8d3cb10101f844375aaf773f70be7ccd5ba4 Mon Sep 17 00:00:00 2001 From: Ben Knight Date: Fri, 25 Oct 2024 14:45:48 +0100 Subject: [PATCH 2/3] Add in a random element as well as the hash of the SQL of the test Add in a random element because sometimes two tests can be completely identical as the unique work of the test is done in the jinja template itself before compilation. See https://github.com/calogica/dbt-expectations/blob/main/macros/schema_tests/table_shape/expect_table_column_count_to_equal_other_table.sql --- dbt/include/sqlserver/macros/materializations/tests.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/include/sqlserver/macros/materializations/tests.sql b/dbt/include/sqlserver/macros/materializations/tests.sql index 0bde2542..e2982dc5 100644 --- a/dbt/include/sqlserver/macros/materializations/tests.sql +++ b/dbt/include/sqlserver/macros/materializations/tests.sql @@ -8,7 +8,7 @@ END {% set testview %} - [{{ target.schema }}.testview_{{ local_md5(main_sql) }}] + [{{ target.schema }}.testview_{{ local_md5(main_sql) }}_{{ range(1300, 19000) | random }}] {% endset %} {% set sql = main_sql.replace("'", "''")%} From a3dc543bec59f91a5e6cc83bc6c1be1a26352d45 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 2 Jan 2025 16:00:37 +0000 Subject: [PATCH 3/3] Update the test and unit test views to also fix #585 as we are altering the same code. Update unit tests to also use a hash of the SQL to generate their view names as we are altering this code for #585. --- dbt/include/sqlserver/macros/materializations/tests.sql | 2 +- dbt/include/sqlserver/macros/materializations/unit_tests.sql | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dbt/include/sqlserver/macros/materializations/tests.sql b/dbt/include/sqlserver/macros/materializations/tests.sql index e2982dc5..17d5d130 100644 --- a/dbt/include/sqlserver/macros/materializations/tests.sql +++ b/dbt/include/sqlserver/macros/materializations/tests.sql @@ -8,7 +8,7 @@ END {% set testview %} - [{{ target.schema }}.testview_{{ local_md5(main_sql) }}_{{ range(1300, 19000) | random }}] + [{{ target.schema }}].[testview_{{ local_md5(main_sql) }}_{{ range(1300, 19000) | random }}] {% endset %} {% set sql = main_sql.replace("'", "''")%} diff --git a/dbt/include/sqlserver/macros/materializations/unit_tests.sql b/dbt/include/sqlserver/macros/materializations/unit_tests.sql index b7df70b6..30a2a06b 100644 --- a/dbt/include/sqlserver/macros/materializations/unit_tests.sql +++ b/dbt/include/sqlserver/macros/materializations/unit_tests.sql @@ -7,13 +7,13 @@ EXEC('CREATE SCHEMA [{{ target.schema }}]') END {% set test_view %} - [{{ target.schema }}.testview_{{ range(1300, 19000) | random }}] + [{{ target.schema }}].[testview_{{ local_md5(main_sql) }}_{{ range(1300, 19000) | random }}] {% endset %} {% set test_sql = main_sql.replace("'", "''")%} EXEC('create view {{test_view}} as {{ test_sql }};') {% set expected_view %} - [{{ target.schema }}.expectedview_{{ range(1300, 19000) | random }}] + [{{ target.schema }}].[expectedview_{{ local_md5(expected_fixture_sql) }}_{{ range(1300, 19000) | random }}] {% endset %} {% set expected_sql = expected_fixture_sql.replace("'", "''")%} EXEC('create view {{expected_view}} as {{ expected_sql }};')