You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Ensure DB initialization only once (important with multi-worker if --preload not used)\n if not app.config.get('_DB_INITIALIZED', False):\n with app.app_context():\n print(f\"Initializing database at URI: {app.config['SQLALCHEMY_DATABASE_URI']}\")\n db.create_all()\n\n # Migrate existing users from single domain to multi-domain\n print(\"Checking for users to migrate to multi-domain...\")\n users_to_migrate = User.query.filter(\n User.da_domain.isnot(None),\n ~User.domains.any()\n ).all()\n \n for user in users_to_migrate:\n print(f\"Migrating user {user.username} with domain {user.da_domain}\")\n success, message = user.add_domain(user.da_domain)\n if success:\n print(f\" ✓ Migrated {user.username}: {message}\")\n else:\n print(f\" ✗ Failed to migrate {user.username}: {message}\")\n \n if users_to_migrate:\n try:\n db.session.commit()\n print(f\"Successfully migrated {len(users_to_migrate)} users to multi-domain.\")\n except Exception as e:\n print(f\"Error during migration: {e}\")\n db.session.rollback()\n\n # Create default admin user only if no administrators exist\n admin_count = User.query.filter_by(is_admin=True).count()\n if admin_count == 0:\n # No administrators exist, create default admin user\n admin_user = User(username='admin', is_admin=True)\n admin_user.set_password('changeme') # Default password\n db.session.add(admin_user)\n try:\n db.session.commit()\n print(\"=\" * 50)\n print(\"Default admin user created!\")\n print(\"Username: admin\")\n print(\"Password: changeme\")\n print(\"PLEASE CHANGE THIS PASSWORD IMMEDIATELY!\")\n print(\"=\" * 50)\n except Exception as e:\n print(f\"Error creating admin user: {e}\")\n db.session.rollback()\n else:\n print(f\"Found {admin_count} administrator(s) - skipping default admin creation\")\n\n app.config['_DB_INITIALIZED'] = True
412
+
# Ensure DB initialization only once (important with multi-worker if --preload not used)
413
+
ifnotapp.config.get('_DB_INITIALIZED', False):
414
+
withapp.app_context():
415
+
print(f"Initializing database at URI: {app.config['SQLALCHEMY_DATABASE_URI']}")
416
+
417
+
# Import models to ensure they're registered
418
+
fromapp.modelsimportUser, UserDomain
419
+
420
+
# Check if we need to handle schema migration
421
+
needs_migration=False
422
+
try:
423
+
# Test if UserDomain table exists by trying a simple query
424
+
db.session.execute(db.text("SELECT COUNT(*) FROM user_domain")).scalar()
425
+
print("UserDomain table exists")
426
+
exceptExceptionase:
427
+
print(f"UserDomain table doesn't exist: {e}")
428
+
needs_migration=True
429
+
430
+
# Always call create_all() to ensure all tables exist
431
+
db.create_all()
432
+
print("Database tables created/updated")
433
+
434
+
# If we needed migration, migrate existing users
435
+
ifneeds_migration:
436
+
print("Performing automatic migration to multi-domain...")
0 commit comments