Skip to content

Commit 1ad0657

Browse files
committed
fix(cli): fix plan ids for workflow files
1 parent 80f0268 commit 1ad0657

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

renku/command/schema/workflow_file.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ class Meta:
3333
model = WorkflowFilePlan
3434
unknown = marshmallow.EXCLUDE
3535

36+
@marshmallow.pre_dump(pass_many=True)
37+
def fix_ids(self, objs, many, **kwargs):
38+
"""Renku up to 2.4.1 had a bug that created wrong ids for workflow file entities, this fixes those on export."""
39+
40+
def _replace_id(obj):
41+
obj.unfreeze()
42+
obj.id = obj.id.replace("//plans/", "/")
43+
obj.freeze()
44+
45+
if many:
46+
for obj in objs:
47+
_replace_id(obj)
48+
return objs
49+
50+
_replace_id(objs)
51+
return objs
52+
3653

3754
class WorkflowFileCompositePlanSchema(CompositePlanSchema):
3855
"""Plan schema."""
@@ -46,3 +63,20 @@ class Meta:
4663

4764
path = fields.String(prov.atLocation)
4865
plans = fields.Nested(renku.hasSubprocess, WorkflowFilePlanSchema, many=True)
66+
67+
@marshmallow.pre_dump(pass_many=True)
68+
def fix_ids(self, objs, many, **kwargs):
69+
"""Renku up to 2.4.1 had a bug that created wrong ids for workflow file entities, this fixes those on export."""
70+
71+
def _replace_id(obj):
72+
obj.unfreeze()
73+
obj.id = obj.id.replace("//plans/", "/")
74+
obj.freeze()
75+
76+
if many:
77+
for obj in objs:
78+
_replace_id(obj)
79+
return objs
80+
81+
_replace_id(objs)
82+
return objs

renku/domain_model/workflow/workflow_file.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,30 @@ def __init__(self, *, path: Union[Path, str], **kwargs):
3737
self.path: str = str(path)
3838

3939
@staticmethod
40-
def generate_id(path: Optional[Union[Path, str]] = None, sequence: Optional[int] = None, **_) -> str:
40+
def generate_id(
41+
path: Optional[Union[Path, str]] = None, sequence: Optional[int] = None, uuid_only: bool = False, **_
42+
) -> str:
4143
"""Generate an identifier for Plan."""
4244
assert path, "Path is needed to generate id for WorkflowFileCompositePlan"
4345

4446
# NOTE: Workflow file's root composite plan's ID is generated only based on the file's path. The ID might be
4547
# changed later if the plan is a derivative
4648
key = f"{path}" if sequence is None else f"{path}::{sequence}"
4749
key_bytes = key.encode("utf-8")
48-
return CompositePlan.generate_id(uuid=hashlib.md5(key_bytes).hexdigest()[:32]) # nosec
50+
51+
uuid = hashlib.md5(key_bytes).hexdigest()[:32] # nosec
52+
53+
if uuid_only:
54+
return uuid
55+
return CompositePlan.generate_id(uuid=uuid)
4956

5057
def assign_new_id(self, *, sequence: Optional[int] = None, **_) -> str:
5158
"""Assign a new UUID or a deterministic."""
52-
new_id = uuid.uuid4().hex if sequence is None else self.generate_id(path=self.path, sequence=sequence)
59+
new_id = (
60+
uuid.uuid4().hex
61+
if sequence is None
62+
else self.generate_id(path=self.path, sequence=sequence, uuid_only=True)
63+
)
5364
return super().assign_new_id(uuid=new_id)
5465

5566
def is_equal_to(self, other: WorkflowFileCompositePlan) -> bool:
@@ -67,15 +78,22 @@ def __init__(self, *, path: Union[Path, str], **kwargs):
6778

6879
@staticmethod
6980
def generate_id(
70-
path: Optional[Union[Path, str]] = None, name: Optional[str] = None, sequence: Optional[int] = None, **_
81+
path: Optional[Union[Path, str]] = None,
82+
name: Optional[str] = None,
83+
sequence: Optional[int] = None,
84+
uuid_only: bool = True,
85+
**_,
7186
) -> str:
7287
"""Generate an identifier for Plan."""
7388
assert path, "Path is needed to generate id for WorkflowFilePlan"
7489
assert name, "Name is needed to generate id for WorkflowFilePlan"
7590

7691
key = f"{path}::{name}" if sequence is None else f"{path}::{name}::{sequence}"
7792
key_bytes = key.encode("utf-8")
78-
return Plan.generate_id(uuid=hashlib.md5(key_bytes).hexdigest()[:32]) # nosec
93+
uuid = hashlib.md5(key_bytes).hexdigest()[:32] # nosec
94+
if uuid_only:
95+
return uuid
96+
return Plan.generate_id(uuid=uuid)
7997

8098
@staticmethod
8199
def validate_name(name: str):
@@ -92,7 +110,7 @@ def assign_new_id(self, *, sequence: Optional[int] = None, **_) -> str:
92110
new_id = (
93111
uuid.uuid4().hex
94112
if sequence is None
95-
else self.generate_id(path=self.path, name=self.name, sequence=sequence)
113+
else self.generate_id(path=self.path, name=self.name, sequence=sequence, uuid_only=True)
96114
)
97115
return super().assign_new_id(uuid=new_id)
98116

0 commit comments

Comments
 (0)