fix(migration): always save media_key/parent_media_key to variant_attributes for MEDIA_LINK image matching
This commit is contained in:
parent
d854bc3284
commit
3d7c2dec0a
1 changed files with 35 additions and 6 deletions
|
|
@ -485,6 +485,8 @@ class UpsertExecutor:
|
|||
all_attributes[key] = val
|
||||
|
||||
# Filter meaningful attributes for variant display & mapping (ignore placeholders & boolean flags)
|
||||
# NOTE: media_key/parent_media_key are intentionally excluded here to keep specs display clean,
|
||||
# but they MUST be saved separately below so the MEDIA_LINK phase can match them.
|
||||
PLACEHOLDER_VALUES = {"n/a", "na", "none", "null", "-", "true", "false", "undefined"}
|
||||
clean_attributes = {
|
||||
k: str(v).strip() for k, v in all_attributes.items()
|
||||
|
|
@ -520,20 +522,47 @@ class UpsertExecutor:
|
|||
if attr_type_cache is not None:
|
||||
attr_type_cache[attr_code] = attr_type
|
||||
|
||||
# Link VariantAttribute (canonicalizing media_key and parent_media_key values)
|
||||
clean_attr_val = str(attr_val)
|
||||
if attr_code in ("media_key", "parent_media_key"):
|
||||
clean_attr_val = MediaResolver.canonicalize_media_key(attr_val) or clean_attr_val
|
||||
|
||||
va_id = str(ulid.ULID())
|
||||
var_attr = VariantAttribute(
|
||||
id=va_id,
|
||||
variant_id=variant.variant_id,
|
||||
attribute_id=attr_type.attribute_id,
|
||||
attribute_value=clean_attr_val
|
||||
attribute_value=str(attr_val)
|
||||
)
|
||||
db_session.add(var_attr)
|
||||
|
||||
# CRITICAL: Always save media_key and parent_media_key separately.
|
||||
# They are excluded from clean_attributes (to keep variant specs display clean)
|
||||
# but MUST exist in variant_attributes for MEDIA_LINK to match and link images.
|
||||
for mk_code, mk_name in (("media_key", "media_key"), ("parent_media_key", "parent_media_key")):
|
||||
raw_mk_val = all_attributes.get(mk_code)
|
||||
if raw_mk_val:
|
||||
canonical_val = MediaResolver.canonicalize_media_key(str(raw_mk_val)) or str(raw_mk_val)
|
||||
if canonical_val:
|
||||
mk_attr_type = None
|
||||
if attr_type_cache is not None and mk_code in attr_type_cache:
|
||||
mk_attr_type = attr_type_cache[mk_code]
|
||||
else:
|
||||
mk_attr_type = db_session.query(AttributeType).filter(AttributeType.code == mk_code).first()
|
||||
if not mk_attr_type:
|
||||
mk_at_id = str(ulid.ULID())
|
||||
mk_attr_type = AttributeType(
|
||||
attribute_id=mk_at_id,
|
||||
name=mk_name,
|
||||
code=mk_code,
|
||||
status="active"
|
||||
)
|
||||
db_session.add(mk_attr_type)
|
||||
if attr_type_cache is not None:
|
||||
attr_type_cache[mk_code] = mk_attr_type
|
||||
|
||||
db_session.add(VariantAttribute(
|
||||
id=str(ulid.ULID()),
|
||||
variant_id=variant.variant_id,
|
||||
attribute_id=mk_attr_type.attribute_id,
|
||||
attribute_value=canonical_val
|
||||
))
|
||||
|
||||
# 6. Attach Variant Images via Smart 3-Tier Canonical Media Key Resolver
|
||||
raw_media_key = mapped_row.get("media_key") or mapped_row.get("parent_media_key")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue