-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from fivetran/feature/metafields
feature/metafields
- Loading branch information
Showing
15 changed files
with
163 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
target/ | ||
dbt_modules/ | ||
logs/ | ||
|
||
env/ | ||
dbt_packages/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{% macro get_metafields(source_object, reference_value, lookup_object="stg_shopify__metafield", key_field="metafield_reference", key_value="value", reference_field="owner_resource") %} | ||
|
||
{% set pivot_fields = dbt_utils.get_column_values(table=ref(lookup_object), column=key_field, where="lower(" ~ reference_field ~ ") = lower('" ~ reference_value ~ "')") %} | ||
|
||
{% set source_columns = adapter.get_columns_in_relation(ref(source_object)) %} | ||
{% set source_column_count = source_columns | length %} | ||
|
||
with source_table as ( | ||
select * | ||
from {{ ref(source_object) }} | ||
) | ||
|
||
{% if pivot_fields is not none %}, | ||
lookup_object as ( | ||
select | ||
*, | ||
{{ dbt_utils.pivot( | ||
column=key_field, | ||
values=pivot_fields, | ||
agg='', | ||
then_value=key_value, | ||
else_value="null", | ||
quote_identifiers=false | ||
) | ||
}} | ||
from {{ ref(lookup_object) }} | ||
where is_most_recent_record | ||
), | ||
|
||
final as ( | ||
select | ||
{% for column in source_columns %} | ||
source_table.{{ column.name }}{% if not loop.last %},{% endif %} | ||
{% endfor %} | ||
{% for fields in pivot_fields %} | ||
, max(lookup_object.{{ dbt_utils.slugify(fields) }}) as metafield_{{ dbt_utils.slugify(fields) }} | ||
{% endfor %} | ||
from source_table | ||
left join lookup_object | ||
on lookup_object.{{ reference_field }}_id = source_table.{{ reference_value }}_id | ||
and lookup_object.{{ reference_field }} = '{{ reference_value }}' | ||
{{ dbt_utils.group_by(source_column_count) }} | ||
) | ||
|
||
select * | ||
from final | ||
{% else %} | ||
|
||
select * | ||
from source_table | ||
{% endif %} | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_collection_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__collection", | ||
reference_value = 'collection') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_customer_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__customer", | ||
reference_value = 'customer') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_order_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__order", | ||
reference_value = 'order') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_product_image_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__product_image", | ||
reference_value = 'image') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_product_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__product", | ||
reference_value = 'product') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_product_variant_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__product_variant", | ||
reference_value = 'variant') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{ config(enabled=var('shopify_using_all_metafields', False) or var('shopify_using_shop_metafields', False)) }} | ||
|
||
{{ get_metafields( | ||
source_object = "stg_shopify__shop", | ||
reference_value = 'shop') | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: shopify__collection_metafields | ||
description: Replica of the stg_shopify__collection model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__collection") | ||
|
||
- name: shopify__customer_metafields | ||
description: Replica of the stg_shopify__customer model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__customer") | ||
|
||
- name: shopify__order_metafields | ||
description: Replica of the stg_shopify__order model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__order") | ||
|
||
- name: shopify__product_image_metafields | ||
description: Replica of the stg_shopify__product_image model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__product_image") | ||
|
||
- name: shopify__product_metafields | ||
description: Replica of the stg_shopify__product model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__product") | ||
|
||
- name: shopify__product_variant_metafields | ||
description: Replica of the stg_shopify__product_variant model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__product_variant") | ||
|
||
- name: shopify__shop_metafields | ||
description: Replica of the stg_shopify__shop model with the addition of metafields pivoted out from the stg_shopify__metafield model. | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_equal_other_table: | ||
compare_model: ref("stg_shopify__shop") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters