Fix 502 after update: safe startup, single DB init, healthcheck
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+9
-18
@@ -21,7 +21,7 @@ def load_user(user_id):
|
||||
return db.session.get(User, int(user_id))
|
||||
|
||||
|
||||
def create_app():
|
||||
def create_app(setup_database=True):
|
||||
app = Flask(__name__)
|
||||
|
||||
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "dev-secret-change-me")
|
||||
@@ -63,7 +63,10 @@ def create_app():
|
||||
def inject_banners():
|
||||
from app.banner_service import get_banners_by_position
|
||||
|
||||
return {"site_banners": get_banners_by_position()}
|
||||
try:
|
||||
return {"site_banners": get_banners_by_position()}
|
||||
except Exception:
|
||||
return {"site_banners": {}}
|
||||
|
||||
with app.app_context():
|
||||
from app.models import ( # noqa: F401
|
||||
@@ -79,23 +82,11 @@ def create_app():
|
||||
)
|
||||
|
||||
db.create_all()
|
||||
from app.bootstrap import (
|
||||
create_first_admin,
|
||||
ensure_default_group,
|
||||
ensure_group_limit_columns,
|
||||
ensure_photo_storage_column,
|
||||
ensure_schema,
|
||||
ensure_site_settings,
|
||||
)
|
||||
from app.folders import ensure_folder_schema
|
||||
|
||||
ensure_schema()
|
||||
ensure_default_group(app)
|
||||
ensure_group_limit_columns()
|
||||
ensure_folder_schema()
|
||||
ensure_site_settings(app)
|
||||
ensure_photo_storage_column()
|
||||
create_first_admin(app)
|
||||
if setup_database and os.getenv("SKIP_DB_INIT") != "1":
|
||||
from app.bootstrap import run_database_setup
|
||||
|
||||
run_database_setup(app)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
+42
-23
@@ -12,20 +12,22 @@ def ensure_schema():
|
||||
tables = inspector.get_table_names()
|
||||
|
||||
if "photos" in tables:
|
||||
columns = {col["name"] for col in inspector.get_columns("photos")}
|
||||
if "user_id" not in columns:
|
||||
db.session.execute(
|
||||
text("ALTER TABLE photos ADD COLUMN user_id INTEGER REFERENCES users(id)")
|
||||
db.session.execute(
|
||||
text(
|
||||
"ALTER TABLE photos ADD COLUMN IF NOT EXISTS "
|
||||
"user_id INTEGER REFERENCES users(id)"
|
||||
)
|
||||
db.session.commit()
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
if "users" in tables and "user_groups" in tables:
|
||||
columns = {col["name"] for col in inspector.get_columns("users")}
|
||||
if "group_id" not in columns:
|
||||
db.session.execute(
|
||||
text("ALTER TABLE users ADD COLUMN group_id INTEGER REFERENCES user_groups(id)")
|
||||
db.session.execute(
|
||||
text(
|
||||
"ALTER TABLE users ADD COLUMN IF NOT EXISTS "
|
||||
"group_id INTEGER REFERENCES user_groups(id)"
|
||||
)
|
||||
db.session.commit()
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def ensure_default_group(app):
|
||||
@@ -65,17 +67,19 @@ def ensure_group_limit_columns():
|
||||
if "user_groups" not in inspector.get_table_names():
|
||||
return
|
||||
|
||||
columns = {col["name"] for col in inspector.get_columns("user_groups")}
|
||||
if "max_folders" not in columns:
|
||||
db.session.execute(
|
||||
text("ALTER TABLE user_groups ADD COLUMN max_folders INTEGER NOT NULL DEFAULT 10")
|
||||
db.session.execute(
|
||||
text(
|
||||
"ALTER TABLE user_groups ADD COLUMN IF NOT EXISTS "
|
||||
"max_folders INTEGER NOT NULL DEFAULT 10"
|
||||
)
|
||||
db.session.commit()
|
||||
if "max_photos" not in columns:
|
||||
db.session.execute(
|
||||
text("ALTER TABLE user_groups ADD COLUMN max_photos INTEGER NOT NULL DEFAULT 500")
|
||||
)
|
||||
db.session.execute(
|
||||
text(
|
||||
"ALTER TABLE user_groups ADD COLUMN IF NOT EXISTS "
|
||||
"max_photos INTEGER NOT NULL DEFAULT 500"
|
||||
)
|
||||
db.session.commit()
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def ensure_site_settings(app):
|
||||
@@ -91,10 +95,25 @@ def ensure_photo_storage_column():
|
||||
inspector = inspect(db.engine)
|
||||
if "photos" not in inspector.get_table_names():
|
||||
return
|
||||
columns = {col["name"] for col in inspector.get_columns("photos")}
|
||||
if "storage_backend" not in columns:
|
||||
db.session.execute(text("ALTER TABLE photos ADD COLUMN storage_backend VARCHAR(20) DEFAULT 'local'"))
|
||||
db.session.commit()
|
||||
db.session.execute(
|
||||
text(
|
||||
"ALTER TABLE photos ADD COLUMN IF NOT EXISTS "
|
||||
"storage_backend VARCHAR(20) DEFAULT 'local'"
|
||||
)
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def run_database_setup(app):
|
||||
ensure_schema()
|
||||
ensure_default_group(app)
|
||||
ensure_group_limit_columns()
|
||||
from app.folders import ensure_folder_schema
|
||||
|
||||
ensure_folder_schema()
|
||||
ensure_site_settings(app)
|
||||
ensure_photo_storage_column()
|
||||
create_first_admin(app)
|
||||
|
||||
|
||||
def slugify(name):
|
||||
|
||||
+6
-5
@@ -319,9 +319,10 @@ def ensure_folder_schema():
|
||||
tables = inspector.get_table_names()
|
||||
|
||||
if "photos" in tables:
|
||||
columns = {col["name"] for col in inspector.get_columns("photos")}
|
||||
if "folder_id" not in columns:
|
||||
db.session.execute(
|
||||
text("ALTER TABLE photos ADD COLUMN folder_id INTEGER REFERENCES folders(id)")
|
||||
db.session.execute(
|
||||
text(
|
||||
"ALTER TABLE photos ADD COLUMN IF NOT EXISTS "
|
||||
"folder_id INTEGER REFERENCES folders(id)"
|
||||
)
|
||||
db.session.commit()
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
@@ -25,6 +25,15 @@ from app.upload_service import process_uploads
|
||||
bp = Blueprint("main", __name__)
|
||||
|
||||
|
||||
@bp.route("/health")
|
||||
def health():
|
||||
try:
|
||||
db.session.execute(db.text("SELECT 1"))
|
||||
return {"status": "ok"}, 200
|
||||
except Exception as exc:
|
||||
return {"status": "error", "detail": str(exc)}, 503
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def index():
|
||||
photos = Photo.query.order_by(Photo.created_at.desc()).limit(24).all()
|
||||
|
||||
Reference in New Issue
Block a user