mardi 14 juillet 2015

Flask-Migration (alembic) upgrade in-memory sqlite database in code

I have an application, that connects to database with sqlalchemy. Migrations fill database with some pre-defined settings. Also application have pytest, that i want to run with base settings. Main parts of code are:

# config.py
class DefaultConfig(object):
    DEBUG = True
    TESTING = True

    CSRF_ENABLED = True
    SECRET_KEY = 'this-really-needs-to-change'

    SQLALCHEMY_DATABASE_URI = 'sqlite://'

# __init__.py
db = SQLAlchemy()
socketio = SocketIO()
migrate = Migrate()

def create_app(config=None):
    app = Flask(__name__)

    if config is not None:
        config_path = os.path.abspath(config)
        app.config.from_pyfile(config_path)

    elif os.path.isfile(os.path.abspath(CONFIGFILE)):
        app.config.from_pyfile(os.path.abspath(CONFIGFILE))

    else:
        app.config.from_object(DefaultConfig)

    db.init_app(app)
    socketio.init_app(app)
    migrate.init_app(app, db)

    return app


# fixtures.py
from flask.ext.migrate import upgrade

from . import create_app, db
from .models import User


class AppFixture(object):
    def __init__(self):
        self.app = create_app(init_blueprints=False)
        self.db = db
        with self.app.app_context():
            upgrade() # <---- NOT WORKS
            self.users = User.query.all() # OperationalError: (sqlite3.OperationalError) no such table: user [SQL: u'SELECT

P.S. I really need to store test database in memory, with sqlite:///tmp/tempdb.db its works 20x slower!

P.P.S. Application factory is needed too, for some application running details. But without factory, in-memory database was fine.

Aucun commentaire:

Enregistrer un commentaire