94 lines
3 KiB
Python
94 lines
3 KiB
Python
|
|
import mysql.connector
|
||
|
|
import yaml
|
||
|
|
import os
|
||
|
|
|
||
|
|
def initialize_database():
|
||
|
|
# === Load config file ===
|
||
|
|
with open("config.yml", "r") as file:
|
||
|
|
config = yaml.safe_load(file)
|
||
|
|
|
||
|
|
# === DB Connection ===
|
||
|
|
conn = mysql.connector.connect(
|
||
|
|
host=os.getenv("DB_HOST", "localhost"),
|
||
|
|
port=os.getenv("DB_PORT", 3306),
|
||
|
|
user=os.getenv("DB_USER", "emailuser"),
|
||
|
|
password=os.getenv("DB_PASSWORD", "miguel33020"),
|
||
|
|
database=os.getenv("DB_NAME", "emailassistant")
|
||
|
|
)
|
||
|
|
cursor = conn.cursor()
|
||
|
|
|
||
|
|
# === Table: metadata (previously main_account) ===
|
||
|
|
cursor.execute("""
|
||
|
|
CREATE TABLE IF NOT EXISTS metadata (
|
||
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
|
|
user VARCHAR(255),
|
||
|
|
email VARCHAR(255) UNIQUE NOT NULL,
|
||
|
|
token TEXT
|
||
|
|
);
|
||
|
|
""")
|
||
|
|
print("✅ Table ready: metadata")
|
||
|
|
|
||
|
|
# === Table: emails ===
|
||
|
|
cursor.execute("""
|
||
|
|
CREATE TABLE IF NOT EXISTS emails (
|
||
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
|
|
user VARCHAR(255),
|
||
|
|
account VARCHAR(255),
|
||
|
|
message_id VARCHAR(255) UNIQUE,
|
||
|
|
thread_id VARCHAR(255),
|
||
|
|
account_id VARCHAR(255),
|
||
|
|
sender VARCHAR(255),
|
||
|
|
cc TEXT,
|
||
|
|
subject TEXT,
|
||
|
|
body LONGTEXT,
|
||
|
|
links LONGTEXT,
|
||
|
|
unsubscribe_data TEXT,
|
||
|
|
received_at DATETIME,
|
||
|
|
folder VARCHAR(50),
|
||
|
|
attachments LONGTEXT,
|
||
|
|
is_read BOOLEAN DEFAULT FALSE,
|
||
|
|
labels LONGTEXT,
|
||
|
|
|
||
|
|
-- 🔍 AI-Generated Fields
|
||
|
|
ai_category VARCHAR(100), -- Top-level (e.g. 'promo')
|
||
|
|
ai_confidence FLOAT, -- Confidence score
|
||
|
|
ai_summary TEXT, -- Summary of subject/body
|
||
|
|
ai_keywords TEXT, -- Comma-separated extracted keywords
|
||
|
|
ai_label_source VARCHAR(100), -- 'subject', 'body', 'combined', 'llm'
|
||
|
|
summary_source VARCHAR(100), -- Similar to above
|
||
|
|
ai_model_version VARCHAR(100), -- Versioning helps long-term debugging
|
||
|
|
is_ai_reviewed BOOLEAN DEFAULT FALSE, -- Was this fully processed by AI?
|
||
|
|
processing_notes TEXT, -- Optional notes about fallback, etc.
|
||
|
|
|
||
|
|
-- 🔄 Sync and Processing Status
|
||
|
|
processing_status VARCHAR(50),
|
||
|
|
sync_status VARCHAR(50),
|
||
|
|
attachment_path TEXT,
|
||
|
|
downloaded BOOLEAN DEFAULT FALSE
|
||
|
|
);
|
||
|
|
""")
|
||
|
|
print("✅ Table ready: emails")
|
||
|
|
|
||
|
|
|
||
|
|
# === Table: logs ===
|
||
|
|
cursor.execute("""
|
||
|
|
CREATE TABLE IF NOT EXISTS logs (
|
||
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
|
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||
|
|
level VARCHAR(20),
|
||
|
|
source VARCHAR(255),
|
||
|
|
message TEXT
|
||
|
|
);
|
||
|
|
""")
|
||
|
|
print("✅ Table ready: logs")
|
||
|
|
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
# if __name__ == "__main__":
|
||
|
|
# initialize_database()
|
||
|
|
|
||
|
|
|
||
|
|
#if __name__ == "__main__":
|
||
|
|
# initialize_database()
|