def clean_database(self, delete_folders=True): """ Deletes data from all tables """ self.cancel_all_operations() LOGGER.warning("Your Database content will be deleted.") try: session = SessionMaker() for table in reversed(model.Base.metadata.sorted_tables): # We don't delete data from some tables, because those are # imported only during introspection which is done one time if table.name not in self.EXCLUDE_TABLES: try: session.open_session() con = session.connection() LOGGER.debug("Executing Delete From Table " + table.name) con.execute(table.delete()) session.commit() except Exception, e: # We cache exception here, in case some table does not exists and # to allow the others to be deleted LOGGER.warning(e) session.rollback() finally: session.close_session()
def clean_database(self, delete_folders=True): """ Deletes data from all tables """ self.cancel_all_operations() LOGGER.warning("Your Database content will be deleted.") try: session = SessionMaker() for table in reversed(model.Base.metadata.sorted_tables): # We don't delete data from some tables, because those are # imported only during introspection which is done one time if table.name not in self.EXCLUDE_TABLES: try: session.open_session() con = session.connection() LOGGER.debug("Executing Delete From Table " + table.name) con.execute(table.delete()) session.commit() except Exception as e: # We cache exception here, in case some table does not exists and # to allow the others to be deleted LOGGER.warning(e) session.rollback() finally: session.close_session() LOGGER.info("Database was cleanup!") except Exception as excep: LOGGER.warning(excep) raise # Now if the database is clean we can delete also project folders on disk if delete_folders: self.delete_project_folders() dao.store_entity(model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None))
def _evaluate_db_filter(self, filter_chain, expected_number): """ Evaluate filter on DB and assert number of results. """ session = SessionMaker() try: session.open_session() query = session.query(Datatype1) filter_str = filter_chain.get_sql_filter_equivalent("Datatype1") query = query.filter(eval(filter_str)) result = query.all() session.close_session() except Exception, excep: session.close_session() raise excep
def get_all_entities(self, entity_type): """ Retrieve all entities of a given type. """ result = [] session = None try: session = SessionMaker() session.open_session() result = session.query(entity_type).all() except Exception as excep: LOGGER.warning(excep) finally: if session: session.close_session() return result
def count_all_entities(self, entity_type): """ Count all entities of a given type currently stored in DB. """ result = 0 session = None try: session = SessionMaker() session.open_session() result = session.query(entity_type).count() except Exception as excep: LOGGER.warning(excep) finally: if session: session.close_session() return result
def _evaluate_db_filter(self, filter_chain, expected_number): """ Evaluate filter on DB and assert number of results. """ session = SessionMaker() try: session.open_session() query = session.query(Datatype1) filter_str = filter_chain.get_sql_filter_equivalent("Datatype1") query = query.filter(eval(filter_str)) result = query.all() session.close_session() except Exception as excep: session.close_session() raise excep self.assertEquals(expected_number, len(result), "Expected %s DTs after filtering with %s, " "but got %s instead." % (expected_number, filter_chain, len(result,)))
def _evaluate_db_filter(self, filter_chain, expected_number): """ Evaluate filter on DB and assert number of results. """ session = SessionMaker() try: session.open_session() query = session.query(Datatype1) filter_str = filter_chain.get_sql_filter_equivalent("Datatype1") query = query.filter(eval(filter_str)) result = query.all() session.close_session() except Exception as excep: session.close_session() raise excep assert expected_number == len(result), "Expected %s DTs after filtering with %s, "\ "but got %s instead." % (expected_number, filter_chain, len(result,))