#!/usr/bin/env python3 """ Create ContactSource Table This script creates a new table to track which data sources contributed to each contact. This allows the UI to show where contact information came from (e.g., Public Nouns, Raid Guild, etc.) """ import os import sys import logging from dotenv import load_dotenv # Add parent directory to path to import utils sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from utils.db_connector import DatabaseConnector from utils.logger import setup_logger # Load environment variables load_dotenv() # Setup logging logger = setup_logger("contact_source_creator") def create_contact_source_table(): """Create the ContactSource table if it doesn't exist""" db = DatabaseConnector() # Check if table already exists query = """ SELECT EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'ContactSource' ) """ result = db.execute_query(query) if result[0]["exists"]: logger.info("ContactSource table already exists") return # Create the table query = """ CREATE TABLE "ContactSource" ( id TEXT PRIMARY KEY, "contactId" TEXT NOT NULL, "dataSourceId" TEXT NOT NULL, "createdAt" TIMESTAMP NOT NULL, "updatedAt" TIMESTAMP NOT NULL, FOREIGN KEY ("contactId") REFERENCES "Contact"(id) ON DELETE CASCADE, FOREIGN KEY ("dataSourceId") REFERENCES "DataSource"(id) ON DELETE CASCADE, UNIQUE("contactId", "dataSourceId") ) """ db.execute_update(query) logger.info("Created ContactSource table") # Create index for faster lookups query = """ CREATE INDEX "ContactSource_contactId_idx" ON "ContactSource"("contactId"); CREATE INDEX "ContactSource_dataSourceId_idx" ON "ContactSource"("dataSourceId"); """ db.execute_update(query) logger.info("Created indexes on ContactSource table") def add_contact_source_methods(): """Add methods to DatabaseConnector to work with ContactSource table""" # This is just for documentation - we'll implement these in the actual script pass def main(): """Main function""" try: create_contact_source_table() logger.info("ContactSource table setup completed successfully") return 0 except Exception as e: logger.exception(f"Error setting up ContactSource table: {e}") return 1 if __name__ == "__main__": sys.exit(main())