rewrite
This commit is contained in:
@@ -2,25 +2,44 @@
|
||||
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
import logging
|
||||
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def init_db(app):
|
||||
"""Initialize database - create tables"""
|
||||
"""Initialize database - create tables"""
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
print("[DB] All tables created successfully", flush=True)
|
||||
try:
|
||||
db.create_all()
|
||||
print("[DB] All tables created successfully", flush=True)
|
||||
except Exception as e:
|
||||
print(f"[DB] Error creating tables: {e}", flush=True)
|
||||
raise
|
||||
|
||||
from database.models import User
|
||||
admin = User.query.filter_by(username='admin').first()
|
||||
|
||||
if not admin:
|
||||
admin = User(username='admin', is_admin=True)
|
||||
admin.set_password('admin123')
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
print("[DB] Default admin user created (admin/admin123)", flush=True)
|
||||
else:
|
||||
print("[DB] Admin user already exists", flush=True)
|
||||
try:
|
||||
admin = User.query.filter_by(username='admin').first()
|
||||
|
||||
if not admin:
|
||||
print("[DB] Creating default admin user...", flush=True)
|
||||
admin = User(username='admin', is_admin=True)
|
||||
admin.set_password('admin123')
|
||||
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
print("[DB] Default admin user created (admin/admin123)", flush=True)
|
||||
print(f"[DB] Hash: {admin.password_hash}", flush=True)
|
||||
else:
|
||||
print("[DB] Admin user already exists", flush=True)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"[DB] Error creating admin: {e}", flush=True)
|
||||
db.session.rollback()
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user