Skip to content

Commit db02fd4

Browse files
authored
Merge pull request pallets-eco#731 from lbeaufort/663-add-error-missing-uri-no-binds
Default SQLALCHEMY_DATABASE_URI to None, error if no URI or binds
2 parents 324a670 + bde166e commit db02fd4

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

CHANGES.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ Unreleased
66
- Drop support for Python 3.4.
77
- Bump minimum version of Flask to 1.0.4.
88
- Bump minimum version of SQLAlchemy to 1.2.
9-
- Set `SQLALCHEMY_TRACK_MODIFICATIONS` to `False` by default,
9+
- Set ``SQLALCHEMY_TRACK_MODIFICATIONS`` to ``False`` by default,
1010
remove deprecation warning (:pr:`727`)
11+
- Remove default ``'sqlite:///:memory:'`` setting for
12+
``SQLALCHEMY_DATABASE_URI``, raise error when both ``SQLALCHEMY_DATABASE_URI``
13+
and ``SQLALCHEMY_BINDS`` are unset (:pr:`731`)
1114

1215

1316
Version 2.4.1

docs/config.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ A list of configuration keys currently understood by the extension:
106106
* ``SQLALCHEMY_POOL_RECYCLE``
107107
* ``SQLALCHEMY_MAX_OVERFLOW``
108108

109+
.. versionchanged:: 3.0
110+
111+
* ``SQLALCHEMY_TRACK_MODIFICATIONS`` configuration key now defaults to ``False``
112+
* ``SQLALCHEMY_DATABASE_URI`` configuration key no longer defaults to ``'sqlite:///:memory:'``
109113

110114
Connection URI Format
111115
---------------------

flask_sqlalchemy/__init__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -810,19 +810,18 @@ def init_app(self, app):
810810
of an application not initialized that way or connections will
811811
leak.
812812
"""
813+
813814
# We intentionally don't set self.app = app, to support multiple
814815
# applications. If the app is passed in the constructor,
815816
# we set it and don't support multiple applications.
816-
if (
817-
'SQLALCHEMY_DATABASE_URI' not in app.config and
818-
'SQLALCHEMY_BINDS' not in app.config
817+
if not (
818+
app.config.get('SQLALCHEMY_DATABASE_URI')
819+
or app.config.get('SQLALCHEMY_BINDS')
819820
):
820-
warnings.warn(
821-
'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
822-
'Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".'
823-
)
821+
raise RuntimeError('Either SQLALCHEMY_DATABASE_URI '
822+
'or SQLALCHEMY_BINDS needs to be set.')
824823

825-
app.config.setdefault('SQLALCHEMY_DATABASE_URI', 'sqlite:///:memory:')
824+
app.config.setdefault('SQLALCHEMY_DATABASE_URI', None)
826825
app.config.setdefault('SQLALCHEMY_BINDS', None)
827826
app.config.setdefault('SQLALCHEMY_NATIVE_UNICODE', None)
828827
app.config.setdefault('SQLALCHEMY_ECHO', False)

tests/test_config.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,39 @@ def app_nr(app):
1818

1919
class TestConfigKeys:
2020

21-
def test_defaults(self, app, recwarn):
21+
def test_default_error_without_uri_or_binds(self, app, recwarn):
2222
"""
23-
Test all documented config values in the order they appear in our
24-
documentation: http://flask-sqlalchemy.pocoo.org/dev/config/
23+
Test that default configuration throws an error because
24+
SQLALCHEMY_DATABASE_URI and SQLALCHEMY_BINDS are unset
2525
"""
2626

2727
fsa.SQLAlchemy(app)
2828

29-
# Expecting no warnings for default config
29+
# Our pytest fixture for creating the app sets
30+
# SQLALCHEMY_DATABASE_URI, so undo that here so that we
31+
# can inspect what FSA does below:
32+
del app.config['SQLALCHEMY_DATABASE_URI']
33+
34+
with pytest.raises(RuntimeError) as exc_info:
35+
fsa.SQLAlchemy(app)
36+
37+
expected = 'Either SQLALCHEMY_DATABASE_URI ' \
38+
'or SQLALCHEMY_BINDS needs to be set.'
39+
assert exc_info.value.args[0] == expected
40+
41+
def test_defaults_with_uri(self, app, recwarn):
42+
"""
43+
Test default config values when URI is provided, in the order they
44+
appear in the documentation: http://flask-sqlalchemy.pocoo.org/dev/config/
45+
46+
Our pytest fixture for creating the app sets SQLALCHEMY_DATABASE_URI
47+
"""
48+
49+
fsa.SQLAlchemy(app)
50+
51+
# Expecting no warnings for default config with URI
3052
assert len(recwarn) == 0
3153

32-
assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///:memory:'
3354
assert app.config['SQLALCHEMY_BINDS'] is None
3455
assert app.config['SQLALCHEMY_ECHO'] is False
3556
assert app.config['SQLALCHEMY_RECORD_QUERIES'] is None
@@ -41,18 +62,6 @@ def test_defaults(self, app, recwarn):
4162
assert app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] is False
4263
assert app.config['SQLALCHEMY_ENGINE_OPTIONS'] == {}
4364

44-
def test_uri_binds_warning(self, app, recwarn):
45-
# Let's trigger the warning
46-
del app.config['SQLALCHEMY_DATABASE_URI']
47-
fsa.SQLAlchemy(app)
48-
49-
# and verify it showed up as expected
50-
assert len(recwarn) == 1
51-
expect = 'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS' \
52-
' is set. Defaulting SQLALCHEMY_DATABASE_URI to'\
53-
' "sqlite:///:memory:".'
54-
assert recwarn[0].message.args[0] == expect
55-
5665
def test_engine_creation_ok(self, app, recwarn):
5766
""" create_engine() isn't called until needed. Let's make sure we can do that without
5867
errors or warnings.

0 commit comments

Comments
 (0)