Skip to content

Commit 149b42c

Browse files
committed
db: get rid of code to replace session object
This was causing a new session object to be created anytime a new app was initialized which would leave existing sessions and transactions open with no discernible way to close them (until GC runs I believe). I'm not sure why this code was here in the first place but it doesn't seem to add any value. If you want split scope sessions, you can easily do that with Flask-WebTest.
1 parent 619aa28 commit 149b42c

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

keg/db/__init__.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
from __future__ import absolute_import
22

3-
from flask_sqlalchemy import SQLAlchemy
3+
import flask_sqlalchemy as fsa
44

55
from keg.signals import testing_run_start, db_init_pre, db_init_post, db_clear_pre, db_clear_post
66
from keg.utils import visit_modules
77

88

9-
class KegSQLAlchemy(SQLAlchemy):
10-
11-
def init_app(self, app):
12-
SQLAlchemy.init_app(self, app)
13-
if app.testing:
14-
self.testing_scoped_session()
15-
16-
def testing_scoped_session(self):
17-
# don't want to have to import this if we are in production, so put import
18-
# inside of the method
19-
from flask_webtest import get_scopefunc
20-
21-
# flask-sqlalchemy creates the session when the class is initialized. We have to re-create
22-
# with different session options and override the session attribute with the new session
23-
db.session = db.create_scoped_session(options={'scopefunc': get_scopefunc()})
9+
class KegSQLAlchemy(fsa.SQLAlchemy):
2410

2511
def get_engines(self, app):
2612
# the default engine doesn't have a bind

keg/tests/test_db.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from keg import current_app
66
from keg.db import db
77
from keg.signals import db_init_pre, db_init_post, db_clear_post, db_clear_pre
8+
from keg.testing import invoke_command
89

910
import keg_apps.db.model.entities as ents
1011
from keg_apps.db.app import DBApp
@@ -102,3 +103,13 @@ def catch_clear_post(app):
102103
assert self.init_post_connected
103104
assert self.clear_pre_connected
104105
assert self.clear_post_connected
106+
107+
108+
class TestKegSQLAlchemy(object):
109+
def test_session_ids_with_cli_invocation(self):
110+
sess_id = id(db.session)
111+
112+
result = invoke_command(DBApp, 'hello')
113+
assert 'hello db cli' in result.output
114+
115+
assert id(db.session) == sess_id

keg_apps/db/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ class DBApp(Keg):
88
db_enabled = True
99

1010

11+
@DBApp.cli.command()
12+
def hello():
13+
print('hello db cli')
14+
15+
1116
if __name__ == '__main__':
1217
DBApp.cli.main()

0 commit comments

Comments
 (0)