Skip to content

Commit

Permalink
Set SQLALCHEMY_TRACK_MODIFICATIONS to False by default
Browse files Browse the repository at this point in the history
- Update default value
- Update tests
- Update docs
  • Loading branch information
lbeaufort committed May 7, 2019
1 parent 3d3261f commit 2d40657
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Version 3.0.0

Unreleased

- Set `SQLALCHEMY_TRACK_MODIFICATIONS` to `False` by default,
remove deprecation warning (:pr:`727`)


Version 2.4.0
-------------
Expand Down
6 changes: 2 additions & 4 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ A list of configuration keys currently understood by the extension:
**Deprecated** as of v2.4 and will be removed in v3.0.
``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
signals. The default is ``False`` because
this requires extra memory
and should be disabled if not needed.
``SQLALCHEMY_ENGINE_OPTIONS`` A dictionary of keyword args to send to
:func:`~sqlalchemy.create_engine`. See
Expand Down
13 changes: 2 additions & 11 deletions flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __init__(self, db, autocommit=False, autoflush=True, **options):
bind = options.pop('bind', None) or db.engine
binds = options.pop('binds', db.get_binds(app))

if track_modifications is None or track_modifications:
if track_modifications:
_SessionSignalEvents.register(self)

SessionBase.__init__(
Expand Down Expand Up @@ -829,18 +829,9 @@ 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)
track_modifications = app.config.setdefault(
'SQLALCHEMY_TRACK_MODIFICATIONS', None
)
app.config.setdefault('SQLALCHEMY_TRACK_MODIFICATIONS', False)
app.config.setdefault('SQLALCHEMY_ENGINE_OPTIONS', {})

if track_modifications is None:
warnings.warn(FSADeprecationWarning(
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
'will be disabled by default in the future. Set it to True '
'or False to suppress this warning.'
))

# Deprecation warnings for config keys that should be replaced by SQLALCHEMY_ENGINE_OPTIONS.
utils.engine_config_warning(app.config, '3.0', 'SQLALCHEMY_POOL_SIZE', 'pool_size')
utils.engine_config_warning(app.config, '3.0', 'SQLALCHEMY_POOL_TIMEOUT', 'pool_timeout')
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def app(request):
app = flask.Flask(request.module.__name__)
app.testing = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
return app


Expand Down
34 changes: 5 additions & 29 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@ def app_nr(app):

class TestConfigKeys:

def test_defaults(self, app):
def test_defaults(self, app, recwarn):
"""
Test all documented config values in the order they appear in our
documentation: http://flask-sqlalchemy.pocoo.org/dev/config/
"""
# Our pytest fixture for creating the app sets
# SQLALCHEMY_TRACK_MODIFICATIONS, so we undo that here so that we
# can inspect what FSA does below:
del app.config['SQLALCHEMY_TRACK_MODIFICATIONS']

with pytest.warns(fsa.FSADeprecationWarning) as records:
fsa.SQLAlchemy(app)
fsa.SQLAlchemy(app)

# Only expecting one warning for the track modifications deprecation.
assert len(records) == 1
# Expecting no warnings for default config
assert len(recwarn) == 0

assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///:memory:'
assert app.config['SQLALCHEMY_BINDS'] is None
Expand All @@ -43,28 +38,9 @@ def test_defaults(self, app):
assert app.config['SQLALCHEMY_POOL_TIMEOUT'] is None
assert app.config['SQLALCHEMY_POOL_RECYCLE'] is None
assert app.config['SQLALCHEMY_MAX_OVERFLOW'] is None
assert app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] is None
assert app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] is False
assert app.config['SQLALCHEMY_ENGINE_OPTIONS'] == {}

def test_track_modifications_warning(self, app, recwarn):

# pytest fixuture sets SQLALCHEMY_TRACK_MODIFICATIONS = False
fsa.SQLAlchemy(app)

# So we shouldn't have any warnings
assert len(recwarn) == 0

# Let's trigger the warning
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = None
fsa.SQLAlchemy(app)

# and verify it showed up as expected
assert len(recwarn) == 1
expect = 'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead' \
' and will be disabled by default in the future. Set it' \
' to True or False to suppress this warning.'
assert recwarn[0].message.args[0] == expect

def test_uri_binds_warning(self, app, recwarn):
# Let's trigger the warning
del app.config['SQLALCHEMY_DATABASE_URI']
Expand Down

0 comments on commit 2d40657

Please sign in to comment.