Skip to content

Commit

Permalink
Merge pull request #237 from dbt-labs/bug/directory_pattern
Browse files Browse the repository at this point in the history
Bug/directory pattern
  • Loading branch information
graciegoheen authored Dec 23, 2022
2 parents 994e52b + 7a80691 commit a4bf238
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
25 changes: 25 additions & 0 deletions macros/get_directory_pattern.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- these macros will read a user’s home environment and detect whether a computer’s operating system is Windows based or Mac/Linux, and display the right directory pattern.
{% macro get_directory_pattern() %}
{%- set env_var_home_exists = env_var("HOME", "not_set") != "not_set" -%}
{%- set on_mac_or_linux = env_var_home_exists and "\\\\" not in env_var("HOME") -%}
{%- if on_mac_or_linux -%}
{{ return("/") }}
{% else %}
{{ return("\\\\") }}
{% endif %}
{% endmacro %}

{% macro get_regexp_directory_pattern() %}
{% set regexp_escaped = get_directory_pattern() | replace("\\\\", "\\\\\\\\") %}
{% do return(regexp_escaped) %}
{% endmacro %}

{% macro get_dbtreplace_directory_pattern() %}
{%- set env_var_home_exists = env_var("HOME", "not_set") != "not_set" -%}
{%- set on_mac_or_linux = env_var_home_exists and "\\\\" not in env_var("HOME") -%}
{%- if on_mac_or_linux -%}
{{ dbt.replace("file_path", "regexp_replace(file_path,'.*/','')", "''") }}
{% else %}
{{ dbt.replace("file_path", "regexp_replace(file_path,'.*\\\\\\\\','')", "''") }}
{% endif %}
{% endmacro %}
8 changes: 4 additions & 4 deletions models/marts/core/int_all_graph_resources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{%- do test_macro_list.append(test) -%}
{%- endfor %}
{%- endfor -%}

with unioned as (

{{ dbt_utils.union_relations([
Expand Down Expand Up @@ -38,8 +38,8 @@ unioned_with_calc as (
when resource_type = 'source' then null
else {{ dbt.split_part('name', "'_'", 1) }}||'_'
end as prefix,
{{ dbt.replace("file_path", "regexp_replace(file_path,'.*/','')", "''") }} as directory_path,
regexp_replace(file_path,'.*/','') as file_name
{{ get_dbtreplace_directory_pattern() }} as directory_path,
regexp_replace(file_path,'.*{{ get_regexp_directory_pattern() }}','') as file_name
from unioned
where coalesce(is_enabled, True) = True and package_name != 'dbt_project_evaluator'
),
Expand Down Expand Up @@ -125,4 +125,4 @@ final as (

select
*
from final
from final
11 changes: 7 additions & 4 deletions models/marts/structure/fct_model_directories.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-- This model finds all cases where a model is NOT in the appropriate subdirectory:
-- For staging models: The files should be in nested in the staging folder in a subfolder that matches their source parent's name.
-- For non-staging models: The files should be nested closest to their appropriate folder.

{% set directory_pattern = get_directory_pattern() %}

with all_graph_resources as (
select * from {{ ref('int_all_graph_resources') }}
),
Expand Down Expand Up @@ -36,7 +37,7 @@ inappropriate_subdirectories_staging as (
child_resource_type as resource_type,
child_model_type as model_type,
child_file_path as current_file_path,
'models/' || '{{ var("staging_folder_name") }}' || '/' || parent_source_name || '/' || child_file_name as change_file_path_to
'models{{ directory_pattern }}' || '{{ var("staging_folder_name") }}' || '{{ directory_pattern }}' || parent_source_name || '{{ directory_pattern }}' || child_file_name as change_file_path_to
from staging_models
where child_directory_path not like '%' || parent_source_name || '%'
),
Expand All @@ -48,7 +49,7 @@ innappropriate_subdirectories_non_staging_models as (
all_graph_resources.resource_type,
all_graph_resources.model_type,
all_graph_resources.file_path as current_file_path,
'models' || '/.../' || folders.folder_name_value || '/.../' || all_graph_resources.file_name as change_file_path_to
'models' || '{{ directory_pattern }}...{{ directory_pattern }}' || folders.folder_name_value || '{{ directory_pattern }}...{{ directory_pattern }}' || all_graph_resources.file_name as change_file_path_to
from all_graph_resources
left join folders
on folders.model_type = all_graph_resources.model_type
Expand All @@ -64,4 +65,6 @@ unioned as (

select * from unioned

{{ filter_exceptions(this) }}
{{ filter_exceptions(this) }}


4 changes: 3 additions & 1 deletion models/marts/structure/fct_source_directories.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

-- This model finds all cases where a source definition is NOT in the appropriate subdirectory.

{% set directory_pattern = get_directory_pattern() %}

with all_graph_resources as (
select * from {{ ref('int_all_graph_resources') }}
),
Expand All @@ -12,7 +14,7 @@ inappropriate_subdirectories_sources as (
resource_name,
resource_type,
file_path as current_file_path,
'models/' || '{{ var("staging_folder_name") }}' || '/' || source_name || '/' || file_name as change_file_path_to
'models{{ directory_pattern }}' || '{{ var("staging_folder_name") }}' || '{{ directory_pattern }}' || source_name || '{{ directory_pattern }}' || file_name as change_file_path_to
from all_graph_resources
where resource_type = 'source'
and directory_path not like '%' || source_name || '%'
Expand Down

0 comments on commit a4bf238

Please sign in to comment.