Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow engine options to be set through the constructor like session options. #487

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ def get_engine(self):
self._sa.apply_driver_hacks(self._app, info, options)
if echo:
options['echo'] = echo
options.update(self._sa.engine_options)
self._engine = rv = sqlalchemy.create_engine(info, **options)
if _record_queries(self._app):
_EngineDebuggingSignalEvents(self._engine,
Expand Down Expand Up @@ -710,6 +711,10 @@ class User(db.Model):
to be passed to the session constructor. See :class:`~sqlalchemy.orm.session.Session`
for the standard options.

The ``engine_options`` parameter, if provided, is a dict of parameters
to be passed to the engine constructor. See :class:`~sqlalchemy.create_engine`
for the standard options.

.. versionadded:: 0.10
The `session_options` parameter was added.

Expand Down Expand Up @@ -738,14 +743,16 @@ class to be used in place of :class:`Model`.
Query = None

def __init__(self, app=None, use_native_unicode=True, session_options=None,
metadata=None, query_class=BaseQuery, model_class=Model):
metadata=None, query_class=BaseQuery, model_class=Model,
engine_options=None):

self.use_native_unicode = use_native_unicode
self.Query = query_class
self.session = self.create_scoped_session(session_options)
self.Model = self.make_declarative_base(model_class, metadata)
self._engine_lock = Lock()
self.app = app
self.engine_options = engine_options or {}
_include_sqlalchemy(self, query_class)

if app is not None:
Expand Down
10 changes: 10 additions & 0 deletions test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,16 @@ class FooBoundModel(AbstractFooBoundModel):
self.assertEqual(len(metadata.tables), 1)
self.assertTrue('foo_bound_model' in metadata.tables)

def test_extended_engine_options(self):
app = flask.Flask(__name__)
app.config['SQLALCHEMY_BINDS'] = {
'foo': 'sqlite://'
}
db = fsa.SQLAlchemy(app, engine_options=dict(echo=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not the best example for a unit test considering this one can be set through the flask config (SQLALCHEMY_ECHO)

db.create_all()

self.assertTrue(db.engine.echo)


class DefaultQueryClassTestCase(unittest.TestCase):
def test_default_query_class(self):
Expand Down