From 76c4c60b0d8cfbc223df563fa969e06ea89825c6 Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 5 Jan 2026 14:09:41 -0600 Subject: [PATCH] fix: add tts_failed and render_failed to MongoDB schema validator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MongoDB was rejecting status updates to 'tts_failed' and 'render_failed' because these values weren't in the schema validator's enum, even though they were defined in the Python JobStatus model. This caused TTS failures to leave jobs stuck in 'tts_generating' status with no error feedback to users - the WriteError from MongoDB prevented the status and error fields from being updated. The migration adds both failed statuses to the jobs collection validator. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- ...n_2026-01-05-000000_add_failed_statuses.py | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 backend/app/migrations/scripts/migration_2026-01-05-000000_add_failed_statuses.py diff --git a/backend/app/migrations/scripts/migration_2026-01-05-000000_add_failed_statuses.py b/backend/app/migrations/scripts/migration_2026-01-05-000000_add_failed_statuses.py new file mode 100644 index 0000000..d0718fe --- /dev/null +++ b/backend/app/migrations/scripts/migration_2026-01-05-000000_add_failed_statuses.py @@ -0,0 +1,114 @@ +"""Add tts_failed and render_failed statuses to jobs collection validator. + +These statuses were defined in the Python model but missing from the MongoDB +schema validation, causing WriteError when setting job status to failed states. +""" + +from app.migrations.migrator import Migration + + +class Migration(Migration): + """Update MongoDB schema validator to support tts_failed and render_failed statuses.""" + + def __init__(self): + super().__init__() + self.version = "2026-01-05-000000" + self.description = "Add tts_failed and render_failed statuses to jobs collection" + + async def up(self) -> None: + """Update the jobs collection validator to include failed statuses.""" + + # Define updated schema validator with failed statuses + validator = { + "$jsonSchema": { + "bsonType": "object", + "required": ["_id", "title", "status", "client_id", "created_at", "updated_at"], + "properties": { + "_id": {"bsonType": "string"}, + "title": {"bsonType": "string"}, + "status": { + "enum": [ + "created", + "ingesting", + "ai_processing", + "pending_qc", + "approved_english", + "approved_source", + "rejected", + "qc_feedback", + "translating", + "tts_generating", + "tts_failed", # NEW: TTS synthesis failed after retries + "rendering_video", + "render_failed", # NEW: Accessible video rendering failed + "pending_final_review", + "completed" + ] + }, + "client_id": {"bsonType": "string"}, + "created_at": {"bsonType": "date"}, + "updated_at": {"bsonType": "date"} + } + } + } + + # Update the collection validator + try: + await self.db.command({ + "collMod": "jobs", + "validator": validator, + "validationLevel": "moderate", + "validationAction": "error" + }) + print(f" Updated jobs collection validator") + except Exception as e: + print(f" Could not update validator: {e}") + raise + + print(f" Applied migration {self.version}: {self.description}") + + async def down(self) -> None: + """Revert to previous validator (without failed statuses).""" + + # Define old schema validator + validator = { + "$jsonSchema": { + "bsonType": "object", + "required": ["_id", "title", "status", "client_id", "created_at", "updated_at"], + "properties": { + "_id": {"bsonType": "string"}, + "title": {"bsonType": "string"}, + "status": { + "enum": [ + "created", + "ingesting", + "ai_processing", + "pending_qc", + "approved_english", + "approved_source", + "rejected", + "qc_feedback", + "translating", + "tts_generating", + "rendering_video", + "pending_final_review", + "completed" + ] + }, + "client_id": {"bsonType": "string"}, + "created_at": {"bsonType": "date"}, + "updated_at": {"bsonType": "date"} + } + } + } + + # Update the collection validator + await self.db.command({ + "collMod": "jobs", + "validator": validator, + "validationLevel": "moderate", + "validationAction": "error" + }) + + print(f" Rolled back migration {self.version}: {self.description}") + print(f" WARNING: Jobs with tts_failed or render_failed status will fail validation!")