From 543807c27de5be617e8addd5e19bfe30aad07482 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Jul 04 2018 05:53:41 +0000 Subject: handle invalid 'subject' values during migration Fixes #210. --- diff --git a/waiverdb/migrations/versions/f6bc296ba966_subject_dict_to_type_identifier.py b/waiverdb/migrations/versions/f6bc296ba966_subject_dict_to_type_identifier.py index d742520..3270ae3 100644 --- a/waiverdb/migrations/versions/f6bc296ba966_subject_dict_to_type_identifier.py +++ b/waiverdb/migrations/versions/f6bc296ba966_subject_dict_to_type_identifier.py @@ -35,7 +35,16 @@ def upgrade(): connection = op.get_bind() rows = connection.execute(select([waiver_table.c.id, waiver_table.c.subject])) for waiver_id, subject in rows: - subject_type, subject_identifier = subject_dict_to_type_identifier(subject) + try: + subject_type, subject_identifier = subject_dict_to_type_identifier(subject) + except ValueError: + # The 'subject' value might be invalid, see: https://pagure.io/waiverdb/issue/210 + # Let's map it to something which is valid but will never match + # anything Greenwave is looking for. + # Note that the original, invalid 'subject' value is still + # preserved in the row in case of downgrade. So we are not losing + # any data here. + subject_type, subject_identifier = 'koji_build', '' connection.execute(update(waiver_table) .where(waiver_table.c.id == waiver_id) .values(subject_type=subject_type, diff --git a/waiverdb/models/waivers.py b/waiverdb/models/waivers.py index 00bd4a5..4150390 100644 --- a/waiverdb/models/waivers.py +++ b/waiverdb/models/waivers.py @@ -11,13 +11,17 @@ def subject_dict_to_type_identifier(subject): Now we expect a specific type and identifier. This maps from the old style to the new, for backwards compatibility. """ - if subject.get('type') == 'bodhi_update' and 'item' in subject: + if (subject.get('type') == 'bodhi_update' and + 'item' in subject and + isinstance(subject['item'], str)): return ('bodhi_update', subject['item']) - elif subject.get('type') in ['koji_build', 'brew-build'] and 'item' in subject: + elif (subject.get('type') in ['koji_build', 'brew-build'] and + 'item' in subject and + isinstance(subject['item'], str)): return ('koji_build', subject['item']) - elif 'original_spec_nvr' in subject: + elif 'original_spec_nvr' in subject and isinstance(subject['original_spec_nvr'], str): return ('koji_build', subject['original_spec_nvr']) - elif 'productmd.compose.id' in subject: + elif 'productmd.compose.id' in subject and isinstance(subject['productmd.compose.id'], str): return ('compose', subject['productmd.compose.id']) else: raise ValueError('Unrecognised subject type: %r' % subject)