|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Safe database migration - adds theme_preference column and then updates the model. |
| 4 | +Run this BEFORE starting the application. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import sqlite3 |
| 10 | + |
| 11 | +def add_theme_column_safe(): |
| 12 | + """Safely add theme_preference column to the database""" |
| 13 | + try: |
| 14 | + # Try to find the database file |
| 15 | + possible_db_paths = [ |
| 16 | + 'instance/database.db', |
| 17 | + 'database.db', |
| 18 | + 'app.db', |
| 19 | + 'instance/app.db' |
| 20 | + ] |
| 21 | + |
| 22 | + db_path = None |
| 23 | + for path in possible_db_paths: |
| 24 | + if os.path.exists(path): |
| 25 | + db_path = path |
| 26 | + break |
| 27 | + |
| 28 | + if not db_path: |
| 29 | + print("No existing database found.") |
| 30 | + print("The application will create the database with the column on first run.") |
| 31 | + # Add the column to the model file |
| 32 | + add_column_to_model() |
| 33 | + return True |
| 34 | + |
| 35 | + print(f"Found database at: {db_path}") |
| 36 | + |
| 37 | + # Connect to SQLite database |
| 38 | + conn = sqlite3.connect(db_path) |
| 39 | + cursor = conn.cursor() |
| 40 | + |
| 41 | + # Check if user table exists |
| 42 | + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user'") |
| 43 | + if not cursor.fetchone(): |
| 44 | + print("User table doesn't exist yet.") |
| 45 | + print("The application will create it with the column on first run.") |
| 46 | + conn.close() |
| 47 | + add_column_to_model() |
| 48 | + return True |
| 49 | + |
| 50 | + # Check if theme_preference column exists |
| 51 | + cursor.execute("PRAGMA table_info(user)") |
| 52 | + columns = [column[1] for column in cursor.fetchall()] |
| 53 | + |
| 54 | + if 'theme_preference' not in columns: |
| 55 | + print("Adding theme_preference column to database...") |
| 56 | + cursor.execute("ALTER TABLE user ADD COLUMN theme_preference VARCHAR(20) DEFAULT 'light'") |
| 57 | + cursor.execute("UPDATE user SET theme_preference = 'light' WHERE theme_preference IS NULL") |
| 58 | + conn.commit() |
| 59 | + print("✅ Added theme_preference column to database.") |
| 60 | + else: |
| 61 | + print("✅ theme_preference column already exists in database.") |
| 62 | + |
| 63 | + conn.close() |
| 64 | + |
| 65 | + # Now add the column to the model |
| 66 | + add_column_to_model() |
| 67 | + return True |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + print(f"Error: {e}") |
| 71 | + return False |
| 72 | + |
| 73 | +def add_column_to_model(): |
| 74 | + """Add the theme_preference column definition to the User model""" |
| 75 | + try: |
| 76 | + model_file = 'app/models.py' |
| 77 | + if not os.path.exists(model_file): |
| 78 | + print(f"Model file {model_file} not found!") |
| 79 | + return False |
| 80 | + |
| 81 | + # Read the current model file |
| 82 | + with open(model_file, 'r', encoding='utf-8') as f: |
| 83 | + content = f.read() |
| 84 | + |
| 85 | + # Check if theme_preference column is already defined |
| 86 | + if 'theme_preference = db.Column' in content: |
| 87 | + print("✅ theme_preference column already defined in model.") |
| 88 | + return True |
| 89 | + |
| 90 | + # Find the right place to insert the column definition |
| 91 | + if '# Unique encryption key per user for DA password' in content: |
| 92 | + # Insert before the encryption_key line |
| 93 | + new_content = content.replace( |
| 94 | + ' # Unique encryption key per user for DA password\n encryption_key = db.Column(db.String(255), nullable=True)', |
| 95 | + ' # User preferences\n theme_preference = db.Column(db.String(20), default=\'light\', nullable=True) # \'light\' or \'dark\'\n\n # Unique encryption key per user for DA password\n encryption_key = db.Column(db.String(255), nullable=True)' |
| 96 | + ) |
| 97 | + |
| 98 | + if new_content != content: |
| 99 | + # Write the updated content |
| 100 | + with open(model_file, 'w', encoding='utf-8') as f: |
| 101 | + f.write(new_content) |
| 102 | + print("✅ Added theme_preference column definition to model.") |
| 103 | + return True |
| 104 | + |
| 105 | + print("⚠️ Could not automatically add column to model - manual edit needed.") |
| 106 | + return False |
| 107 | + |
| 108 | + except Exception as e: |
| 109 | + print(f"Error updating model: {e}") |
| 110 | + return False |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + print("=" * 60) |
| 114 | + print("Safe Theme Migration for DirectAdmin Email Forwarder") |
| 115 | + print("=" * 60) |
| 116 | + print() |
| 117 | + |
| 118 | + success = add_theme_column_safe() |
| 119 | + |
| 120 | + if success: |
| 121 | + print() |
| 122 | + print("=" * 60) |
| 123 | + print("✅ Migration completed successfully!") |
| 124 | + print("You can now start the application safely.") |
| 125 | + print("=" * 60) |
| 126 | + sys.exit(0) |
| 127 | + else: |
| 128 | + print() |
| 129 | + print("=" * 60) |
| 130 | + print("❌ Migration failed!") |
| 131 | + print("Please check the errors above.") |
| 132 | + print("=" * 60) |
| 133 | + sys.exit(1) |
0 commit comments