Files
karaoke-gen/scripts/get_job.py
T
Andrew Beveridge ☄️andClaude Opus 5.5 2db5407a5d feat: archive deleted jobs (doc + logs) instead of losing them (v0.295.0) (#1261)
## Why
When NOMAD-1875 was deleted (2026-11-09), its Firestore record and logs
were gone for good, so we couldn't use them to debug the delete-outputs
result afterwards. Andrew's call: job records are **system data**. Keep
them forever, and on account deletion redact the user's identity rather
than deleting the record.

## What
- `FirestoreService.archive_job()` copies `jobs/{id}` to
`jobs_archive/{id}`, adding `archived_at`, `archived_by` and
`archive_reason`. It also copies the `logs` subcollection to
`archived_logs` without `ttl_expiry` (the TTL policy is on the `logs`
collection group, so otherwise they'd expire after 30 days), and the
`audio_edit_sessions` subcollection.
- `JobManager.delete_job()` archives first and **aborts the delete if
the archive fails**.
- Callers pass who deleted the job and why: `admin_delete` /
`owner_delete` from `DELETE /api/jobs/{id}`, and
`cleanup_distribution_delete`.
- **Account deletion** now also redacts archived jobs: owner/customer
email becomes `deleted:<uuid>`, and IPs, notes and contact fields are
removed. These are single-field queries, so no new composite index is
needed. Archived jobs are never deleted.
- Bulk test cleanup (`delete_jobs_by_filter`) still hard-deletes.
- `scripts/get_job.py` falls back to the archive. Documented in
`docs/ARCHITECTURE.md`.

No infra change: Firestore creates the collection on first write, and no
TTL policy is set because archived jobs are kept forever.

## Testing
- Unit: archive happens before delete; the delete is aborted when the
archive fails; archived jobs are redacted on account deletion (including
made-for-you customer jobs).
- Emulator: `backend/tests/emulator/test_job_archive_emulator.py` checks
the doc, logs (no `ttl_expiry`) and audio-edit sessions are copied, and
that a missing job returns False.
- `test_account_deletion`, `test_job_manager`, `test_jobs_api`,
`test_services`, `test_api_integration`: 195 passed.
- Local CodeRabbit: no findings.

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_012RYLVUwKapcjF8czVQrPD5

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 12:06:51 -05:00

69 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Fetch and display job data from Firestore.
Usage:
python scripts/get_job.py <job_id>
python scripts/get_job.py ffb0b8fa
Requires:
- google-cloud-firestore
- GOOGLE_APPLICATION_CREDENTIALS or gcloud auth application-default login
"""
import sys
import json
from google.cloud import firestore
def get_job(job_id: str) -> dict | None:
"""Fetch job document from Firestore."""
db = firestore.Client(project='nomadkaraoke')
doc = db.collection('jobs').document(job_id).get()
if doc.exists:
return doc.to_dict()
# Deleted jobs are archived (doc + archived_logs) instead of lost.
archived = db.collection('jobs_archive').document(job_id).get()
if archived.exists:
print(f"(job {job_id} was deleted; showing jobs_archive copy)", file=sys.stderr)
return archived.to_dict()
return None
def main():
if len(sys.argv) < 2:
print("Usage: python get_job.py <job_id> [--field <field_name>]")
print("\nExamples:")
print(" python get_job.py ffb0b8fa # Full job data")
print(" python get_job.py ffb0b8fa --field status # Just status")
print(" python get_job.py ffb0b8fa --field timeline # Just timeline")
sys.exit(1)
job_id = sys.argv[1]
field_filter = None
if len(sys.argv) >= 4 and sys.argv[2] == "--field":
field_filter = sys.argv[3]
data = get_job(job_id)
if data is None:
print(f"Job {job_id} not found", file=sys.stderr)
sys.exit(1)
if field_filter:
if field_filter in data:
output = data[field_filter]
else:
print(f"Field '{field_filter}' not found. Available fields:", file=sys.stderr)
print(", ".join(sorted(data.keys())), file=sys.stderr)
sys.exit(1)
else:
output = data
print(json.dumps(output, indent=2, default=str))
if __name__ == "__main__":
main()