Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multivalue metafield macro support #99

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

fivetran-avinash
Copy link
Contributor

@fivetran-avinash fivetran-avinash commented Jan 27, 2025

PR Overview

This PR will address the following Issue/Feature: Internal Height ticket

This PR will result in the following new package version: 0.16.1

This is an update of a macro to catch new field values from an API update but does not change any of the existing fields.

Please provide the finalized CHANGELOG entry which details the relevant changes included in this PR:

Bug Fixes

  • Updated the get_metafields macro to support multiple reference values, ensuring compatibility with both the Shopify GraphQL API (ex: 'PRODUCTVARIANT' from the product_variant source) and the deprecated REST API (previously 'variant' for product_variant). See the Shopify API docs for more information.
    • Updated shopify__product_variant_metafields and shopify__product_image_metafields to ensure all metafields from stg_shopify__metafield are properly included, preventing loss of existing records.
    • Introduced id_column_override in get_metafields macros to to handle non-standard primary key column names (variant_id from stg_shopify__product_variant, product_image_id from stg_shopify__product_image where image is the default reference value). The override ensures models explicitly call the correct column names.

Under the Hood

  • Updated the shopify_metafield_data seed to validate the functionality of the get_metafields macro, ensuring it correctly retrieves metafield data for all supported reference values.

PR Checklist

Basic Validation

Please acknowledge that you have successfully performed the following commands locally:

  • dbt run –full-refresh && dbt test
  • [NA] dbt run (if incremental models are present) && dbt test

Before marking this PR as "ready for review" the following have been applied:

  • The appropriate issue has been linked, tagged, and properly assigned
  • All necessary documentation and version upgrades have been applied
  • docs were regenerated (unless this PR does not include any code or yml updates)
  • BuildKite integration tests are passing
  • Detailed validation steps have been provided below

Detailed Validation

Please share any and all of your validation steps:

Validated that metafields supported multiple field value types, using integration tests seed data on BigQuery, Postgres, and Snowflake.

Validation tests also passed.
Screenshot 2025-01-27 at 11 54 32 PM

If you had to summarize this PR in an emoji, which would it be?

🪛

@fivetran-avinash fivetran-avinash self-assigned this Jan 27, 2025
@fivetran-avinash fivetran-avinash marked this pull request as ready for review January 28, 2025 09:07
Copy link
Contributor

@fivetran-joemarkiewicz fivetran-joemarkiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fivetran-avinash thanks for these updates! Please see my comments and suggestions below.

I did end up going back on my idea to remove the id field. After a deeper investigation into the macro and the use case, I feel it would be best to explicitly define the id for each macro call.

macros/get_metafields.sql Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines 2 to 10
{{ return(adapter.dispatch('get_metafields', 'shopify')(
source_object=source_object,
reference_values=reference_values,
lookup_object=lookup_object,
key_field=key_field,
key_value=key_value,
reference_field=reference_field
)) }}
{%- endmacro %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we aligned in the past that the adapter.dispatch shouldn't include the default values as that can result in inconsistent results. Can you remove these default values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I can file a feature request to make similar updates to the recent Netsuite macro I created as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great, just to stay on top of ensuring our standards are consistent across the board.

macros/get_metafields.sql Outdated Show resolved Hide resolved
macros/get_metafields.sql Outdated Show resolved Hide resolved
macros/get_metafields.sql Outdated Show resolved Hide resolved
{% endif %}

{# Derive the _id column dynamically #}
{% set id_column = reference_values[0] ~ '_id' %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a unique approach! However, this makes me question future maintainability. Future contributors will be required to know that the first value in the list is what drives the _id and that isn't documented clearly anywhere. This is more a reflection on the initial design as it's a bit of a forced shortcut than a more robust design (speaking as the one who initially designed this 😆).

I know we discussed removing the id parameter, but seeing the nuances behind this macro, it seems to be the best design approach to ensure we clarify the id_column for each reference. This way it's extremely explicit and we're aware of these changes clearly moving forward.

This would apply to all the metafield macro calls and would not have a default value. Each metafield call would need to input the id column.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points about future maintenance. Simple code > gnarly code.

Updated.

macros/get_metafields.sql Outdated Show resolved Hide resolved
macros/get_metafields.sql Outdated Show resolved Hide resolved
models/metafields/shopify__product_image_metafields.sql Outdated Show resolved Hide resolved
Copy link
Contributor Author

@fivetran-avinash fivetran-avinash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fivetran-joemarkiewicz Changes addressed and ready for re-review.

Copy link
Contributor

@fivetran-joemarkiewicz fivetran-joemarkiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fivetran-avinash just one small comment for you to look at. Other than that, this looks good to go!

@@ -37,8 +53,8 @@ final as (
{% 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 }}'
on lookup_object.owner_resource_id = source_table.{{ id_column }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think we should keep the old approach for the owner_resource_id where it was {{ reference_field }}_id. I imagine it was parameterized for a reason and we should probably maintain that instead of hard coding owner_resource_id.

Let me know if you think differently.

Copy link
Contributor Author

@fivetran-avinash fivetran-avinash Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, applied!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants