File size: 6,941 Bytes
08ec4c0 994d93e 08ec4c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
#!/usr/bin/env python3
"""
Database initialization script for new features:
- AppSettings table for dynamic app configuration
- User registration bonus tracking fields
Run this script after updating the models to apply database changes.
Usage: python scripts/init_settings.py
"""
import sys
import os
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app import create_app, db
from app.models import AppSettings, User
from sqlalchemy import inspect, text
def table_exists(engine, table_name):
"""Check if a table exists in the database"""
inspector = inspect(engine)
return table_name in inspector.get_table_names()
def column_exists(engine, table_name, column_name):
"""Check if a column exists in a table"""
inspector = inspect(engine)
columns = [col['name'] for col in inspector.get_columns(table_name)]
return column_name in columns
def init_database():
"""Initialize database with new tables and columns"""
app = create_app()
with app.app_context():
print("=" * 60)
print("Database Initialization Script")
print("=" * 60)
engine = db.engine
# Create all tables (including new AppSettings table)
print("\n[1/4] Creating new tables if they don't exist...")
db.create_all()
print(" β Tables created/verified")
# Check and add new columns to users table
print("\n[2/4] Checking User table for new columns...")
if table_exists(engine, 'users'):
# Check for registration_bonus column
if not column_exists(engine, 'users', 'registration_bonus'):
print(" Adding 'registration_bonus' column...")
with engine.connect() as conn:
conn.execute(text(
"ALTER TABLE users ADD COLUMN registration_bonus FLOAT DEFAULT 0.0"
))
conn.commit()
print(" β 'registration_bonus' column added")
else:
print(" β 'registration_bonus' column already exists")
# Check for registration_bonus_unlocked column
if not column_exists(engine, 'users', 'registration_bonus_unlocked'):
print(" Adding 'registration_bonus_unlocked' column...")
with engine.connect() as conn:
conn.execute(text(
"ALTER TABLE users ADD COLUMN registration_bonus_unlocked BOOLEAN DEFAULT 0"
))
conn.commit()
print(" β 'registration_bonus_unlocked' column added")
else:
print(" β 'registration_bonus_unlocked' column already exists")
else:
print(" ! Users table doesn't exist yet (will be created)")
# Migrate existing users' balances to the new system
print("\n[3/4] Migrating existing user data...")
# For existing users who already have a balance from registration bonus,
# we consider them as having unlocked bonus (since they're existing users)
users_to_migrate = User.query.filter(
User.registration_bonus == 0,
User.balance > 0
).all()
migrated_count = 0
for user in users_to_migrate:
# Mark existing users as having unlocked bonus
# (they've been using the system before this feature)
user.registration_bonus_unlocked = True
migrated_count += 1
if migrated_count > 0:
db.session.commit()
print(f" β Migrated {migrated_count} existing users")
else:
print(" β No users need migration")
# Initialize default app settings
print("\n[4/4] Initializing default app settings...")
default_settings = [
('app_name', 'Apex Ores', 'Nom de l\'application'),
('app_logo', None, 'URL du logo de l\'application'),
]
for key, default_value, description in default_settings:
existing = AppSettings.query.filter_by(key=key).first()
if not existing:
setting = AppSettings(
key=key,
value=default_value,
description=description
)
db.session.add(setting)
print(f" β Created setting: {key} = {default_value}")
else:
print(f" β Setting '{key}' already exists: {existing.value}")
db.session.commit()
print("\n" + "=" * 60)
print("Database initialization completed successfully!")
print("=" * 60)
# Print current settings
print("\nCurrent App Settings:")
print("-" * 40)
settings = AppSettings.query.all()
for setting in settings:
print(f" {setting.key}: {setting.value or '(not set)'}")
# Print user statistics
print("\nUser Statistics:")
print("-" * 40)
total_users = User.query.count()
users_with_locked_bonus = User.query.filter(
User.registration_bonus > 0,
User.registration_bonus_unlocked == False
).count()
users_with_unlocked_bonus = User.query.filter(
User.registration_bonus_unlocked == True
).count()
print(f" Total users: {total_users}")
print(f" Users with locked bonus: {users_with_locked_bonus}")
print(f" Users with unlocked bonus: {users_with_unlocked_bonus}")
def update_app_name(new_name):
"""Quick function to update app name"""
app = create_app()
with app.app_context():
AppSettings.set_setting('app_name', new_name, 'Nom de l\'application')
print(f"App name updated to: {new_name}")
def update_app_logo(logo_url):
"""Quick function to update app logo"""
app = create_app()
with app.app_context():
AppSettings.set_setting('app_logo', logo_url, 'URL du logo de l\'application')
print(f"App logo updated to: {logo_url}")
if __name__ == '__main__':
if len(sys.argv) > 1:
command = sys.argv[1]
if command == 'set-name' and len(sys.argv) > 2:
update_app_name(sys.argv[2])
elif command == 'set-logo' and len(sys.argv) > 2:
update_app_logo(sys.argv[2])
elif command == 'help':
print("Usage:")
print(" python init_settings.py - Initialize database")
print(" python init_settings.py set-name <name> - Set app name")
print(" python init_settings.py set-logo <url> - Set app logo URL")
print(" python init_settings.py help - Show this help")
else:
print("Unknown command. Use 'help' for usage information.")
else:
init_database()
|