Spaces:
Running
Running
Download backend/simple_notification_migration.py from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/simple_notification_migration.py
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/backend/simple_notification_migration.py
-
curl -L -o simple_notification_migration.py https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/simple_notification_migration.py
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() | |