Coverage for backend / app / job_email_scraping / migrate_email_ids.py: 0%
27 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-17 21:34 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-17 21:34 +0000
1"""Migration script to update external_email_id from IMAP sequence numbers to UIDs.
3Fetches all emails from the past 1000 days via IMAP, matches them to database
4records by subject + date_received, and updates the database.
5Multiple IMAP matches for a single DB record are highlighted as ambiguous.
6"""
8import sys
10from app.config import settings
11from app.database import get_db
12from app.emails.email_service import EmailService
13from app.job_email_scraping.models import JobEmail
16def run_migration(dry_run: bool = True):
17 """Run the migration.
18 :param dry_run: If True, only print what would change without updating the DB."""
20 db = next(get_db())
21 email_service = EmailService(settings.scraper_email_username, settings.scraper_email_password)
23 vps_emails = email_service.get_emails(timedelta_days=10000)
24 emails_db = db.query(JobEmail).order_by(JobEmail.external_email_id).all()
25 emails_db = [email for email in emails_db if not email.external_email_id.startswith("jam")]
26 emails_db = sorted(emails_db, key=lambda email: int(email.external_email_id))
28 for email_db in emails_db[::-1]:
29 matching = [
30 vps_email
31 for vps_email in vps_emails
32 if vps_email["date"] == email_db.date_received and vps_email["subject"] == email_db.subject
33 ]
34 if len(matching) != 1:
35 raise AssertionError(f"Should be 1, Is {len(matching)}")
36 print(
37 f"Current external_email_id is {email_db.external_email_id}, new UID is {matching[0]["id"]}. Difference is {int(email_db.external_email_id) - int(matching[0]["id"])}"
38 )
39 if dry_run:
40 pass
41 else:
42 email_db.external_email_id = matching[0]["id"]
43 db.commit()
46if __name__ == "__main__":
47 dry = "--apply" not in sys.argv
48 if dry:
49 print("=== DRY RUN MODE (pass --apply to update the database) ===\n")
50 else:
51 print("=== APPLYING CHANGES ===\n")
53 run_migration(dry_run=dry)