dgilland / sqlservice

The missing SQLAlchemy ORM interface.
https://sqlservice.readthedocs.io
MIT License
179 stars 9 forks source link

Migration from Flask-SQLAlchemy Document? #7

Closed dsully closed 7 years ago

dsully commented 7 years ago

Hi Derek - do you have a guide on migrating from Flask-SQLAlchemy to using SQLService?

I'm trying to wrap my head around what needs to be done, how the standard pattern of delaying engine creation can/should be done. For example:

import flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app(testing=False):
    app = flask.create_app(__name__, testing=testing)

    with app.app_context():
        db.init_app(app)

        log.info("SQLAlchemy URI: %s", app.config['SQLALCHEMY_DATABASE_URI'])

    from .api import api
    ...

Obviously the app context wouldn't be needed any longer. But the delayed connection is quite useful to handle production vs unit test configuration of the database.

dgilland commented 7 years ago

I actually have an example FlaskSQLService class that I showed in https://github.com/dgilland/sqlservice/issues/3 which includes the late-binding approach for engine creation via the Flask extension proxy class.

It's not tested by me in any way so there may be bugs or edge cases that need to be addressed but that example should help you get started.

One day I may have enough time to formalize that example into a flask-sqlservice extension proper but until then, feel free to use it directly.

dsully commented 7 years ago

Great, thanks!

dgilland commented 7 years ago

Closing this for now. If you run into any other issues, feel free to re-open the issue or create a new one.