1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00
ihatemoney_ynh/budget/run.py
Jocelyn Delande a5579220e0 Fix migrations upgrade path for MySQL
For some reason, the migration path from unmanaged db (from alembic
point-of-view) to managed db, through the initial migration works well with
sqlite… But not with mysql where the db system tries to re-create the existing
tables.

This commit is a way to detect if we are migrating from pre-alembic era and
skip the first migration (which would do nothing anyway), marking it as already
executed.

It's quite hackish but that's the best I found so far to get it working with
both MySQL and SQLite.
2016-06-18 12:13:46 +02:00

91 lines
2.1 KiB
Python

import os
import warnings
from flask import Flask, g, request, session
from flask.ext.babel import Babel
from flask.ext.migrate import Migrate, upgrade, stamp
from raven.contrib.flask import Sentry
from web import main, db, mail
from api import api
from utils import PrefixedWSGI
from utils import minimal_round
app = Flask(__name__)
def pre_alembic_db():
""" Checks if we are migrating from a pre-alembic ihatemoney
"""
con = db.engine.connect()
tables_exist = db.engine.dialect.has_table(con, 'project')
alembic_setup = db.engine.dialect.has_table(con, 'alembic_version')
return tables_exist and not alembic_setup
def configure():
""" A way to (re)configure the app, specially reset the settings
"""
config_obj = os.environ.get('FLASK_SETTINGS_MODULE', 'merged_settings')
app.config.from_object(config_obj)
app.wsgi_app = PrefixedWSGI(app)
# Deprecations
if 'DEFAULT_MAIL_SENDER' in app.config:
# Since flask-mail 0.8
warnings.warn(
"DEFAULT_MAIL_SENDER is deprecated in favor of MAIL_DEFAULT_SENDER"
+" and will be removed in further version",
UserWarning
)
if not 'MAIL_DEFAULT_SENDER' in app.config:
app.config['MAIL_DEFAULT_SENDER'] = DEFAULT_MAIL_SENDER
configure()
app.register_blueprint(main)
app.register_blueprint(api)
# custom jinja2 filters
app.jinja_env.filters['minimal_round'] = minimal_round
# db
db.init_app(app)
db.app = app
# db migrations
migrate = Migrate(app, db)
if pre_alembic_db():
with app.app_context():
# fake the first migration
stamp(revision='b9a10d5d63ce')
# auto-execute migrations on runtime
with app.app_context():
upgrade()
# mail
mail.init_app(app)
# translations
babel = Babel(app)
# sentry
sentry = Sentry(app)
@babel.localeselector
def get_locale():
# get the lang from the session if defined, fallback on the browser "accept
# languages" header.
lang = session.get('lang', request.accept_languages.best_match(['fr', 'en']))
setattr(g, 'lang', lang)
return lang
def main():
app.run(host="0.0.0.0", debug=True)
if __name__ == '__main__':
main()