chore: avoid unconditional db.create_all(); prefer Flask-Migrate when migrations/ exists

This commit is contained in:
Albert
2025-11-10 21:00:37 +01:00
parent 2545ab84fd
commit 50cbd08950

12
app.py
View File

@@ -5,6 +5,7 @@ import re
from utils import generate_vcard
import logging
from logging.handlers import RotatingFileHandler
from flask_migrate import Migrate
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_DIR, 'anmeldung.db')
@@ -14,6 +15,8 @@ app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Flask-Migrate (Alembic) integration
migrate = Migrate(app, db)
# --- logging setup -------------------------------------------------
LOG_DIR = os.path.join(BASE_DIR, 'logs')
@@ -135,7 +138,12 @@ def danke():
if __name__ == '__main__':
# Ensure DB exists
if not os.path.exists(DB_PATH):
# In development only: if there is no migrations directory, create tables automatically.
# In all other cases prefer using Flask-Migrate (flask db upgrade).
migrations_dir = os.path.join(BASE_DIR, 'migrations')
if not os.path.exists(migrations_dir):
app.logger.info('No migrations directory found; creating database tables with db.create_all()')
db.create_all()
else:
app.logger.info('Migrations directory present; please use "flask db upgrade" to update the database schema')
app.run(debug=True)