Files
digiserver/init_db.py
2025-06-27 16:50:35 +03:00

25 lines
930 B
Python

import os
from app import app
from extensions import db, bcrypt
from models import User, ServerLog # Import from models.py instead of app.py
def create_admin_user():
admin_username = os.getenv('ADMIN_USER', 'admin')
admin_password = os.getenv('ADMIN_PASSWORD', 'admin')
hashed_password = bcrypt.generate_password_hash(admin_password).decode('utf-8')
if not User.query.filter_by(username=admin_username).first():
admin_user = User(username=admin_username, password=hashed_password, role='admin')
db.session.add(admin_user)
db.session.commit()
print(f"Admin user '{admin_username}' created with password '{admin_password}'")
else:
print(f"Admin user '{admin_username}' already exists")
if __name__ == '__main__':
with app.app_context():
db.create_all()
create_admin_user()
print("Database initialized with all models including ServerLog")