Add regenerate-search
This commit is contained in:
parent
3962dfe3b8
commit
afb2815883
|
@ -35,6 +35,7 @@ class Devicehub(Teal):
|
||||||
instance_relative_config, root_path, Auth)
|
instance_relative_config, root_path, Auth)
|
||||||
self.dummy = Dummy(self)
|
self.dummy = Dummy(self)
|
||||||
self.before_request(self.register_db_events_listeners)
|
self.before_request(self.register_db_events_listeners)
|
||||||
|
self.cli.command('regenerate-search')(self.regenerate_search)
|
||||||
|
|
||||||
def register_db_events_listeners(self):
|
def register_db_events_listeners(self):
|
||||||
"""Registers the SQLAlchemy event listeners."""
|
"""Registers the SQLAlchemy event listeners."""
|
||||||
|
@ -44,3 +45,9 @@ class Devicehub(Teal):
|
||||||
def _init_db(self):
|
def _init_db(self):
|
||||||
super()._init_db()
|
super()._init_db()
|
||||||
DeviceSearch.set_all_devices_tokens_if_empty(self.db.session)
|
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.')
|
||||||
|
|
|
@ -73,6 +73,12 @@ class DeviceSearch(db.Model):
|
||||||
it deletes unlogged tables as ours.
|
it deletes unlogged tables as ours.
|
||||||
"""
|
"""
|
||||||
if not DeviceSearch.query.first():
|
if not DeviceSearch.query.first():
|
||||||
|
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:
|
for device in Device.query:
|
||||||
if not isinstance(device, Component):
|
if not isinstance(device, Component):
|
||||||
cls.set_device_tokens(session, device)
|
cls.set_device_tokens(session, device)
|
||||||
|
|
|
@ -190,6 +190,21 @@ def test_device_search_all_devices_token_if_empty(app: Devicehub, user: UserClie
|
||||||
assert i['items']
|
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):
|
def test_device_query_search(user: UserClient):
|
||||||
# todo improve
|
# todo improve
|
||||||
user.post(file('basic.snapshot'), res=Snapshot)
|
user.post(file('basic.snapshot'), res=Snapshot)
|
||||||
|
|
Reference in a new issue