Skip to content

Commit

Permalink
Fix bugs in upsert search attribs (#440)
Browse files Browse the repository at this point in the history
Fixed some bugs in search attrib upsert that occur if the workflow has no initial search attribs
  • Loading branch information
Sushisource authored Nov 30, 2023
1 parent c47e3f1 commit 6f966c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
13 changes: 8 additions & 5 deletions temporalio/worker/_workflow_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ def workflow_upsert_search_attributes(
# To apply to typed search attributes we remove, replace, or add. We
# don't know any of the key types, so we do our best.
index = next(
i for i, a in enumerate(mut_typed_attrs) if a.key.name == k
(i for i, a in enumerate(mut_typed_attrs) if a.key.name == k), None
)
if not vals:
if index is not None:
Expand Down Expand Up @@ -1285,9 +1285,12 @@ def workflow_upsert_search_attributes(

# Update typed and untyped in info
index = next(
i
for i, a in enumerate(mut_typed_attrs)
if a.key.name == update.key.name
(
i
for i, a in enumerate(mut_typed_attrs)
if a.key.name == update.key.name
),
None,
)
if update.value is None:
# Delete
Expand All @@ -1301,7 +1304,7 @@ def workflow_upsert_search_attributes(
update.key, update.value
)
if index is None:
mut_typed_attrs.append()
mut_typed_attrs.append(pair)
else:
mut_typed_attrs[index] = pair
# Single-item list if not already a sequence for untyped
Expand Down
29 changes: 29 additions & 0 deletions tests/worker/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,35 @@ async def describe_attributes_typed(
assert updated_attrs_typed == await describe_attributes_typed(handle)


@workflow.defn
class NoSearchAttributesWorkflow:
@workflow.run
async def run(self) -> None:
workflow.upsert_search_attributes(
[
SearchAttributeWorkflow.text_attribute.value_set("text2"),
]
)
# All we need to do is complete


async def test_workflow_no_initial_search_attributes(client: Client, env_type: str):
if env_type != "local":
pytest.skip("Only testing search attributes on local which disables cache")
await ensure_search_attributes_present(
client,
SearchAttributeWorkflow.text_attribute,
)
async with new_worker(client, NoSearchAttributesWorkflow) as worker:
handle = await client.start_workflow(
NoSearchAttributesWorkflow.run,
id=f"workflow-{uuid.uuid4()}",
task_queue=worker.task_queue,
# importantly, no initial search attributes
)
await handle.result()


@workflow.defn
class LoggingWorkflow:
def __init__(self) -> None:
Expand Down

0 comments on commit 6f966c7

Please sign in to comment.