Skip to content

Commit

Permalink
Merge pull request #1769 from 0xbe7a/fix-additional_zip_keys
Browse files Browse the repository at this point in the history
save default values additional_zip_keys for subsequent migrations
  • Loading branch information
isuruf authored Sep 20, 2023
2 parents 01f927d + 0f49b53 commit d8366e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
35 changes: 27 additions & 8 deletions conda_smithy/variant_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def op_variant_key_add(v1: dict, v2: dict):
primary_key = v2["__migrator"]["primary_key"]
additional_zip_keys = v2["__migrator"].get("additional_zip_keys", [])

ordering = v2["__migrator"].get("ordering", {})
if primary_key not in v2:
return v1
if primary_key not in v1:
raise RuntimeError("unhandled")

newly_added_zip_keys = set()

result = v1.copy()
Expand All @@ -155,11 +161,13 @@ def op_variant_key_add(v1: dict, v2: dict):
)
newly_added_zip_keys.update([primary_key] + additional_zip_keys)

ordering = v2["__migrator"].get("ordering", {})
if primary_key not in v2:
return v1
if primary_key not in v1:
raise RuntimeError("unhandled")
for key in newly_added_zip_keys:
# store the default value for the key, so that subsequent
# key additions don't need to specify them and continue to use the default value
# assert len(v1[key]) == 1
result.setdefault("__additional_zip_keys_default_values", {})[
additional_key
] = result[additional_key][0]

for pkey_ind, pkey_val in enumerate(v2[primary_key]):
# object is present already, ignore everything
Expand Down Expand Up @@ -188,16 +196,27 @@ def op_variant_key_add(v1: dict, v2: dict):
continue

# Transform key to zip_key if required
# assert len(v1[key]) == 1
if key in newly_added_zip_keys:
result[key] = [result[key][0]] * len(new_keys)
default_value = result[
"__additional_zip_keys_default_values"
][key]
result[key] = [default_value] * len(new_keys)

# Create a new version of the key from
# assert len(v2[key]) == 1
new_value = [None] * len(new_keys)
for i, j in position_map.items():
new_value[j] = result[key][i]
new_value[new_key_position] = v2[key][pkey_ind]

if key in v2:
new_value[new_key_position] = v2[key][pkey_ind]
elif key in result.get(
"__additional_zip_keys_default_values", {}
):
new_value[new_key_position] = result[
"__additional_zip_keys_default_values"
][key]

result[key] = new_value

return result
Expand Down
23 changes: 23 additions & 0 deletions news/1769-fix-additional_zip_keys.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed additional_zip_keys, so that subsequent migrations don't break.

**Security:**

* <news item>

0 comments on commit d8366e3

Please sign in to comment.