|
6 | 6 |
|
7 | 7 | import sys |
8 | 8 | import os |
| 9 | +import sqlite3 |
9 | 10 |
|
10 | 11 | # Add the app directory to Python path |
11 | 12 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
12 | 13 |
|
13 | | -from app import create_app |
14 | | -from app.models import db, User |
15 | | - |
16 | | -def add_theme_column(): |
17 | | - """Add theme_preference column to users table if it doesn't exist""" |
18 | | - app = create_app() |
19 | | - |
20 | | - with app.app_context(): |
21 | | - try: |
22 | | - # Check if the column already exists by trying to query it |
23 | | - result = db.session.execute("SELECT theme_preference FROM user LIMIT 1") |
| 14 | +def add_theme_column_direct(): |
| 15 | + """Add theme_preference column directly using SQLite""" |
| 16 | + try: |
| 17 | + # Try to find the database file |
| 18 | + possible_db_paths = [ |
| 19 | + 'instance/database.db', |
| 20 | + 'database.db', |
| 21 | + 'app.db', |
| 22 | + 'instance/app.db' |
| 23 | + ] |
| 24 | + |
| 25 | + db_path = None |
| 26 | + for path in possible_db_paths: |
| 27 | + if os.path.exists(path): |
| 28 | + db_path = path |
| 29 | + break |
| 30 | + |
| 31 | + if not db_path: |
| 32 | + print("Database file not found. Trying Flask app method...") |
| 33 | + return add_theme_column_flask() |
| 34 | + |
| 35 | + print(f"Found database at: {db_path}") |
| 36 | + |
| 37 | + # Connect to SQLite database directly |
| 38 | + conn = sqlite3.connect(db_path) |
| 39 | + cursor = conn.cursor() |
| 40 | + |
| 41 | + # Check if the column already exists |
| 42 | + cursor.execute("PRAGMA table_info(user)") |
| 43 | + columns = [column[1] for column in cursor.fetchall()] |
| 44 | + |
| 45 | + if 'theme_preference' in columns: |
24 | 46 | print("Column 'theme_preference' already exists in the database.") |
| 47 | + conn.close() |
25 | 48 | return True |
26 | | - except Exception as e: |
27 | | - print(f"Column 'theme_preference' does not exist. Adding it now...") |
| 49 | + |
| 50 | + print("Adding 'theme_preference' column...") |
| 51 | + |
| 52 | + # Add the column |
| 53 | + cursor.execute("ALTER TABLE user ADD COLUMN theme_preference VARCHAR(20) DEFAULT 'light'") |
| 54 | + |
| 55 | + # Update existing users |
| 56 | + cursor.execute("UPDATE user SET theme_preference = 'light' WHERE theme_preference IS NULL") |
| 57 | + |
| 58 | + # Commit changes |
| 59 | + conn.commit() |
| 60 | + |
| 61 | + # Verify the column was added |
| 62 | + cursor.execute("PRAGMA table_info(user)") |
| 63 | + columns = [column[1] for column in cursor.fetchall()] |
| 64 | + |
| 65 | + if 'theme_preference' in columns: |
| 66 | + print("Successfully added 'theme_preference' column to user table.") |
| 67 | + |
| 68 | + # Count updated users |
| 69 | + cursor.execute("SELECT COUNT(*) FROM user WHERE theme_preference = 'light'") |
| 70 | + count = cursor.fetchone()[0] |
| 71 | + print(f"Updated {count} existing users with default light theme.") |
28 | 72 |
|
| 73 | + conn.close() |
| 74 | + return True |
| 75 | + else: |
| 76 | + print("Failed to add column.") |
| 77 | + conn.close() |
| 78 | + return False |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + print(f"Direct SQLite method failed: {e}") |
| 82 | + return add_theme_column_flask() |
| 83 | + |
| 84 | +def add_theme_column_flask(): |
| 85 | + """Add theme_preference column using Flask app context""" |
| 86 | + try: |
| 87 | + from app import create_app |
| 88 | + from app.models import db |
| 89 | + |
| 90 | + app = create_app() |
| 91 | + |
| 92 | + with app.app_context(): |
| 93 | + # Check if the column already exists by trying to query it |
29 | 94 | try: |
30 | | - # Add the column with default value 'light' |
31 | | - db.session.execute("ALTER TABLE user ADD COLUMN theme_preference VARCHAR(20) DEFAULT 'light'") |
32 | | - db.session.commit() |
33 | | - print("Successfully added 'theme_preference' column to user table.") |
34 | | - |
35 | | - # Update all existing users to have light theme by default |
36 | | - users_updated = User.query.filter(User.theme_preference.is_(None)).update( |
37 | | - {'theme_preference': 'light'}, synchronize_session=False |
38 | | - ) |
39 | | - db.session.commit() |
40 | | - print(f"Updated {users_updated} existing users with default light theme.") |
41 | | - |
| 95 | + result = db.session.execute("SELECT theme_preference FROM user LIMIT 1") |
| 96 | + print("Column 'theme_preference' already exists in the database.") |
42 | 97 | return True |
43 | | - except Exception as e: |
44 | | - print(f"Error adding column: {e}") |
45 | | - db.session.rollback() |
46 | | - return False |
| 98 | + except Exception: |
| 99 | + print("Column 'theme_preference' does not exist. Adding it now...") |
| 100 | + |
| 101 | + try: |
| 102 | + # Add the column with default value 'light' |
| 103 | + db.session.execute("ALTER TABLE user ADD COLUMN theme_preference VARCHAR(20) DEFAULT 'light'") |
| 104 | + db.session.commit() |
| 105 | + print("Successfully added 'theme_preference' column to user table.") |
| 106 | + |
| 107 | + # Update all existing users to have light theme by default |
| 108 | + db.session.execute("UPDATE user SET theme_preference = 'light' WHERE theme_preference IS NULL") |
| 109 | + db.session.commit() |
| 110 | + |
| 111 | + # Count updated users |
| 112 | + result = db.session.execute("SELECT COUNT(*) FROM user WHERE theme_preference = 'light'") |
| 113 | + count = result.fetchone()[0] |
| 114 | + print(f"Updated {count} existing users with default light theme.") |
| 115 | + |
| 116 | + return True |
| 117 | + except Exception as e: |
| 118 | + print(f"Error adding column: {e}") |
| 119 | + db.session.rollback() |
| 120 | + return False |
| 121 | + |
| 122 | + except Exception as e: |
| 123 | + print(f"Flask method failed: {e}") |
| 124 | + return False |
47 | 125 |
|
48 | 126 | if __name__ == "__main__": |
| 127 | + print("=" * 60) |
| 128 | + print("DirectAdmin Email Forwarder - Theme Column Migration") |
| 129 | + print("=" * 60) |
49 | 130 | print("Adding theme_preference column to database...") |
50 | | - success = add_theme_column() |
| 131 | + print() |
| 132 | + |
| 133 | + success = add_theme_column_direct() |
| 134 | + |
51 | 135 | if success: |
52 | | - print("Database migration completed successfully!") |
| 136 | + print() |
| 137 | + print("=" * 60) |
| 138 | + print("✅ Database migration completed successfully!") |
| 139 | + print("You can now restart the application and use the theme toggle.") |
| 140 | + print("=" * 60) |
53 | 141 | sys.exit(0) |
54 | 142 | else: |
55 | | - print("Database migration failed!") |
| 143 | + print() |
| 144 | + print("=" * 60) |
| 145 | + print("❌ Database migration failed!") |
| 146 | + print("Please check the error messages above and try again.") |
| 147 | + print("=" * 60) |
56 | 148 | sys.exit(1) |
0 commit comments