40
40
before_models_committed = _signals .signal ('before-models-committed' )
41
41
42
42
43
+ def _sa_url_set (url , ** kwargs ):
44
+ try :
45
+ url = url .set (** kwargs )
46
+ except AttributeError :
47
+ # SQLAlchemy <= 1.3
48
+ for key , value in kwargs .items ():
49
+ setattr (url , key , value )
50
+
51
+ return url
52
+
53
+
54
+ def _sa_url_query_setdefault (url , ** kwargs ):
55
+ query = dict (url .query )
56
+
57
+ for key , value in kwargs .items ():
58
+ query .setdefault (key , value )
59
+
60
+ return _sa_url_set (url , query = query )
61
+
62
+
43
63
def _make_table (db ):
44
64
def _make_table (* args , ** kwargs ):
45
65
if len (args ) > 1 and isinstance (args [1 ], db .Column ):
@@ -552,7 +572,7 @@ def get_engine(self):
552
572
return self ._engine
553
573
554
574
sa_url = make_url (uri )
555
- options = self .get_options (sa_url , echo )
575
+ sa_url , options = self .get_options (sa_url , echo )
556
576
self ._engine = rv = self ._sa .create_engine (sa_url , options )
557
577
558
578
if _record_queries (self ._app ):
@@ -566,8 +586,9 @@ def get_engine(self):
566
586
def get_options (self , sa_url , echo ):
567
587
options = {}
568
588
569
- self ._sa .apply_pool_defaults (self ._app , options )
570
- self ._sa .apply_driver_hacks (self ._app , sa_url , options )
589
+ options = self ._sa .apply_pool_defaults (self ._app , options )
590
+ sa_url , options = self ._sa .apply_driver_hacks (self ._app , sa_url , options )
591
+
571
592
if echo :
572
593
options ['echo' ] = echo
573
594
@@ -578,7 +599,7 @@ def get_options(self, sa_url, echo):
578
599
# Give options set in SQLAlchemy.__init__() ultimate priority
579
600
options .update (self ._sa ._engine_options )
580
601
581
- return options
602
+ return sa_url , options
582
603
583
604
584
605
def get_state (app ):
@@ -861,6 +882,11 @@ def shutdown_session(response_or_exc):
861
882
return response_or_exc
862
883
863
884
def apply_pool_defaults (self , app , options ):
885
+ """
886
+ .. versionchanged:: 2.5
887
+ Returns the ``options`` dict, for consistency with
888
+ :meth:`apply_driver_hacks`.
889
+ """
864
890
def _setdefault (optionkey , configkey ):
865
891
value = app .config [configkey ]
866
892
if value is not None :
@@ -869,6 +895,7 @@ def _setdefault(optionkey, configkey):
869
895
_setdefault ('pool_timeout' , 'SQLALCHEMY_POOL_TIMEOUT' )
870
896
_setdefault ('pool_recycle' , 'SQLALCHEMY_POOL_RECYCLE' )
871
897
_setdefault ('max_overflow' , 'SQLALCHEMY_MAX_OVERFLOW' )
898
+ return options
872
899
873
900
def apply_driver_hacks (self , app , sa_url , options ):
874
901
"""This method is called before engine creation and used to inject
@@ -879,9 +906,15 @@ def apply_driver_hacks(self, app, sa_url, options):
879
906
The default implementation provides some saner defaults for things
880
907
like pool sizes for MySQL and sqlite. Also it injects the setting of
881
908
`SQLALCHEMY_NATIVE_UNICODE`.
909
+
910
+ .. versionchanged:: 2.5
911
+ Returns ``(sa_url, options)``. SQLAlchemy 1.4 made the URL
912
+ immutable, so any changes to it must now be passed back up
913
+ to the original caller.
882
914
"""
883
915
if sa_url .drivername .startswith ('mysql' ):
884
- sa_url .query .setdefault ('charset' , 'utf8' )
916
+ sa_url = _sa_url_query_setdefault (sa_url , charset = "utf8" )
917
+
885
918
if sa_url .drivername != 'mysql+gaerdbms' :
886
919
options .setdefault ('pool_size' , 10 )
887
920
options .setdefault ('pool_recycle' , 7200 )
@@ -911,7 +944,9 @@ def apply_driver_hacks(self, app, sa_url, options):
911
944
912
945
# if it's not an in memory database we make the path absolute.
913
946
if not detected_in_memory :
914
- sa_url .database = os .path .join (app .root_path , sa_url .database )
947
+ sa_url = _sa_url_set (
948
+ sa_url , database = os .path .join (app .root_path , sa_url .database )
949
+ )
915
950
916
951
unu = app .config ['SQLALCHEMY_NATIVE_UNICODE' ]
917
952
if unu is None :
@@ -932,6 +967,8 @@ def apply_driver_hacks(self, app, sa_url, options):
932
967
DeprecationWarning
933
968
)
934
969
970
+ return sa_url , options
971
+
935
972
@property
936
973
def engine (self ):
937
974
"""Gives access to the engine. If the database configuration is bound
0 commit comments