def __execute(self, sql, execution_log=None): db = self.__mysql_connect() cursor = db.cursor() cursor._defer_warnings = True curr_statement = None try: statments = MySQL._parse_sql_statements(sql) if len(sql.strip(' \t\n\r')) != 0 and len(statments) == 0: raise Exception("invalid sql syntax '%s'" % sql.encode("utf-8")) for statement in statments: curr_statement = statement affected_rows = cursor.execute( statement.encode(self.__mysql_script_encoding)) if execution_log: execution_log( "%s\n-- %d row(s) affected\n" % (statement, affected_rows and int(affected_rows) or 0)) cursor.close() db.commit() except Exception as e: db.rollback() raise MigrationException("error executing migration: %s" % e, curr_statement) finally: db.close()
def __execute(self, sql, execution_log=None): conn = self.__connect() cursor = conn.cursor() curr_statement = None try: statments = Oracle._parse_sql_statements(sql) if len(sql.strip(' \t\n\r')) != 0 and len(statments) == 0: raise Exception("invalid sql syntax '%s'" % sql) for statement in statments: curr_statement = statement affected_rows = cursor.execute( statement.encode(self.__script_encoding)) if execution_log: execution_log( "%s\n-- %d row(s) affected\n" % (statement, affected_rows and int(affected_rows) or 0)) cursor.close() conn.commit() conn.close() except Exception, e: conn.rollback() cursor.close() conn.close() raise MigrationException("error executing migration: %s" % e, curr_statement)
def __change_db_version(self, version, migration_file_name, sql_up, sql_down, up=True, execution_log=None, label_version=None): params = {} params['version'] = version conn = self.__connect() cursor = conn.cursor() if up: # moving up and storing history sql = "insert into %s (id, version, label, name, sql_up, sql_down) values (%s_seq.nextval, :version, :label, :migration_file_name, :sql_up, :sql_down)" % (self.__version_table, self.__version_table) sql_up = sql_up and sql_up.encode(self.__script_encoding) or "" v_sql_up = cursor.var( self.__driver.CLOB, len(sql_up)) v_sql_up.setvalue( 0, sql_up ) params['sql_up'] = sql_up sql_down = sql_down and sql_down.encode(self.__script_encoding) or "" v_sql_down = cursor.var( self.__driver.CLOB, len(sql_down)) v_sql_down.setvalue( 0, sql_down ) params['sql_down'] = sql_down params['migration_file_name'] = migration_file_name params['label'] = label_version else: # moving down and deleting from history sql = "delete from %s where version = :version" % (self.__version_table) try: cursor.execute(sql.encode(self.__script_encoding), params) cursor.close() conn.commit() if execution_log: execution_log("migration %s registered\n" % (migration_file_name)) except Exception, e: conn.rollback() raise MigrationException(("error logging migration: %s" % e), migration_file_name)
def __execute(self, sql, execution_log=None): db = self.__mysql_connect() cursor = db.cursor() cursor._defer_warnings = True curr_statement = None try: for statement in self._parse_sql_statements(sql): curr_statement = statement affected_rows = cursor.execute(statement.encode(self.__mysql_script_encoding)) if execution_log: execution_log("%s\n-- %d row(s) affected\n" % (statement, affected_rows and int(affected_rows) or 0)) cursor.close() db.commit() db.close() except Exception, e: raise MigrationException("error executing migration: %s" % e, curr_statement)
def __execute(self, sql, execution_log=None): db = self.__mssql_connect() curr_statement = None try: statments = MSSQL._parse_sql_statements(sql) if len(sql.strip(' \t\n\r')) != 0 and len(statments) == 0: raise Exception("invalid sql syntax '%s'" % sql) for statement in statments: curr_statement = statement db.execute_non_query(statement) affected_rows = db.rows_affected if execution_log: execution_log( "%s\n-- %d row(s) affected\n" % (statement, affected_rows and int(affected_rows) or 0)) except Exception, e: db.cancel() raise MigrationException("error executing migration: %s" % e, curr_statement)
def __change_db_version(self, version, migration_file_name, sql_up, sql_down, up=True, execution_log=None, label_version=None): if up: if not label_version: label_version = "NULL" else: label_version = "\"%s\"" % (str(label_version)) # moving up and storing history sql = "insert into %s (version, label, name, sql_up, sql_down) values (\"%s\", %s, \"%s\", \"%s\", \"%s\");" % (self.__version_table, str(version), label_version, migration_file_name, sql_up.replace('"', '\\"'), sql_down.replace('"', '\\"')) else: # moving down and deleting from history sql = "delete from %s where version = \"%s\";" % (self.__version_table, str(version)) db = self.__mysql_connect() cursor = db.cursor() cursor._defer_warnings = True try: cursor.execute(sql.encode(self.__mysql_script_encoding)) if execution_log: execution_log("migration %s registered\n" % (migration_file_name)) except Exception, e: raise MigrationException("error logging migration: %s" % e, migration_file_name)
def __change_db_version(self, version, migration_file_name, sql_up, sql_down, up=True, execution_log=None, label_version=None): params = [] params.append(version) if up: # moving up and storing history sql = "insert into %s (version, label, name, sql_up, sql_down) values (%%s, %%s, %%s, %%s, %%s);" % ( self.__version_table) params.append(label_version) params.append(migration_file_name) params.append( sql_up and sql_up.encode(self.__mssql_script_encoding) or "") params.append( sql_down and sql_down.encode(self.__mssql_script_encoding) or "") else: # moving down and deleting from history sql = "delete from %s where version = %%s;" % ( self.__version_table) db = self.__mssql_connect() try: db.execute_non_query(sql.encode(self.__mssql_script_encoding), tuple(params)) if execution_log: execution_log("migration %s registered\n" % (migration_file_name)) except Exception as e: db.cancel() raise MigrationException("error logging migration: %s" % e, migration_file_name) finally: db.close()
def __execute(self, sql, execution_log=None): db = self.__mysql_connect() cursor = db.cursor() cursor._defer_warnings = True curr_statement = None try: statements = MySQL._parse_sql_statements(sql) if len(sql.strip(' \t\n\r')) != 0 and len(statements) == 0: raise Exception("invalid sql syntax '%s'" % sql) if not self._MySQL__execute_inorder: alter_tables = [ a for a in statements if len(a.split(' ')) > 2 and a.split(' ')[:2] == ['alter', 'table'] ] def rfunction(dic, tup): dic[tup[0]] = dic.get(tup[0], []) + [tup[1]] return dic table_alter = reduce( rfunction, map( lambda a: (a.split(' ')[2], ' '.join(a.split(' ')[3:])), alter_tables), {}) for table, alters in table_alter.iteritems(): subprocess.check_call( 'pt-online-schema-change --execute -h {} -P {} -u {} -p{} --alter \'{}\' D={},t={}' .format(self._MySQL__mysql_host, self._MySQL__mysql_port, self._MySQL__mysql_user, self._MySQL__mysql_passwd, ','.join(alters), self._MySQL__mysql_db, table), shell=True) statements = set(statements) - set(alter_tables) for statement in statements: curr_statement = statement curr_split = curr_statement.split(' ') if len(curr_split) > 2 and curr_split[:2] == [ 'alter', 'table' ]: table = curr_split[2] subprocess.check_call( 'pt-online-schema-change --execute -h {} -P {} -u {} -p{} --alter \'{}\' D={},t={}' .format(self._MySQL__mysql_host, self._MySQL__mysql_port, self._MySQL__mysql_user, self._MySQL__mysql_passwd, ' '.join(curr_split[3:]), self._MySQL__mysql_db, table), shell=True) else: affected_rows = cursor.execute( statement.encode(self.__mysql_script_encoding)) if execution_log: execution_log( "%s\n-- %d row(s) affected\n" % (statement, affected_rows and int(affected_rows) or 0)) cursor.close() db.commit() except Exception, e: db.rollback() raise MigrationException("error executing migration: %s" % e, curr_statement)