def _get_transform(server1, server2, object1, object2, options): """Get the transformation SQL statements This method generates the SQL statements to transform the destination object based on direction of the compare. server1[in] first server connection server2[in] second server connection object1 the first object in the compare in the form: (db.name) object2 the second object in the compare in the form: (db.name) options[in] a dictionary containing the options for the operation: (quiet, etc.) Returns tuple - (bool - same db name?, list of transformation statements) """ from mysql.utilities.common.database import Database from mysql.utilities.common.sql_transform import SQLTransformer obj_type = None direction = options.get("changes-for", "server1") # If there is no dot, we do not have the format 'db_name.obj_name' for # object1 and therefore must treat it as a database name. if object1.find(".") == -1: obj_type = "DATABASE" # We are working with databases so db and name need to be set # to the database name to tell the get_object_definition() method # to retrieve the database information. db1 = object1 db2 = object2 name1 = object1 name2 = object2 else: try: db1, name1 = object1.split(".") db2, name2 = object2.split(".") except: raise UtilError("Invalid object name arguments for _get_transform" "(): %s, %s." % (object1, object2)) db_1 = Database(server1, db1, options) db_2 = Database(server2, db2, options) if obj_type is None: obj_type = db_1.get_object_type(name1) transform_str = [] obj1 = db_1.get_object_definition(db1, name1, obj_type) obj2 = db_2.get_object_definition(db2, name2, obj_type) # Get the transformation based on direction. transform_str = [] same_db_name = True xform = SQLTransformer(db_1, db_2, obj1[0], obj2[0], obj_type, options.get("verbosity", 0)) differences = xform.transform_definition() if differences is not None and len(differences) > 0: transform_str.extend(differences) return transform_str
def _get_transform(server1, server2, object1, object2, options, object_type): """Get the transformation SQL statements This method generates the SQL statements to transform the destination object based on direction of the compare. server1[in] first server connection server2[in] second server connection object1 the first object in the compare in the form: (db.name) object2 the second object in the compare in the form: (db.name) options[in] a dictionary containing the options for the operation: (quiet, etc.) object_type[in] type of the objects to be compared (e.g., TABLE, PROCEDURE, etc.). Returns tuple - (bool - same db name?, list of transformation statements) """ try: m_obj1 = re.match(REGEXP_QUALIFIED_OBJ_NAME, object1) db1, name1 = m_obj1.groups() m_obj2 = re.match(REGEXP_QUALIFIED_OBJ_NAME, object2) db2, name2 = m_obj2.groups() except: raise UtilError("Invalid object name arguments for _get_transform" "(): %s, %s." % (object1, object2)) # If the second part of the object qualified name is None, then the format # is not 'db_name.obj_name' for object1 and therefore must treat it as a # database name. (supports backticks and the use of '.' (dots) in names.) if not name1 or object_type == 'DATABASE': # We are working with databases so db and name need to be set # to the database name to tell the get_object_definition() method # to retrieve the database information. name1 = db1 name2 = db2 db_1 = Database(server1, db1, options) db_2 = Database(server2, db2, options) obj1 = db_1.get_object_definition(db1, name1, object_type) obj2 = db_2.get_object_definition(db2, name2, object_type) # Get the transformation based on direction. transform_str = [] xform = SQLTransformer(db_1, db_2, obj1[0], obj2[0], object_type, options.get('verbosity', 0)) differences = xform.transform_definition() if differences and len(differences) > 0: transform_str.extend(differences) return transform_str
def _get_transform(server1, server2, object1, object2, options): """Get the transformation SQL statements This method generates the SQL statements to transform the destination object based on direction of the compare. server1[in] first server connection server2[in] second server connection object1 the first object in the compare in the form: (db.name) object2 the second object in the compare in the form: (db.name) options[in] a dictionary containing the options for the operation: (quiet, etc.) Returns tuple - (bool - same db name?, list of transformation statements) """ from mysql.utilities.common.database import Database from mysql.utilities.common.sql_transform import SQLTransformer obj_type = None direction = options.get("changes-for", "server1") # If there is no dot, we do not have the format 'db_name.obj_name' for # object1 and therefore must treat it as a database name. if object1.find('.') == -1: obj_type = "DATABASE" # We are working with databases so db and name need to be set # to the database name to tell the get_object_definition() method # to retrieve the database information. db1 = object1 db2 = object2 name1 = object1 name2 = object2 else: try: db1, name1 = object1.split('.') db2, name2 = object2.split('.') except: raise UtilError("Invalid object name arguments for _get_transform" "(): %s, %s." % (object1, object2)) db_1 = Database(server1, db1, options) db_2 = Database(server2, db2, options) if obj_type is None: obj_type = db_1.get_object_type(name1) transform_str = [] obj1 = db_1.get_object_definition(db1, name1, obj_type) obj2 = db_2.get_object_definition(db2, name2, obj_type) # Get the transformation based on direction. transform_str = [] same_db_name = True xform = SQLTransformer(db_1, db_2, obj1[0], obj2[0], obj_type, options.get('verbosity', 0)) differences = xform.transform_definition() if differences is not None and len(differences) > 0: transform_str.extend(differences) return transform_str
def _check_tables_structure(server1, server2, object1, object2, options, diff_type): """Check if the tables have the same structure. This method compares the tables structure ignoring the order of the columns and retrieves the differences between the table options. server1[in] first server connection. server2[in] second server connection. object1 the first object in the compare in the form: (db.name). object2 the second object in the compare in the form: (db.name). options[in] a dictionary containing the options for the operation: (quiet, verbosity, difftype, width, suppress_sql). diff_type[in] difference type. Returns a tuple (bool, list, bool) - The first tuple value is a boolean that indicates if both tables have the same structure (i.e. column definitions). The second returns the table options differences. Finally, the third is a boolean indicating if the partition options are the same. """ try: m_obj1 = re.match(REGEXP_QUALIFIED_OBJ_NAME, object1) db1, name1 = m_obj1.groups() m_obj2 = re.match(REGEXP_QUALIFIED_OBJ_NAME, object2) db2, name2 = m_obj2.groups() except: raise UtilError("Invalid object name arguments for diff_objects(): " "{0}, {1}.".format(object1, object2)) # If the second part of the object qualified name is None, then the format # is not 'db_name.obj_name' for object1 and therefore must treat it as a # database name. if not name1: return None, None, None db_1 = Database(server1, db1, options) db_2 = Database(server2, db2, options) # Get tables definitions. table_1 = db_1.get_object_definition(db1, name1, 'TABLE')[0] table_2 = db_2.get_object_definition(db2, name2, 'TABLE')[0] # Check table options. table1_opts = db_1.get_table_options(db1, name1) table2_opts = db_2.get_table_options(db2, name2) diff = _get_diff(table1_opts, table2_opts, object1, object2, diff_type) # Check if both tables have the same columns definition. # Discard column order. table_1_cols = [col[1:] for col in table_1[1]] table_2_cols = [col[1:] for col in table_2[1]] same_cols_def = set(table_1_cols) == set(table_2_cols) # Check if both tables have the same partition options. # Discard partition name. table_1_part = [part[1:] for part in table_1[2]] table_2_part = [part[1:] for part in table_2[2]] same_partition_opts = set(table_1_part) == set(table_2_part) # Return tables check results. return same_cols_def, diff, same_partition_opts