Add regenerate-search

This commit is contained in:
Xavier Bustamante Talavera 2018-10-31 12:27:16 +01:00
parent 3962dfe3b8
commit afb2815883
3 changed files with 31 additions and 3 deletions

View File

@ -35,6 +35,7 @@ class Devicehub(Teal):
instance_relative_config, root_path, Auth)
self.dummy = Dummy(self)
self.before_request(self.register_db_events_listeners)
self.cli.command('regenerate-search')(self.regenerate_search)
def register_db_events_listeners(self):
"""Registers the SQLAlchemy event listeners."""
@ -44,3 +45,9 @@ class Devicehub(Teal):
def _init_db(self):
super()._init_db()
DeviceSearch.set_all_devices_tokens_if_empty(self.db.session)
def regenerate_search(self):
"""Re-creates from 0 all the search tables."""
DeviceSearch.regenerate_search_table(self.db.session)
db.session.commit()
print('Done.')

View File

@ -73,9 +73,15 @@ class DeviceSearch(db.Model):
it deletes unlogged tables as ours.
"""
if not DeviceSearch.query.first():
for device in Device.query:
if not isinstance(device, Component):
cls.set_device_tokens(session, device)
cls.regenerate_search_table(session)
@classmethod
def regenerate_search_table(cls, session: db.Session):
"""Deletes and re-computes all the search table."""
DeviceSearch.query.delete()
for device in Device.query:
if not isinstance(device, Component):
cls.set_device_tokens(session, device)
@classmethod
def set_device_tokens(cls, session: db.Session, device: Device):

View File

@ -190,6 +190,21 @@ def test_device_search_all_devices_token_if_empty(app: Devicehub, user: UserClie
assert i['items']
def test_device_search_regenerate_table(app: DeviceSearch, user: UserClient):
user.post(file('basic.snapshot'), res=Snapshot)
i, _ = user.get(res=Device, query=[('search', 'Desktop')])
assert i['items'], 'Normal search works'
with app.app_context():
app.db.session.execute('TRUNCATE TABLE {}'.format(DeviceSearch.__table__.name))
app.db.session.commit()
i, _ = user.get(res=Device, query=[('search', 'Desktop')])
assert not i['items'], 'Truncate deleted all items'
runner = app.test_cli_runner()
runner.invoke(args=['regenerate-search'], catch_exceptions=False)
i, _ = user.get(res=Device, query=[('search', 'Desktop')])
assert i['items'], 'Regenerated re-made the table'
def test_device_query_search(user: UserClient):
# todo improve
user.post(file('basic.snapshot'), res=Snapshot)