|
|
|
|
|
""" |
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
print("\n[1/4] Creating new tables if they don't exist...") |
|
|
db.create_all() |
|
|
print(" β Tables created/verified") |
|
|
|
|
|
|
|
|
print("\n[2/4] Checking User table for new columns...") |
|
|
|
|
|
if table_exists(engine, 'users'): |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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)") |
|
|
|
|
|
|
|
|
print("\n[3/4] Migrating existing user data...") |
|
|
|
|
|
|
|
|
|
|
|
users_to_migrate = User.query.filter( |
|
|
User.registration_bonus == 0, |
|
|
User.balance > 0 |
|
|
).all() |
|
|
|
|
|
migrated_count = 0 |
|
|
for user in users_to_migrate: |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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("\nCurrent App Settings:") |
|
|
print("-" * 40) |
|
|
settings = AppSettings.query.all() |
|
|
for setting in settings: |
|
|
print(f" {setting.key}: {setting.value or '(not set)'}") |
|
|
|
|
|
|
|
|
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() |
|
|
|