Files
asterisk-voicemail/src/vm_backfill_contacts.py
jp 05c5dbe45a MySQL backend + CDR + contacts email + matcher fix
- vm_store: MySQL (PyMySQL) default backend, SQLite fallback
- CDR: cdr_adaptive_odbc + res_odbc + MariaDB ODBC driver + Master.csv backfill
- contacts: full E.164 matching, WORK-email preference, junk-name guard
- backfill scripts: vm_migrate_sqlite_to_mysql.py, vm_backfill_contacts.py,
  vm_backfill_cdr.py
- live: 131 messages migrated, 7591 CDR rows backfilled
2026-08-13 13:09:03 +01:00

43 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Backfill contact_name + contact_email for existing voicemail rows.
Re-resolves each message's callerid through vm_contacts and writes the name
and email (if any) back to the row. Idempotent: re-running only fills rows
that are still empty. Also clears the contacts cache first so stale misses
don't block re-resolution.
Run as the asterisk user:
sudo -u asterisk /opt/vm-transcribe/venv/bin/python3 /opt/vm-transcribe/vm_backfill_contacts.py
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import vm_store
import vm_contacts
CACHE = vm_contacts.CONF_PATH # noqa
def main():
con = vm_store.connect()
total = con.execute("SELECT count(*) FROM messages").fetchone()[0]
rows = con.execute(
"SELECT id, mailbox, callerid FROM messages "
"WHERE contact_name IS NULL OR contact_name = ''").fetchall()
updated = 0
for r in rows:
cid = r["callerid"] or ""
name, email = vm_contacts.resolve(cid, log=lambda m: None)
if name:
con.execute(
"UPDATE messages SET contact_name=?, contact_email=? WHERE id=?",
(name, email, r["id"]))
updated += 1
con.commit()
print("rows total: %d, updated with a contact name: %d" % (total, updated))
if __name__ == "__main__":
main()