From 42ecabe967d28a1f855e61ba9b47ac487434574b Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 6 Dec 2021 22:08:06 +0000 Subject: [PATCH] export: Add check_metadata flag. --- zerver/lib/export.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 742edf8dbc..24e6bed348 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -1455,25 +1455,31 @@ def export_files_from_s3( key = bucket.Object(bkey.key) - if "realm_id" not in key.metadata: - raise AssertionError(f"Missing realm_id in key metadata: {key.metadata}") + """ + For very old realms we may not have proper metadata. If you really need + an export to bypass these checks, flip the following flag. + """ + checking_metadata = True + if checking_metadata: + if "realm_id" not in key.metadata: + raise AssertionError(f"Missing realm_id in key metadata: {key.metadata}") - if "user_profile_id" not in key.metadata: - raise AssertionError(f"Missing user_profile_id in key metadata: {key.metadata}") + if "user_profile_id" not in key.metadata: + raise AssertionError(f"Missing user_profile_id in key metadata: {key.metadata}") - if int(key.metadata["user_profile_id"]) not in user_ids: - continue + if int(key.metadata["user_profile_id"]) not in user_ids: + continue - # This can happen if an email address has moved realms - if key.metadata["realm_id"] != str(realm.id): - if email_gateway_bot is None or key.metadata["user_profile_id"] != str( - email_gateway_bot.id - ): - raise AssertionError( - f"Key metadata problem: {key.key} / {key.metadata} / {realm.id}" - ) - # Email gateway bot sends messages, potentially including attachments, cross-realm. - print(f"File uploaded by email gateway bot: {key.key} / {key.metadata}") + # This can happen if an email address has moved realms + if key.metadata["realm_id"] != str(realm.id): + if email_gateway_bot is None or key.metadata["user_profile_id"] != str( + email_gateway_bot.id + ): + raise AssertionError( + f"Key metadata problem: {key.key} / {key.metadata} / {realm.id}" + ) + # Email gateway bot sends messages, potentially including attachments, cross-realm. + print(f"File uploaded by email gateway bot: {key.key} / {key.metadata}") record = _get_exported_s3_record(bucket_name, key, processing_emoji)