Download migrate_workspaces_sql.py from Roccuss/RAG-ITS: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/spaces/Roccuss/RAG-ITS/resolve/main/migrate_workspaces_sql.py
- Command line
-
hf download hf://spaces/Roccuss/RAG-ITS/migrate_workspaces_sql.py
-
curl -L -o migrate_workspaces_sql.py https://huggingface.co/spaces/Roccuss/RAG-ITS/resolve/main/migrate_workspaces_sql.py
3.51 kB
| """ | |
| SQL Migration: Add workspace support to existing tables | |
| """ | |
| import psycopg2 | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Parse DATABASE_URL | |
| db_url = os.getenv('DATABASE_URL', '') | |
| # Format: postgresql://user:password@host:port/database | |
| print("\nπ Starting database migration for workspaces...\n") | |
| try: | |
| # Connect to PostgreSQL | |
| conn = psycopg2.connect(db_url) | |
| cur = conn.cursor() | |
| print("β Connected to database") | |
| # 1. Create workspaces table | |
| print("\n1οΈβ£ Creating workspaces table...") | |
| cur.execute(""" | |
| CREATE TABLE IF NOT EXISTS workspaces ( | |
| id SERIAL PRIMARY KEY, | |
| title VARCHAR(60) NOT NULL, | |
| emoji VARCHAR(10) DEFAULT 'π§', | |
| color VARCHAR(7) DEFAULT '#1976d2', | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| is_active BOOLEAN DEFAULT TRUE | |
| ) | |
| """) | |
| print("β Workspaces table created") | |
| # 2. Insert default workspace if not exists | |
| print("\n2οΈβ£ Creating default workspace...") | |
| cur.execute(""" | |
| INSERT INTO workspaces (id, title, emoji, color, is_active) | |
| VALUES (1, 'ITS Maker Academy', 'π', '#1976d2', TRUE) | |
| ON CONFLICT (id) DO NOTHING | |
| """) | |
| print("β Default workspace created") | |
| # 3. Add workspace_id to historical_emails | |
| print("\n3οΈβ£ Adding workspace_id to historical_emails...") | |
| cur.execute(""" | |
| ALTER TABLE historical_emails | |
| ADD COLUMN IF NOT EXISTS workspace_id INTEGER DEFAULT 1 NOT NULL | |
| REFERENCES workspaces(id) ON DELETE CASCADE | |
| """) | |
| print("β Added workspace_id to historical_emails") | |
| # 4. Add workspace_id to enrollment_documents | |
| print("\n4οΈβ£ Adding workspace_id to enrollment_documents...") | |
| cur.execute(""" | |
| ALTER TABLE enrollment_documents | |
| ADD COLUMN IF NOT EXISTS workspace_id INTEGER DEFAULT 1 NOT NULL | |
| REFERENCES workspaces(id) ON DELETE CASCADE | |
| """) | |
| print("β Added workspace_id to enrollment_documents") | |
| # 5. Add workspace_id to corrections | |
| print("\n5οΈβ£ Adding workspace_id to corrections...") | |
| cur.execute(""" | |
| ALTER TABLE corrections | |
| ADD COLUMN IF NOT EXISTS workspace_id INTEGER DEFAULT 1 NOT NULL | |
| REFERENCES workspaces(id) ON DELETE CASCADE | |
| """) | |
| print("β Added workspace_id to corrections") | |
| # Commit all changes | |
| conn.commit() | |
| # Get statistics | |
| cur.execute("SELECT COUNT(*) FROM historical_emails WHERE workspace_id = 1") | |
| emails_count = cur.fetchone()[0] | |
| cur.execute("SELECT COUNT(*) FROM enrollment_documents WHERE workspace_id = 1") | |
| docs_count = cur.fetchone()[0] | |
| cur.execute("SELECT COUNT(*) FROM corrections WHERE workspace_id = 1") | |
| corrections_count = cur.fetchone()[0] | |
| print(f"\nπ Default workspace (ITS Maker Academy π):") | |
| print(f" - Historical Emails: {emails_count}") | |
| print(f" - Enrollment Documents: {docs_count}") | |
| print(f" - Corrections: {corrections_count}") | |
| print("\nβ Migration completed successfully!") | |
| print(" You can now start Flask and access the workspace dashboard.\n") | |
| cur.close() | |
| conn.close() | |
| except Exception as e: | |
| print(f"\nβ Migration failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |