Skip to content

Commit

Permalink
Refactor naming macro to use snake_case (#431)
Browse files Browse the repository at this point in the history
* Reformat naming macros to use snake_case

* Add test change for naming schema

* Namespace slugify()

* Drop slugify test

* Fix slugify test result

* Revert removing homerolled slugify

* Remove dupe macro name

* Update slugify macro to replace hyphens

* Update hyphen test

* Update other tests

* Fix order of regex

* Rename stub DB

* Revert "Add test change for naming schema"

This reverts commit 20e9954.
  • Loading branch information
dfsnow authored May 6, 2024
1 parent bd4bc17 commit 48c7178
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 45 deletions.
4 changes: 2 additions & 2 deletions dbt/macros/generate_schema_name.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
{%- set default_schema = target.schema -%}
{%- if target.name == "dev" -%}
{%- set schema_prefix = "z_dev_" ~ env_var_func("USER") ~ "_" -%}
{%- set schema_prefix = "z_dev_" ~ slugify(env_var_func("USER")) ~ "_" -%}
{%- elif target.name == "ci" -%}
{%- set github_head_ref = kebab_slugify(env_var_func("HEAD_REF")) -%}
{%- set github_head_ref = slugify(env_var_func("HEAD_REF")) -%}
{%- set schema_prefix = "z_ci_" ~ github_head_ref ~ "_" -%}
{%- else -%} {%- set schema_prefix = "" -%}
{%- endif -%}
Expand Down
10 changes: 5 additions & 5 deletions dbt/macros/slugify.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- Variation of dbt_utils.slugify macro using kebab-case instead of snake_case
{% macro kebab_slugify(string) %}
-- Variation of dbt_utils.slugify macro using strict snake_case
{% macro slugify(string) %}
{#- Lower case the string -#}
{% set string = string | lower %}

{#- Replace spaces, slashes, and underscores with hyphens -#}
{% set string = modules.re.sub("[ _/]+", "-", string) %}
{#- Replace spaces, slashes, and hyphens with underscores -#}
{% set string = modules.re.sub("[ /-]+", "_", string) %}

{#- Only take letters, numbers, and hyphens -#}
{% set string = modules.re.sub("[^a-z0-9-]+", "", string) %}
{% set string = modules.re.sub("[^a-z0-9_]+", "", string) %}

{{ return(string) }}
{% endmacro %}
6 changes: 3 additions & 3 deletions dbt/macros/tests/test_generate_schema_name.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% endmacro %}

{% macro mock_env_var(var_name) %}
{% if var_name == "USER" %} {{ return("testuser") }}
{% if var_name == "USER" %} {{ return("test-user") }}
{% elif var_name == "HEAD_REF" %} {{ return("testuser/feature-branch-1") }}
{% else %} {{ return("") }}
{% endif %}
Expand All @@ -22,7 +22,7 @@
mock_env_var,
exceptions.raise_compiler_error,
),
"z_dev_testuser_test",
"z_dev_test_user_test",
) %}
{% endmacro %}

Expand All @@ -36,7 +36,7 @@
mock_env_var,
exceptions.raise_compiler_error,
),
"z_ci_testuser-feature-branch-1_test",
"z_ci_testuser_feature_branch_1_test",
) %}
{% endmacro %}

Expand Down
52 changes: 19 additions & 33 deletions dbt/macros/tests/test_slugify.sql
Original file line number Diff line number Diff line change
@@ -1,52 +1,38 @@
{% macro test_slugify() %} {% do test_kebab_slugify() %} {% endmacro %}

{% macro test_kebab_slugify() %}
{% do test_kebab_slugify_lowercases_strings() %}
{% do test_kebab_slugify_replaces_spaces() %}
{% do test_kebab_slugify_replaces_slashes() %}
{% do test_kebab_slugify_replaces_underscores() %}
{% do test_kebab_slugify_removes_special_characters() %}
{% macro test_slugify() %}
{% do test_slugify_lowercases_strings() %}
{% do test_slugify_replaces_spaces() %}
{% do test_slugify_replaces_slashes() %}
{% do test_slugify_replaces_hyphens() %}
{% do test_slugify_removes_special_characters() %}
{% endmacro %}

{% macro test_kebab_slugify_lowercases_strings() %}
{{
assert_equals(
"test_kebab_slugify_lowercases_strings", kebab_slugify("TEST"), "test"
)
}}
{% macro test_slugify_lowercases_strings() %}
{{ assert_equals("test_slugify_lowercases_strings", slugify("TEST"), "test") }}
{% endmacro %}

{% macro test_kebab_slugify_replaces_spaces() %}
{{
assert_equals(
"test_kebab_slugify_replaces_spaces", kebab_slugify("t e s t"), "t-e-s-t"
)
}}
{% macro test_slugify_replaces_spaces() %}
{{ assert_equals("test_slugify_replaces_spaces", slugify("t e s t"), "t_e_s_t") }}
{% endmacro %}

{% macro test_kebab_slugify_replaces_slashes() %}
{{
assert_equals(
"test_kebab_slugify_replaces_slashes", kebab_slugify("t/e/s/t"), "t-e-s-t"
)
}}
{% macro test_slugify_replaces_slashes() %}
{{ assert_equals("test_slugify_replaces_slashes", slugify("t/e/s/t"), "t_e_s_t") }}
{% endmacro %}

{% macro test_kebab_slugify_replaces_underscores() %}
{% macro test_slugify_replaces_hyphens() %}
{{
assert_equals(
"test_kebab_slugify_replaces_underscores",
kebab_slugify("t_e_s_t"),
"t-e-s-t",
"test_slugify_replaces_hyphens",
slugify("t-e-s-t"),
"t_e_s_t",
)
}}
{% endmacro %}

{% macro test_kebab_slugify_removes_special_characters() %}
{% macro test_slugify_removes_special_characters() %}
{{
assert_equals(
"test_kebab_slugify_removes_special_characters",
kebab_slugify("t!@#e$%^s&*()t"),
"test_slugify_removes_special_characters",
slugify("t!@#e$%^s&*()t"),
"test",
)
}}
Expand Down
4 changes: 2 additions & 2 deletions dbt/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ athena:
s3_data_naming: schema_table
region_name: us-east-1
# "schema" here corresponds to a Glue database
schema: z_static_unused-dbt-stub-database
schema: z_static_unused_dbt_stub_database
# "database" here corresponds to a Glue data catalog
database: awsdatacatalog
threads: 5
Expand All @@ -19,7 +19,7 @@ athena:
s3_data_dir: s3://ccao-dbt-athena-ci-us-east-1/data/
s3_data_naming: schema_table
region_name: us-east-1
schema: z_static_unused-dbt-stub-database
schema: z_static_unused_dbt_stub_database
database: awsdatacatalog
threads: 5
num_retries: 1
Expand Down

0 comments on commit 48c7178

Please sign in to comment.