[30388f42] Infrastructure Hardening: Repaired CE/Connector DB schema, fixed frontend styling build, implemented robust echo shield in worker v2.1.1, and integrated Lead Engine into gateway.
This commit is contained in:
44
company-explorer/backend/scripts/add_unsubscribe_tokens.py
Normal file
44
company-explorer/backend/scripts/add_unsubscribe_tokens.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import uuid
|
||||
import os
|
||||
import sys
|
||||
|
||||
# This is the crucial part to fix the import error.
|
||||
# We add the 'company-explorer' directory to the path, so imports can be absolute
|
||||
# from the 'backend' module.
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
||||
|
||||
from backend.database import Contact, SessionLocal
|
||||
|
||||
def migrate_existing_contacts():
|
||||
"""
|
||||
Generates and adds an unsubscribe_token for all existing contacts
|
||||
that do not have one yet.
|
||||
"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
contacts_to_update = db.query(Contact).filter(Contact.unsubscribe_token == None).all()
|
||||
|
||||
if not contacts_to_update:
|
||||
print("All contacts already have an unsubscribe token. No migration needed.")
|
||||
return
|
||||
|
||||
print(f"Found {len(contacts_to_update)} contacts without an unsubscribe token. Generating tokens...")
|
||||
|
||||
for contact in contacts_to_update:
|
||||
token = str(uuid.uuid4())
|
||||
contact.unsubscribe_token = token
|
||||
print(f" - Generated token for contact ID {contact.id} ({contact.email})")
|
||||
|
||||
db.commit()
|
||||
print("\nSuccessfully updated all contacts with new unsubscribe tokens.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starting migration: Populating unsubscribe_token for existing contacts.")
|
||||
migrate_existing_contacts()
|
||||
print("Migration finished.")
|
||||
Reference in New Issue
Block a user