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

Support additional SQLAlchemy parameters. #540

Closed
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
117 changes: 67 additions & 50 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,73 @@ A list of configuration keys currently understood by the extension:

.. tabularcolumns:: |p{6.5cm}|p{8.5cm}|

================================== =========================================
``SQLALCHEMY_DATABASE_URI`` The database URI that should be used for
the connection. Examples:

- ``sqlite:////tmp/test.db``
- ``mysql://username:password@server/db``
``SQLALCHEMY_BINDS`` A dictionary that maps bind keys to
SQLAlchemy connection URIs. For more
information about binds see :ref:`binds`.
``SQLALCHEMY_ECHO`` If set to `True` SQLAlchemy will log all
the statements issued to stderr which can
be useful for debugging.
``SQLALCHEMY_RECORD_QUERIES`` Can be used to explicitly disable or
enable query recording. Query recording
automatically happens in debug or testing
mode. See :func:`get_debug_queries` for
more information.
``SQLALCHEMY_NATIVE_UNICODE`` Can be used to explicitly disable native
unicode support. This is required for
some database adapters (like PostgreSQL
on some Ubuntu versions) when used with
improper database defaults that specify
encoding-less databases.
``SQLALCHEMY_POOL_SIZE`` The size of the database pool. Defaults
to the engine's default (usually 5)
``SQLALCHEMY_POOL_TIMEOUT`` Specifies the connection timeout for the
pool. Defaults to 10.
``SQLALCHEMY_POOL_RECYCLE`` Number of seconds after which a
connection is automatically recycled.
This is required for MySQL, which removes
connections after 8 hours idle by
default. Note that Flask-SQLAlchemy
automatically sets this to 2 hours if
MySQL is used. Some backends may use a
different default timeout value. For more
information about timeouts see
:ref:`timeouts`.
``SQLALCHEMY_MAX_OVERFLOW`` Controls the number of connections that
can be created after the pool reached
its maximum size. When those additional
connections are returned to the pool,
they are disconnected and discarded.
``SQLALCHEMY_TRACK_MODIFICATIONS`` If set to ``True``, Flask-SQLAlchemy will
track modifications of objects and emit
signals. The default is ``None``, which
enables tracking but issues a warning
that it will be disabled by default in
the future. This requires extra memory
and should be disabled if not needed.
================================== =========================================
=================================== =========================================
``SQLALCHEMY_DATABASE_URI`` The database URI that should be used for
the connection. Examples:

- ``sqlite:////tmp/test.db``
- ``mysql://username:password@server/db``
``SQLALCHEMY_BINDS`` A dictionary that maps bind keys to
SQLAlchemy connection URIs. For more
information about binds see :ref:`binds`.
``SQLALCHEMY_ECHO`` If set to `True` SQLAlchemy will log all
the statements issued to stderr which can
be useful for debugging.
``SQLALCHEMY_RECORD_QUERIES`` Can be used to explicitly disable or
enable query recording. Query recording
automatically happens in debug or testing
mode. See :func:`get_debug_queries` for
more information.
``SQLALCHEMY_NATIVE_UNICODE`` Can be used to explicitly disable native
unicode support. This is required for
some database adapters (like PostgreSQL
on some Ubuntu versions) when used with
improper database defaults that specify
encoding-less databases.
``SQLALCHEMY_POOL_SIZE`` The size of the database pool. Defaults
to the engine's default (usually 5)
``SQLALCHEMY_POOL_TIMEOUT`` Specifies the connection timeout for the
pool. Defaults to 10.
``SQLALCHEMY_POOL_RECYCLE`` Number of seconds after which a
connection is automatically recycled.
This is required for MySQL, which removes
connections after 8 hours idle by
default. Note that Flask-SQLAlchemy
automatically sets this to 2 hours if
MySQL is used. Some backends may use a
different default timeout value. For more
information about timeouts see
:ref:`timeouts`.
``SQLALCHEMY_MAX_OVERFLOW`` Controls the number of connections that
can be created after the pool reached
its maximum size. When those additional
connections are returned to the pool,
they are disconnected and discarded.
``SQLALCHEMY_TRACK_MODIFICATIONS`` If set to ``True``, Flask-SQLAlchemy will
track modifications of objects and emit
signals. The default is ``None``, which
enables tracking but issues a warning
that it will be disabled by default in
the future. This requires extra memory
and should be disabled if not needed.
``SQLALCHEMY_CREATOR`` Can be used to bring a creator function
which returns a DBAPI connection. The
underlying connection pool will use this
function to create all new database
connections.
``SQLALCHEMY_ECHO_POOL`` If set to ``True``, the connection pool will
log all checkouts/checkins to the logging
stream.
``SQLALCHEMY_POOL_PRE_PING`` If set to ``True``, the connection pool will
test whether connections are live upon each
checkout.

.. Warning::
You must have SQLAlchemy 1.2+ to use
this option
``SQLALCHEMY_POOL_RESET_ON_RETURN`` Can be used to set a default action when
connections are returned to the pool.
=================================== =========================================

.. versionadded:: 0.8
The ``SQLALCHEMY_NATIVE_UNICODE``, ``SQLALCHEMY_POOL_SIZE``,
Expand Down
8 changes: 8 additions & 0 deletions flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,10 @@ def init_app(self, app):
app.config.setdefault('SQLALCHEMY_POOL_RECYCLE', None)
app.config.setdefault('SQLALCHEMY_MAX_OVERFLOW', None)
app.config.setdefault('SQLALCHEMY_COMMIT_ON_TEARDOWN', False)
app.config.setdefault('SQLALCHEMY_CREATOR', None)
app.config.setdefault('SQLALCHEMY_ECHO_POOL', None)
app.config.setdefault('SQLALCHEMY_POOL_PRE_PING', None)
app.config.setdefault('SQLALCHEMY_POOL_RESET_ON_RETURN', None)
track_modifications = app.config.setdefault(
'SQLALCHEMY_TRACK_MODIFICATIONS', None
)
Expand Down Expand Up @@ -866,6 +870,10 @@ def _setdefault(optionkey, configkey):
_setdefault('pool_timeout', 'SQLALCHEMY_POOL_TIMEOUT')
_setdefault('pool_recycle', 'SQLALCHEMY_POOL_RECYCLE')
_setdefault('max_overflow', 'SQLALCHEMY_MAX_OVERFLOW')
_setdefault('creator', 'SQLALCHEMY_CREATOR')
_setdefault('echo_pool', 'SQLALCHEMY_ECHO_POOL')
_setdefault('pool_pre_ping', 'SQLALCHEMY_POOL_PRE_PING')
_setdefault('pool_reset_on_return', 'SQLALCHEMY_POOL_RESET_ON_RETURN')

def apply_driver_hacks(self, app, info, options):
"""This method is called before engine creation and used to inject
Expand Down