Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
import_util: Filter out stale records from failed parallel downloads.
PieterCK8f1d64748aea
diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.pyindex b00b0885532..2ef94fbf7cb 100644--- a/zerver/data_import/import_util.py+++ b/zerver/data_import/import_util.py@@ -583,6 +583,12 @@ class AttachmentRecordData:size: int+@dataclass+class ScrubbedRecords:+ zerver_attachments: list[AttachmentRecordData]+ upload_records: list[UploadRecordData]++def build_attachment(realm_id: int,message_ids: set[int],@@ -1006,3 +1012,26 @@ class ImportedBotEmail:cls.assigned_email[bot_id] = emailreturn email+++def scrub_missing_upload_records_after_download(+ output_dir: str,+ zerver_attachments: list[AttachmentRecordData],+ upload_records: list[UploadRecordData],+) -> ScrubbedRecords:+ """+ This filters out any orphaned records in zerver_attachment and upload_record+ due to failed parallel download.+ """+ return ScrubbedRecords(+ zerver_attachments=[+ attachment_record+ for attachment_record in zerver_attachments+ if os.path.isfile(os.path.join(output_dir, "uploads", attachment_record.path_id))+ ],+ upload_records=[+ upload_record+ for upload_record in upload_records+ if os.path.isfile(os.path.join(output_dir, "uploads", upload_record.path))+ ],+ )diff --git a/zerver/data_import/microsoft_teams.py b/zerver/data_import/microsoft_teams.pyindex b4c11a33a26..a2b5480e68b 100644--- a/zerver/data_import/microsoft_teams.py+++ b/zerver/data_import/microsoft_teams.py@@ -35,6 +35,7 @@ from zerver.data_import.import_util import (get_data_file,make_subscriber_map,request_file_stream,+ scrub_missing_upload_records_after_download,validate_user_emails_for_import,write_response_file_stream_to_path,)@@ -796,6 +797,15 @@ def convert_messages()dump_file_id += 1+ scrubbed_data = scrub_missing_upload_records_after_download(+ output_dir,+ # Attachment record data is built inside download workers only+ # on successful downloads, so there should be no stale records.+ zerver_attachments=[],+ upload_records=total_accumulated_upload_records,+ )+ total_accumulated_upload_records = scrubbed_data.upload_records+def fix_attachment_id(attachment: AttachmentRecordData) -> AttachmentRecordData:attachment.id = NEXT_ID("attachment")return attachmentdiff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.pyindex 9657ad82fc0..d018bacc257 100644--- a/zerver/data_import/slack.py+++ b/zerver/data_import/slack.py@@ -47,6 +47,7 @@ from zerver.data_import.import_util import (process_avatars,process_emojis,request_file_stream,+ scrub_missing_upload_records_after_download,validate_user_emails_for_import,write_response_file_stream_to_path,)@@ -1791,6 +1792,16 @@ def do_convert_directory(do_download_and_export_upload_file,)+ # Attachment and upload records are built before the download runs,+ # so records for files whose download failed will be invalid.+ # This filters out such records after the download process has+ # completed.+ scrubbed_data = scrub_missing_upload_records_after_download(+ output_dir, zerver_attachment, uploads_list+ )+ zerver_attachment = scrubbed_data.zerver_attachments+ uploads_list = scrubbed_data.upload_records+# Move zerver_reactions to realm.json filerealm["zerver_reaction"] = reactionsdiff --git a/zerver/tests/test_microsoft_teams_importer.py b/zerver/tests/test_microsoft_teams_importer.pyindex fb9ad5f5a22..289ca27e756 100644--- a/zerver/tests/test_microsoft_teams_importer.py+++ b/zerver/tests/test_microsoft_teams_importer.py@@ -11,6 +11,7 @@ from functools import wrapsfrom typing import Any, Concatenate, TypeAliasfrom urllib.parse import parse_qs, urlsplit+import orjsonimport responsesfrom django.utils.timezone import now as timezone_nowfrom requests import PreparedRequest@@ -226,6 +227,12 @@ def mock_microsoft_graph_api_calls("microsoft_graph_api_response_fixtures",),)+ # Add one failed download to test scrub_uploads_records_after_download.+ responses.add(+ responses.GET,+ "https://graph.microsoft.com/v1.0/teams/1d513e46-d8cd-41db-b84f-381fe5730794/channels/19:[email protected]/messages/1755003422344/hostedContents/aWQ9eF8wLXd1cy1kMi1iNGM5NjhiYmNhMDU1OWViYWFkMzdiMGUzMzBmMzY5MSx0eXBlPTEsdXJsPWh0dHBzOi8vdXMtYXBpLmFzbS5za3lwZS5jb20vdjEvb2JqZWN0cy8wLXd1cy1kMi1iNGM5NjhiYmNhMDU1OWViYWFkMzdiMGUzMzBmMzY5MS92aWV3cy9pbWdv/$value",+ status=400,+ )responses.add(responses.GET,re.compile(HOSTED_CONTENT_GRAPH_API_URL_REGEX),@@ -265,13 +272,36 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):fixture_file_path = self.fixture_file_name(fixture_folder, "microsoft_teams_fixtures")if not os.path.isdir(fixture_file_path):raise AssertionError(f"Fixture file not found: {fixture_file_path}")- with self.assertLogs(level="INFO"), self.settings(EXTERNAL_HOST="zulip.example.com"):+ with self.assertLogs(level="INFO") as log, self.settings(EXTERNAL_HOST="zulip.example.com"):do_convert_directory(fixture_file_path,self.converted_file_output_dir,"MICROSOFT_GRAPH_API_TOKEN",processes=1,)+ with self.subTest("no records for failed downloads"):+ self.assertIn("INFO:root:HTTP error: 400, Response: ", log.output)+ uploads_records_path = os.path.join(+ self.converted_file_output_dir, "uploads", "records.json"+ )+ with open(uploads_records_path, "rb") as f:+ upload_records = orjson.loads(f.read())++ for record in upload_records:+ file_path = os.path.join(self.converted_file_output_dir, "uploads", record["path"])+ self.assertTrue(os.path.isfile(file_path))++ attachment_records_path = os.path.join(+ self.converted_file_output_dir, "attachment.json"+ )+ with open(attachment_records_path, "rb") as f:+ attachment_records = orjson.loads(f.read())++ for record in attachment_records["zerver_attachment"]:+ file_path = os.path.join(+ self.converted_file_output_dir, "uploads", record["path_id"]+ )+ self.assertTrue(os.path.isfile(file_path))def import_microsoft_teams_export_fixture(self, fixture_folder: str) -> None:self.convert_microsoft_teams_export_fixture(fixture_folder)