def create_non_exist_tables(self, table_diff): """ create tables that do not exist from the table difference between the current database and the configuration """ if len(table_diff) > 0: for table in table_diff: Table_Init.create_table(table, self.global_config) self.log.info('Updated database schema, table names now match configuration.') else: self.log.info('Database Schema and Configuration table names already match.')
def test_create_table(self,gci_get_db_dir): database.create_default_database(self.gci) Table_Init.create_table('TestTable1',self.gci) connection = sqlite3.connect(self.gci.get_db_dir() + '/honeyDB.sqlite') cursor = connection.cursor() table_list = [] for table_tuple in cursor.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall(): table_list.append(table_tuple[0]) self.assertIn('TestTable1', table_list) connection.close()
def update_table_structure(self): cfg_schema = self.create_dict_config_column_list() db_schema = DataValidator().get_schema() db_schema_sans_defaults = self.create_dict_transformed_column_list(db_schema) for table in cfg_schema: if not [(x[1], x[2]) for x in cfg_schema[table]] == \ [(x[1], x[2]) for x in db_schema_sans_defaults[table]]: Table_Init.change_table_structure( table, cfg_schema[table], db_schema[table], self.global_config)
def test_verify_data_types(self,gci_get_db_dir): database.create_default_database(self.gci) good_list = database.create_dict_config_column_list(self.gci).get('test_telnet_test') self.assertIsNone(Table_Init.verify_data_types(good_list)) bad_list = database.create_dict_config_column_list(self.gci).get('test_telnet_test') bad_list[0][2] = 'ASDF' self.assertRaises(ValueError, Table_Init.verify_data_types, bad_list)
def update_schema(self): """ Updates the database when columns have been added to, or removed from, the schema. """ # Create any new tables that have been added to the plugin # config schema. db_tables = DataValidator().get_tables() cfg_tables = get_config_table_list( self.global_config.get_ports(), self.global_config.get_plugin_dictionary()) table_diff = list(set(cfg_tables) - set(db_tables)) self.create_non_exist_tables(table_diff) # Populate the newly created tables with their column # definitions. DataValidator().update_tables_and_schema() self.update_table_structure() #create indexes on plugin tables if they do not exist Table_Init.create_index(cfg_tables,self.global_config)
def test_check_table_exists(self,gci_get_db_dir): database.create_default_database(self.gci) self.assertTrue(Table_Init.check_table_exists('test_http_test',self.gci)) self.assertTrue(Table_Init.check_table_exists('test_telnet_test',self.gci)) self.assertFalse(Table_Init.check_table_exists('TESTTABLE1',self.gci)) self.assertFalse(Table_Init.check_table_exists('TestTable2',self.gci))