def init_db(self, name: str, org_name: str, org_id: str, tag_url: boltons.urlutils.URL, tag_token: uuid.UUID, erase: bool, common: bool): """Creates an inventory. This creates the database and adds the inventory to the inventory tables with the passed-in settings, and does nothing if the inventory already exists. After you create the inventory you might want to create an user executing *dh user add*. """ assert _app_ctx_stack.top, 'Use an app context.' print('Initializing database...'.ljust(30), end='') with click_spinner.spinner(): if erase: self.db.drop_all(common_schema=common) assert not db.has_schema( self.id), 'Schema {} already exists.'.format(self.id) exclude_schema = 'common' if not common else None self._init_db(exclude_schema=exclude_schema) InventoryDef.set_inventory_config(name, org_name, org_id, tag_url, tag_token) DeviceSearch.set_all_devices_tokens_if_empty(self.db.session) self._init_resources(exclude_schema=exclude_schema) self.db.session.commit() print('done.')
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('inv', 'search') i, _ = user.get(res=Device, query=[('search', 'Desktop')]) assert i['items'], 'Regenerated re-made the table'
def test_device_search_all_devices_token_if_empty(app: Devicehub, user: UserClient): """Ensures DeviceSearch can regenerate itself when the table is empty.""" user.post(file('basic.snapshot'), res=Snapshot) 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 len(i['items']) with app.app_context(): DeviceSearch.set_all_devices_tokens_if_empty(app.db.session) app.db.session.commit() i, _ = user.get(res=Device, query=[('search', 'Desktop')]) assert i['items']
def final_flush(self): """A regular flush that performs expensive final operations through Devicehub (like saving searches), so it is thought to be used once in each request, at the very end before a commit. """ # This was done before with an ``before_commit`` sqlalchemy event # however it is too fragile –it does not detect previously-flushed # things # This solution makes this more aware to the user, although # has the same problem. This is not final solution. # todo a solution would be for this session to save, on every # flush, all the new / dirty interesting things in a variable # until DeviceSearch is executed from ereuse_devicehub.resources.device.search import DeviceSearch DeviceSearch.update_modified_devices(session=self)
def regenerate_search(self): """Re-creates from 0 all the search tables.""" DeviceSearch.regenerate_search_table(self.db.session) db.session.commit() print('Done.')