From 85ac7789f473eb755a1278e3155a519e7dfc2d1c Mon Sep 17 00:00:00 2001 From: Andrew Vaccaro Date: Tue, 28 Mar 2023 10:52:05 -0400 Subject: [PATCH 1/4] Handle empty strings in slugify macro --- CHANGELOG.md | 3 ++- integration_tests/tests/jinja_helpers/test_slugify.sql | 3 ++- macros/jinja_helpers/slugify.sql | 6 +++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 728c2010..4c129dde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,10 +11,11 @@ # Unreleased ## Fixes - get_relations_by_pattern for Databricks connection with Unity catalog #768 ([#768](https://github.com/dbt-labs/dbt-utils/issues/768), [#769](https://github.com/dbt-labs/dbt-utils/pull/769)) +- Handle empty strings in slugify macro #773 ([#773](https://github.com/dbt-labs/dbt-utils/issues/773), [#XXX](https://github.com/dbt-labs/dbt-utils/pull/XXX)) ## Contributors: @Harmuth94, [#768](https://github.com/dbt-labs/dbt-utils/issues/768) - +@atvaccaro, [#773](https://github.com/dbt-labs/dbt-utils/issues/773) # dbt utils v1.0 diff --git a/integration_tests/tests/jinja_helpers/test_slugify.sql b/integration_tests/tests/jinja_helpers/test_slugify.sql index 7d07ec44..18c3b197 100644 --- a/integration_tests/tests/jinja_helpers/test_slugify.sql +++ b/integration_tests/tests/jinja_helpers/test_slugify.sql @@ -1,4 +1,5 @@ with comparisons as ( + select '{{ dbt_utils.slugify("") }}' as output, '' as expected select '{{ dbt_utils.slugify("!Hell0 world-hi") }}' as output, 'hell0_world_hi' as expected union all select '{{ dbt_utils.slugify("0Hell0 world-hi") }}' as output, '_0hell0_world_hi' as expected @@ -6,4 +7,4 @@ with comparisons as ( select * from comparisons -where output != expected \ No newline at end of file +where output != expected diff --git a/macros/jinja_helpers/slugify.sql b/macros/jinja_helpers/slugify.sql index 14c0c74c..1be89e22 100644 --- a/macros/jinja_helpers/slugify.sql +++ b/macros/jinja_helpers/slugify.sql @@ -1,5 +1,9 @@ {% macro slugify(string) %} +{% if not string %} +{{ return(string) }} +{% endif %} + {#- Lower case the string -#} {% set string = string | lower %} {#- Replace spaces and dashes with underscores -#} @@ -11,4 +15,4 @@ {{ return(string) }} -{% endmacro %} \ No newline at end of file +{% endmacro %} From c9c35e85c297ac9be292dba3ba7da5413cde8b1d Mon Sep 17 00:00:00 2001 From: Andrew Vaccaro Date: Tue, 28 Mar 2023 11:05:25 -0400 Subject: [PATCH 2/4] update changelog with PR number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c129dde..bec7b395 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ # Unreleased ## Fixes - get_relations_by_pattern for Databricks connection with Unity catalog #768 ([#768](https://github.com/dbt-labs/dbt-utils/issues/768), [#769](https://github.com/dbt-labs/dbt-utils/pull/769)) -- Handle empty strings in slugify macro #773 ([#773](https://github.com/dbt-labs/dbt-utils/issues/773), [#XXX](https://github.com/dbt-labs/dbt-utils/pull/XXX)) +- Handle empty strings in slugify macro #773 ([#773](https://github.com/dbt-labs/dbt-utils/issues/773), [#774](https://github.com/dbt-labs/dbt-utils/pull/774)) ## Contributors: @Harmuth94, [#768](https://github.com/dbt-labs/dbt-utils/issues/768) From 7e2acd657f4688f1b347ff2e366101b8430480e3 Mon Sep 17 00:00:00 2001 From: Andrew Vaccaro Date: Tue, 28 Mar 2023 11:06:17 -0400 Subject: [PATCH 3/4] fix slugify test --- integration_tests/tests/jinja_helpers/test_slugify.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/integration_tests/tests/jinja_helpers/test_slugify.sql b/integration_tests/tests/jinja_helpers/test_slugify.sql index 18c3b197..fd0984fe 100644 --- a/integration_tests/tests/jinja_helpers/test_slugify.sql +++ b/integration_tests/tests/jinja_helpers/test_slugify.sql @@ -1,5 +1,6 @@ with comparisons as ( select '{{ dbt_utils.slugify("") }}' as output, '' as expected + union all select '{{ dbt_utils.slugify("!Hell0 world-hi") }}' as output, 'hell0_world_hi' as expected union all select '{{ dbt_utils.slugify("0Hell0 world-hi") }}' as output, '_0hell0_world_hi' as expected From 413227dc66992ea5108af770d90810daf9ad16e1 Mon Sep 17 00:00:00 2001 From: Andrew Vaccaro Date: Mon, 24 Apr 2023 13:13:03 -0400 Subject: [PATCH 4/4] return empty string rather than none --- integration_tests/tests/jinja_helpers/test_slugify.sql | 2 ++ macros/jinja_helpers/slugify.sql | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/integration_tests/tests/jinja_helpers/test_slugify.sql b/integration_tests/tests/jinja_helpers/test_slugify.sql index fd0984fe..e607af0f 100644 --- a/integration_tests/tests/jinja_helpers/test_slugify.sql +++ b/integration_tests/tests/jinja_helpers/test_slugify.sql @@ -1,6 +1,8 @@ with comparisons as ( select '{{ dbt_utils.slugify("") }}' as output, '' as expected union all + select '{{ dbt_utils.slugify(None) }}' as output, '' as expected + union all select '{{ dbt_utils.slugify("!Hell0 world-hi") }}' as output, 'hell0_world_hi' as expected union all select '{{ dbt_utils.slugify("0Hell0 world-hi") }}' as output, '_0hell0_world_hi' as expected diff --git a/macros/jinja_helpers/slugify.sql b/macros/jinja_helpers/slugify.sql index 1be89e22..f4c52c2b 100644 --- a/macros/jinja_helpers/slugify.sql +++ b/macros/jinja_helpers/slugify.sql @@ -1,7 +1,7 @@ {% macro slugify(string) %} {% if not string %} -{{ return(string) }} +{{ return('') }} {% endif %} {#- Lower case the string -#}