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

fix sql session error in auto update related index #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion flask_msearch/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import logging
import sys

from flask_sqlalchemy import models_committed
from flask_sqlalchemy import models_committed, before_models_committed
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.inspection import inspect
from werkzeug import import_string
Expand Down Expand Up @@ -111,8 +111,18 @@ def _signal_connect(self, app):
self._signal = import_string(signal)
else:
self._signal = signal
before_models_committed.connect(self._before_models_committed)
models_committed.connect(self.index_signal)

def _before_models_committed(self, sender, changes):
self.db.session.flush()
for instance, operation in changes:
if hasattr(instance, '__searchable__'):
for field in getattr(instance, '__searchable__', []):
if '.' in field:
splits = field.split('.')
getattr(instance, splits[0])

def index_signal(self, sender, changes):
return self._signal(self, sender, changes)

Expand Down
22 changes: 19 additions & 3 deletions test/test_whoosh.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ def test_field_search(self):
results = self.Post.query.msearch('tag', fields=['tag.name']).all()
self.assertEqual(len(results), 2)

results = self.Post.query.msearch('changed', fields=['title']).all()
self.assertEqual(len(results), 0)

post2 = self.Post.query.filter(self.Post.id == post2.id).first()
post2.title = 'changed title'
self.db.session.commit()

results = self.Post.query.msearch('changed', fields=['title']).all()
self.assertEqual(len(results), 1)

post3 = self.Post(title=title1, content=content1, tag_id=tag1.id)
post3.save(self.db)

results = self.Post.query.msearch('tag', fields=['tag.name']).all()
self.assertEqual(len(results), 3)


class TestSearchHybridProp(TestMixin, SearchTestBase):
def setUp(self):
Expand Down Expand Up @@ -270,10 +286,10 @@ def save(self, db):

if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromNames([
'test_whoosh.TestSearch',
# 'test_whoosh.TestSearch',
# 'test_whoosh.TestPrimaryKey',
'test_whoosh.TestRelationSearch',
'test_whoosh.TestSearchHybridProp',
'test_whoosh.TestHybridPropTypeHint',
# 'test_whoosh.TestSearchHybridProp',
# 'test_whoosh.TestHybridPropTypeHint',
])
unittest.TextTestRunner(verbosity=1).run(suite)