def process(self,db,file_content,filename): ''' Read the file and extract from the CREATE stmt the type and name ''' command = "" db_object_search = re.search("CREATE\W*(\w*)\W*(\w*)\W*(\w*)\W*(\w*)",file_content,re.I) try: db_obj_type = db_object_search.group(1) db_obj_name = db_object_search.group(2) if(db_obj_type == "ALGORITHM"): db_obj_type = db_object_search.group(3) db_obj_name = db_object_search.group(4) command = "DROP {type} {name} ".format(type=db_obj_type,name=db_obj_name) L.info(command) except IndexError: L.fatal("Error parsing file contents [{}]".format(file_content)) raise Exception("Error parsing file contents") try: self.cursor.execute(command) except MyExcp as err: if ( err.errno == MyErrCode.ER_SP_DOES_NOT_EXIST or err.errno == MyErrCode.ER_TRG_DOES_NOT_EXIST ): pass
def bindRulesToTables(self, rules): ''' rule is a DictionaryType with 'table' which can be ALL or a table name and rules, which is the actual rules to attach to the specific table or to all tables in self.list_of_tables ''' for rule in rules: if rule[0] == 'ALL': # Attach rule (it is an array of objects) to all tables for table in self.tables_list.keys(): self.tables_list[table] += [ binded_rules.bind_all_to_sql(left_side_table=table) for binded_rules in rule[1] ] else: # attach the rule to only one table try: self.tables_list[rule[0]] += [ binded_rules.bind_all_to_sql(left_side_table=rule[0]) for binded_rules in rule[1] ] except KeyError as ke: L.fatal( "Database [{db_name}] has no such table [{tbl_name}]". format(db_name=self.current_db, tbl_name=ke)) L.fatal( "Please fix your rule files under database folder [{db_name}]" .format(db_name=self.current_db)) raise Exception("Missmatch between files and db tables")
def process(self, db, file_content, filename): ''' Read the file and extract from the CREATE stmt the type and name ''' command = "" db_object_search = re.search("CREATE\W*(\w*)\W*(\w*)\W*(\w*)\W*(\w*)", file_content, re.I) try: db_obj_type = db_object_search.group(1) db_obj_name = db_object_search.group(2) if (db_obj_type == "ALGORITHM"): db_obj_type = db_object_search.group(3) db_obj_name = db_object_search.group(4) command = "DROP {type} {name} ".format(type=db_obj_type, name=db_obj_name) L.info(command) except IndexError: L.fatal("Error parsing file contents [{}]".format(file_content)) raise Exception("Error parsing file contents") try: self.cursor.execute(command) except MyExcp as err: if (err.errno == MyErrCode.ER_SP_DOES_NOT_EXIST or err.errno == MyErrCode.ER_TRG_DOES_NOT_EXIST): pass
def validate_system(): ''' Checking for failed files in the upgrade DB, Failed files will immediatly fail -> fix those before upgrades can continue ''' actual_db_cnx = app.db.get_connection( ) # need this to know which files I already processed actual_db_cnx.database = upgrade_config['upgrade_tracking_database'] actual_cursor = actual_db_cnx.cursor() failed_files_sql = "SELECT COUNT(*) FROM rcom_sql_upgrades WHERE execution_status IN ('failed','failed_in_test')" actual_cursor.execute(failed_files_sql) res = actual_cursor.fetchall() if res[0][0] > 0: L.fatal( 'There are failed files in [{}.rcom_sql_upgrades] Execution stops until you fix it!' .format(upgrade_config['upgrade_tracking_database'])) raise Exception('We have failed sql upgrade files, fix them!')
def bindRulesToTables(self,rules): ''' rule is a DictionaryType with 'table' which can be ALL or a table name and rules, which is the actual rules to attach to the specific table or to all tables in self.list_of_tables ''' for rule in rules: if rule[0] == 'ALL': # Attach rule (it is an array of objects) to all tables for table in self.tables_list.keys(): self.tables_list[table] += [binded_rules.bind_all_to_sql(left_side_table=table) for binded_rules in rule[1]] else: # attach the rule to only one table try: self.tables_list[rule[0]] += [binded_rules.bind_all_to_sql(left_side_table=rule[0]) for binded_rules in rule[1]] except KeyError as ke: L.fatal("Database [{db_name}] has no such table [{tbl_name}]".format(db_name=self.current_db,tbl_name=ke)) L.fatal("Please fix your rule files under database folder [{db_name}]".format(db_name=self.current_db)) raise Exception("Missmatch between files and db tables")
def parse_to_TestRule_factory(single_rule, left_side_db, right_side_db): ''' Factory function to instantiate, parse and return the correct SQL object ''' # extract the rule name and action_params class_and_params = single_rule.split('[') action_params = [] ignore_params = [] try: params = class_and_params[1].replace(']', '').split(',') # extract ignore rules for param in params: if "ignore" in param: ignore_params += param.split('(')[1].replace(')', '').split('|') else: action_params.append(param) except IndexError: action_params = [] #instantiate the correct class (SqlRule+RuleName) try: TestRuleClass = globals()[class_and_params[0].capitalize().replace( '_', '')] except KeyError: file_name = right_side_db if left_side_db == right_side_db: file_name = "*.schk" L.fatal("The rule [{no_such_rule}] is not yet supported, or you have a syntax error! " + \ "Folder [{asset_folder}] File [{file_name}]".format(\ no_such_rule = class_and_params[0],\ asset_folder = left_side_db,\ file_name=file_name)) exit(-1) return TestRuleClass(single_rule, left_side_db, right_side_db, action_params, ignore_params)
def __init__(self, args,db=None): ''' Stores a dictionary of what to build @var cnx_proxy boolean : whether we use an injected DB connection or create our own. True == injected ''' # Process arguments if args.handle_all: self.what_to_handle = {'s':'All'} else: L.fatal('You must use --all to run this command!') self.assets_path = config.assets_folder self.folders = [] self.args = args # Store it in case we need to instantiate other iterators from within an iterator (like the drop it`) self.file_postfix = '.sql' if len(config.autocomplete['return']) > 1: self._return_type = config.autocomplete['return'] else: self._return_type = "\SP"
def test(limit_of_files_processed): ''' runs the upgrade SQLs on the test server limit_of_files_process can be 0 -> all un processed files or a number. If a number, Will run that number on the files, sorted by execution order ASC process file if fail, mark with [failed_in_test] && crash! @return boolean True for files where processed, false for none ''' test_cnx = app.db.get_test_Server_connection() actual_db_cnx = app.db.get_connection( ) # need this to know which files I already processed actual_db_cnx.database = upgrade_config['upgrade_tracking_database'] test_cursor = test_cnx.cursor() actual_cursor = actual_db_cnx.cursor() upgrade_folder = config.assets_folder + "/upgrades/current" L.info('Reading files from {}'.format(upgrade_folder)) # Get all files ready for running from the DB limit = '' if limit_of_files_processed > 0: limit = "LIMIT " + limit_of_files_processed sql = "SELECT file_name FROM {}.rcom_sql_upgrades WHERE execution_status ='pending_completion' ORDER BY execute_order ASC {}".format( upgrade_config['upgrade_tracking_database'], limit) actual_cursor.execute(sql) res = actual_cursor.fetchall() # check there actually are files to work with if len(res) == 0: L.info("No files to test upon where found") return False for file_to_run, in res: real_file_path_name = upgrade_folder + '/' + file_to_run L.info("About to execute [{}]".format(real_file_path_name)) f = open(real_file_path_name, 'r') file_content = f.read() f.close() sql_string_for_debug = '' try: for one_sql in file_content.split(';'): if len(one_sql) > 6: sql_string_for_debug = one_sql L.info("\n----------\nabout to run test SQL:\n{}\n".format( one_sql)) test_cursor.execute(one_sql) test_cnx.commit() except Exception as err: err_msg = str(err) L.fatal( 'The last SQL has caused an error. Aborting, and marking file as failed with Error:\n{}' .format(err)) L.fatal('File: [{}]'.format(real_file_path_name)) L.fatal("SQL: \n{}\n".format(sql_string_for_debug)) update_failed_sql = "UPDATE {}.rcom_sql_upgrades SET execution_Status='failed_in_test',error_message=%s WHERE file_name = %s".format( upgrade_config['upgrade_tracking_database']) params = (err_msg, file_to_run) actual_cursor.execute(update_failed_sql, params) actual_db_cnx.commit() raise err else: L.debug('marking file as completed_in_test') update_sql = "UPDATE {}.rcom_sql_upgrades SET execution_Status='completed_in_test' WHERE file_name = %s".format( upgrade_config['upgrade_tracking_database']) params = (file_to_run, ) actual_cursor.execute(update_sql, params) actual_db_cnx.commit() return True