From a5579220e005d50f00cce11b4ebb97429fe9c16f Mon Sep 17 00:00:00 2001 From: Jocelyn Delande Date: Sat, 18 Jun 2016 12:13:46 +0200 Subject: [PATCH] Fix migrations upgrade path for MySQL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- budget/run.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/budget/run.py b/budget/run.py index 807ad12..8f29f35 100644 --- a/budget/run.py +++ b/budget/run.py @@ -3,7 +3,7 @@ import warnings from flask import Flask, g, request, session from flask.ext.babel import Babel -from flask.ext.migrate import Migrate, upgrade +from flask.ext.migrate import Migrate, upgrade, stamp from raven.contrib.flask import Sentry from web import main, db, mail @@ -15,6 +15,15 @@ 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 """ @@ -49,6 +58,11 @@ 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()