Update app templates and routes
This commit is contained in:
@@ -4,7 +4,7 @@ from werkzeug.exceptions import RequestEntityTooLarge
|
||||
|
||||
from .extensions import db, login_manager
|
||||
from .models import AppSettings, User
|
||||
from .cli import init_db_command
|
||||
from .cli import ensure_db_command, init_db_command
|
||||
|
||||
|
||||
def create_app():
|
||||
@@ -85,6 +85,7 @@ def create_app():
|
||||
return db.session.get(User, int(user_id))
|
||||
|
||||
# CLI
|
||||
app.cli.add_command(ensure_db_command)
|
||||
app.cli.add_command(init_db_command)
|
||||
|
||||
# Blueprints
|
||||
|
||||
43
app/cli.py
43
app/cli.py
@@ -5,21 +5,11 @@ from .extensions import db
|
||||
from .models import AppSettings, User
|
||||
|
||||
|
||||
@click.command("init-db")
|
||||
@click.option(
|
||||
"--admin-email",
|
||||
required=False,
|
||||
default="beheer@alphen.cloud",
|
||||
show_default=True,
|
||||
help="Email for the initial admin",
|
||||
)
|
||||
@click.option("--admin-pass", required=True, help="Password for the initial admin")
|
||||
@with_appcontext
|
||||
def init_db_command(admin_email: str, admin_pass: str):
|
||||
"""Create tables and ensure an admin account exists."""
|
||||
def _ensure_schema_and_settings() -> None:
|
||||
"""Create tables + run lightweight SQLite migrations + ensure settings row exists."""
|
||||
db.create_all()
|
||||
|
||||
# Lightweight migration for older SQLite DBs: ensure User.email column exists.
|
||||
# Lightweight migration for older SQLite DBs: ensure columns exist.
|
||||
# This avoids requiring Alembic for this small project.
|
||||
try:
|
||||
cols = [r[1] for r in db.session.execute(db.text("PRAGMA table_info(user)")).fetchall()]
|
||||
@@ -50,6 +40,33 @@ def init_db_command(admin_email: str, admin_pass: str):
|
||||
db.session.add(AppSettings(id=1))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@click.command("ensure-db")
|
||||
@with_appcontext
|
||||
def ensure_db_command():
|
||||
"""Create tables / apply lightweight migrations.
|
||||
|
||||
This is useful for container startup where you want schema readiness,
|
||||
without requiring admin credentials.
|
||||
"""
|
||||
_ensure_schema_and_settings()
|
||||
click.echo("Database ready.")
|
||||
|
||||
|
||||
@click.command("init-db")
|
||||
@click.option(
|
||||
"--admin-email",
|
||||
required=False,
|
||||
default="beheer@alphen.cloud",
|
||||
show_default=True,
|
||||
help="Email for the initial admin",
|
||||
)
|
||||
@click.option("--admin-pass", required=True, help="Password for the initial admin")
|
||||
@with_appcontext
|
||||
def init_db_command(admin_email: str, admin_pass: str):
|
||||
"""Create tables and ensure an admin account exists."""
|
||||
_ensure_schema_and_settings()
|
||||
|
||||
admin_email = (admin_email or "").strip().lower()
|
||||
if not admin_email:
|
||||
raise click.UsageError("--admin-email is required")
|
||||
|
||||
Reference in New Issue
Block a user