REMOVE LDAP
This commit is contained in:
53
init_db.py
Normal file
53
init_db.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from flask import Flask
|
||||
from models import db, User
|
||||
from config import Config
|
||||
|
||||
|
||||
def seed_default_user() -> None:
|
||||
"""Create the default local account if it does not exist.
|
||||
|
||||
Requirement: seed egg/123/念萱 with default role Viewer.
|
||||
"""
|
||||
default_username = "egg"
|
||||
default_password = "123"
|
||||
default_name = "念萱"
|
||||
|
||||
existing = User.query.filter_by(username=default_username).first()
|
||||
if existing:
|
||||
print(f" - Default user '{default_username}' already exists. Skipping.")
|
||||
return
|
||||
|
||||
user = User(username=default_username, name=default_name, role="viewer")
|
||||
user.set_password(default_password)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
print(f" - Seeded default user {default_username}/{default_password}/{default_name} (role=viewer)")
|
||||
|
||||
|
||||
def init_database(app: Flask) -> None:
|
||||
"""Reset schema and seed initial data (DANGEROUS: drops all tables)."""
|
||||
with app.app_context():
|
||||
print("Initializing database: dropping and recreating tables...")
|
||||
db.drop_all()
|
||||
db.create_all()
|
||||
seed_default_user()
|
||||
print("Database initialized successfully.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
db.init_app(app)
|
||||
|
||||
print("=================================================")
|
||||
print(" Database Initialization Utility ")
|
||||
print("=================================================")
|
||||
print("WARNING: This will DROP and RECREATE all tables in the target database.")
|
||||
print("Ensure you have backups before proceeding.")
|
||||
|
||||
confirmation = input("Type 'yes' to continue (yes/no): ")
|
||||
if confirmation.strip().lower() == "yes":
|
||||
init_database(app)
|
||||
else:
|
||||
print("Aborted.")
|
||||
|
Reference in New Issue
Block a user