From eeb9476226f9ee8dfcf3b06ca2abe1f83dca41b7 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sat, 19 Apr 2014 02:55:55 -0400 Subject: [PATCH] Upgrade to Flask-SqlAlchemy 2.0 (dev) and add custom SignallingSession Necessary for tests to work --- .../requirements/prod.txt | 4 +-- .../{{cookiecutter.app_name}}/extensions.py | 35 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/{{cookiecutter.app_name}}/requirements/prod.txt b/{{cookiecutter.app_name}}/requirements/prod.txt index c42b7ac..d9de632 100644 --- a/{{cookiecutter.app_name}}/requirements/prod.txt +++ b/{{cookiecutter.app_name}}/requirements/prod.txt @@ -8,8 +8,8 @@ Jinja2==2.7 itsdangerous==0.23 # Database -Flask-SQLAlchemy==1.0 -SQLAlchemy==0.8.3 +-e git://github.com/mitsuhiko/flask-sqlalchemy.git#egg=flask-sqlalchemy +SQLAlchemy==0.9.4 # Migrations Flask-Migrate>=1.0.0 diff --git a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/extensions.py b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/extensions.py index a9dc6b7..0e13839 100644 --- a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/extensions.py +++ b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/extensions.py @@ -9,8 +9,39 @@ bcrypt = Bcrypt() from flask.ext.login import LoginManager login_manager = LoginManager() -from flask.ext.sqlalchemy import SQLAlchemy -db = SQLAlchemy() +from flask.ext.sqlalchemy import SQLAlchemy, SignallingSession, SessionBase +class _SignallingSession(SignallingSession): + """A subclass of `SignallingSession` that allows for `binds` to be specified + in the `options` keyword arguments. + + """ + def __init__(self, db, autocommit=False, autoflush=True, **options): + self.app = db.get_app() + self._model_changes = {} + self.emit_modification_signals = \ + self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] + + bind = options.pop('bind', None) + if bind is None: + bind = db.engine + + binds = options.pop('binds', None) + if binds is None: + binds = db.get_binds(self.app) + + SessionBase.__init__(self, + autocommit=autocommit, + autoflush=autoflush, + bind=bind, + binds=binds, + **options) + + +class _SQLAlchemy(SQLAlchemy): + """A subclass of `SQLAlchemy` that uses `_SignallingSession`.""" + def create_session(self, options): + return _SignallingSession(self, **options) +db = _SQLAlchemy() from flask.ext.migrate import Migrate migrate = Migrate()