Spaces:
Running

microfunnel-api / backend /simple_notification_migration.py
axeronis
feat: HOT lead notifications - Telegram + Dashboard alerts for ready-to-buy leads
727ec8d
Raw History Blame Contribute Delete
3.46 kB
"""
Notification tables migration using psycopg2 (sync)
"""
import psycopg2
from urllib.parse import urlparse
# πŸ”΄ EDIT THIS: Your production Supabase URL
db_url = "postgresql://postgres.qmzkpahvztlbovkpzsij:AbRpHNxBrdbw42IH@aws-0-eu-central-1.pooler.supabase.com:5432/postgres"
# Parse URL
parsed = urlparse(db_url)
print("πŸ”„ Connecting to database...")
print(f"Host: {parsed.hostname}")
try:
conn = psycopg2.connect(
host=parsed.hostname,
port=parsed.port or 5432,
user=parsed.username,
password=parsed.password,
database=parsed.path[1:]
)
cursor = conn.cursor()
print("βœ… Connected!")
# Migration SQL
migration_sql = """
-- notification_settings table
CREATE TABLE IF NOT EXISTS notification_settings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL UNIQUE REFERENCES tenants(id) ON DELETE CASCADE,
telegram_enabled BOOLEAN DEFAULT TRUE,
telegram_chat_id VARCHAR(255),
telegram_bot_token VARCHAR(255),
dashboard_enabled BOOLEAN DEFAULT TRUE,
email_enabled BOOLEAN DEFAULT FALSE,
email_address VARCHAR(255),
notify_hot_leads BOOLEAN DEFAULT TRUE,
notify_inactive_leads BOOLEAN DEFAULT FALSE,
notify_new_leads BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- notifications table
CREATE TABLE IF NOT EXISTS notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
lead_id UUID NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
type VARCHAR(50) NOT NULL,
title VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
is_read BOOLEAN DEFAULT FALSE,
read_at TIMESTAMP,
sent_telegram BOOLEAN DEFAULT FALSE,
sent_dashboard BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Indexes
CREATE INDEX IF NOT EXISTS idx_notification_settings_tenant_id ON notification_settings(tenant_id);
CREATE INDEX IF NOT EXISTS idx_notifications_tenant_id ON notifications(tenant_id);
CREATE INDEX IF NOT EXISTS idx_notifications_is_read ON notifications(is_read);
CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notifications_type ON notifications(type);
-- Default settings
INSERT INTO notification_settings (tenant_id, telegram_enabled, dashboard_enabled, notify_hot_leads)
SELECT id, TRUE, TRUE, TRUE
FROM tenants
WHERE id NOT IN (SELECT tenant_id FROM notification_settings);
"""
print("πŸ”„ Running migration...")
cursor.execute(migration_sql)
conn.commit()
print("\nβœ… βœ… βœ… MIGRATION SUCCESS! βœ… βœ… βœ…\n")
print("πŸ“Š Tables created:")
print(" - notification_settings")
print(" - notifications")
print("πŸ“Œ Indexes created (5)")
print("🎯 Default settings inserted for all tenants")
# Verify
cursor.execute("SELECT COUNT(*) FROM notification_settings")
count = cursor.fetchone()[0]
print(f"\nπŸ“ˆ Total notification_settings rows: {count}")
cursor.close()
conn.close()
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()