chore: avoid unconditional db.create_all(); prefer Flask-Migrate when migrations/ exists
This commit is contained in:
12
app.py
12
app.py
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user