Handle long format context UUIDs during migration (#91657)

In https://github.com/home-assistant/core/issues/91514 is was discovered
these exist in older versions
pull/91680/head
J. Nick Koston 2023-04-19 02:40:04 -10:00 committed by GitHub
parent 162c36f108
commit 408b2171ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -1364,10 +1364,9 @@ def _context_id_to_bytes(context_id: str | None) -> bytes | None:
# ULIDs that filled the column to the max length
# so we need to catch the ValueError and return
# None if it happens
if len(context_id) == 32:
return UUID(context_id).bytes
if len(context_id) == 26:
return ulid_to_bytes(context_id)
return UUID(context_id).bytes
return None

View File

@ -326,6 +326,16 @@ async def test_migrate_states_context_ids(
context_parent_id=None,
context_parent_id_bin=None,
),
States(
entity_id="state.human_readable_uuid_context_id",
last_updated_ts=1677721632.552529,
context_id="0ae29799-ee4e-4f45-8116-f582d7d3ee65",
context_id_bin=None,
context_user_id="0ae29799-ee4e-4f45-8116-f582d7d3ee65",
context_user_id_bin=None,
context_parent_id="0ae29799-ee4e-4f45-8116-f582d7d3ee65",
context_parent_id_bin=None,
),
)
)
@ -351,12 +361,13 @@ async def test_migrate_states_context_ids(
"state.ulid_context_id",
"state.invalid_context_id",
"state.garbage_context_id",
"state.human_readable_uuid_context_id",
]
)
)
.all()
)
assert len(events) == 5
assert len(events) == 6
return {state.entity_id: _object_as_dict(state) for state in events}
states_by_entity_id = await instance.async_add_executor_job(_fetch_migrated_states)
@ -409,6 +420,25 @@ async def test_migrate_states_context_ids(
assert garbage_context_id["context_user_id_bin"] is None
assert garbage_context_id["context_parent_id_bin"] is None
human_readable_uuid_context_id = states_by_entity_id[
"state.human_readable_uuid_context_id"
]
assert human_readable_uuid_context_id["context_id"] is None
assert human_readable_uuid_context_id["context_user_id"] is None
assert human_readable_uuid_context_id["context_parent_id"] is None
assert (
human_readable_uuid_context_id["context_id_bin"]
== b"\n\xe2\x97\x99\xeeNOE\x81\x16\xf5\x82\xd7\xd3\xeee"
)
assert (
human_readable_uuid_context_id["context_user_id_bin"]
== b"\n\xe2\x97\x99\xeeNOE\x81\x16\xf5\x82\xd7\xd3\xeee"
)
assert (
human_readable_uuid_context_id["context_parent_id_bin"]
== b"\n\xe2\x97\x99\xeeNOE\x81\x16\xf5\x82\xd7\xd3\xeee"
)
@pytest.mark.parametrize("enable_migrate_event_type_ids", [True])
async def test_migrate_event_type_ids(