Пример #1
0
def object_diff(server1_val, server2_val, object1, object2, options,
                object_type=None):
    """diff the definition of two objects

    Find the difference among two object definitions.

    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)
    object_type[in]    type of the objects to be compared (e.g., TABLE,
                       PROCEDURE, etc.). By default None (not defined).

    Returns None = objects are the same, diff[] = tables differ
    """
    server1, server2 = server_connect(server1_val, server2_val,
                                      object1, object2, options)

    # Get the object type if unknown considering that objects of different
    # types can be found with the same name.
    if not object_type:
        #Get object types of object1
        regexp_obj = re.compile(REGEXP_QUALIFIED_OBJ_NAME)
        m_obj = regexp_obj.match(object1)
        db_name, obj_name = m_obj.groups()
        db = Database(server1, db_name, options)
        obj1_types = db.get_object_type(obj_name)
        if not obj1_types:
            raise UtilDBError("The object {0} does not exist.".format(object1))

        # Get object types of object2
        m_obj = regexp_obj.match(object2)
        db_name, obj_name = m_obj.groups()
        db = Database(server2, db_name, options)
        obj2_types = db.get_object_type(obj_name)
        if not obj2_types:
            raise UtilDBError("The object {0} does not exist.".format(object2))

        # Merge types found for both objects
        obj_types = set(obj1_types + obj2_types)

        # Diff objects considering all types found
        result = []
        for obj_type in obj_types:
            res = diff_objects(server1, server2, object1, object2, options,
                               obj_type)
            if res:
                result.append(res)
        return result if len(result) > 0 else None
    else:
        # Diff objects of known type
        return diff_objects(server1, server2, object1, object2, options,
                            object_type)
Пример #2
0
def object_diff(server1_val, server2_val, object1, object2, options):
    """diff the definition of two objects
    
    Find the difference among two object definitions.
    
    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)

    Returns None = objects are the same, diff[] = tables differ
    """
    from mysql.utilities.common.dbcompare import diff_objects, server_connect

    server1, server2 = server_connect(server1_val, server2_val, object1,
                                      object2, options)
    result = diff_objects(server1, server2, object1, object2, options)

    return result
Пример #3
0
def _check_databases(server1, server2, db1, db2, options):
    """Check databases

    server1[in]       first server Server instance
    server2[in]       second server Server instance
    db1[in]           first database
    db2[in]           second database
    options[in]       options dictionary

    Returns tuple - Database class instances for databases
    """

    # Check database create for differences
    if not options['no_diff']:
        # temporarily make the diff quiet to retrieve errors
        new_opt = {}
        new_opt.update(options)
        new_opt['quiet'] = True  # do not print messages
        new_opt['suppress_sql'] = True  # do not print SQL statements either
        res = diff_objects(server1, server2, db1, db2, new_opt, 'DATABASE')
        if res is not None:
            for row in res:
                print row
            print
            if not options['run_all_tests']:
                raise UtilError(_ERROR_DB_DIFF)
Пример #4
0
def _check_databases(server1, server2, db1, db2, options):
    """Check databases

    server1[in]       first server Server instance
    server2[in]       second server Server instance
    db1[in]           first database
    db2[in]           second database
    options[in]       options dictionary

    Returns tuple - Database class instances for databases
    """

    # Check database create for differences
    if not options['no_diff']:
        # temporarily make the diff quiet to retrieve errors
        new_opt = {}
        new_opt.update(options)
        new_opt['quiet'] = True          # do not print messages
        new_opt['suppress_sql'] = True   # do not print SQL statements either
        res = diff_objects(server1, server2, db1, db2, new_opt, 'DATABASE')
        if res is not None:
            for row in res:
                print row
            print
            if not options['run_all_tests']:
                raise UtilError(_ERROR_DB_DIFF)
Пример #5
0
def _compare_objects(server1, server2, obj1, obj2, reporter, options):
    """Compare object definitions and produce difference
    
    server1[in]       first server Server instance
    server2[in]       second server Server instance
    obj1[in]          first object
    obj2[in]          second object
    reporter[in]      database compare reporter class instance
    options[in]       options dictionary
    
    Returns list of errors
    """
    from mysql.utilities.common.dbcompare import diff_objects

    errors = []
    if not options['no_diff']:
        # For each database, compare objects
        # temporarily make the diff quiet to retrieve errors
        new_opt = {}
        new_opt.update(options)
        new_opt['quiet'] = True          # do not print messages
        new_opt['suppress_sql'] = True   # do not print SQL statements either
        res = diff_objects(server1, server2, obj1, obj2, new_opt)
        if res is not None:
            reporter.report_state('FAIL')
            errors.extend(res)
            if not options['run_all_tests']:
                raise UtilError(_ERROR_DB_DIFF)
        else:
            reporter.report_state('pass')
    else:
        reporter.report_state('SKIP')

    return errors
Пример #6
0
def object_diff(server1_val, server2_val, object1, object2, options):
    """diff the definition of two objects
    
    Find the difference among two object definitions.
    
    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)

    Returns None = objects are the same, diff[] = tables differ
    """
    from mysql.utilities.common.dbcompare import diff_objects, server_connect

    try:
        server1, server2 = server_connect(server1_val, server2_val,
                                      object1, object2, options)
    except:
        raise

    result = diff_objects(server1, server2, object1, object2, options)
    
    return result
Пример #7
0
def _compare_objects(server1, server2, obj1, obj2, reporter, options):
    """Compare object definitions and produce difference
    
    server1[in]       first server Server instance
    server2[in]       second server Server instance
    obj1[in]          first object
    obj2[in]          second object
    reporter[in]      database compare reporter class instance
    options[in]       options dictionary
    
    Returns list of errors
    """
    from mysql.utilities.common.dbcompare import diff_objects

    errors = []
    if not options['no_diff']:
        # For each database, compare objects
        # temporarily make the diff quiet to retrieve errors
        new_opt = {}
        new_opt.update(options)
        new_opt['quiet'] = True          # do not print messages
        new_opt['suppress_sql'] = True   # do not print SQL statements either
        res = diff_objects(server1, server2, obj1, obj2, new_opt)
        if res is not None:
            reporter.report_state('FAIL')
            errors.extend(res)
            if not options['run_all_tests']:
                raise UtilError(_ERROR_DB_DIFF)
        else:
            reporter.report_state('pass')
    else:
        reporter.report_state('SKIP')

    return errors
Пример #8
0
def _compare_objects(server1, server2, obj1, obj2, reporter, options,
                     object_type):
    """Compare object definitions and produce difference

    server1[in]       first server Server instance
    server2[in]       second server Server instance
    obj1[in]          first object
    obj2[in]          second object
    reporter[in]      database compare reporter class instance
    options[in]       options dictionary
    object_type[in]   type of the objects to be compared (e.g., TABLE,
                      PROCEDURE, etc.).

    Returns list of errors
    """

    errors = []
    if not options['no_diff']:
        # For each database, compare objects
        # temporarily make the diff quiet to retrieve errors
        new_opt = {}
        new_opt.update(options)
        new_opt['quiet'] = True  # do not print messages
        new_opt['suppress_sql'] = True  # do not print SQL statements either
        res = diff_objects(server1, server2, obj1, obj2, new_opt, object_type)
        if res is not None:
            reporter.report_state('FAIL')
            errors.extend(res)
            if not options['run_all_tests'] and \
                    not options.get('quiet', False):
                raise UtilError(_ERROR_DB_DIFF)
        else:
            reporter.report_state('pass')
    else:
        reporter.report_state('SKIP')

    return errors
Пример #9
0
def object_diff(server1_val,
                server2_val,
                object1,
                object2,
                options,
                object_type=None):
    """diff the definition of two objects

    Find the difference among two object definitions.

    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)
    object_type[in]    type of the objects to be compared (e.g., TABLE,
                       PROCEDURE, etc.). By default None (not defined).

    Returns None = objects are the same, diff[] = tables differ
    """
    server1, server2 = server_connect(server1_val, server2_val, object1,
                                      object2, options)

    # Get the object type if unknown considering that objects of different
    # types can be found with the same name.
    if not object_type:
        #Get object types of object1
        regexp_obj = re.compile(REGEXP_QUALIFIED_OBJ_NAME)
        m_obj = regexp_obj.match(object1)
        db_name, obj_name = m_obj.groups()
        db = Database(server1, db_name, options)
        obj1_types = db.get_object_type(obj_name)
        if not obj1_types:
            raise UtilDBError("The object {0} does not exist.".format(object1))

        # Get object types of object2
        m_obj = regexp_obj.match(object2)
        db_name, obj_name = m_obj.groups()
        db = Database(server2, db_name, options)
        obj2_types = db.get_object_type(obj_name)
        if not obj2_types:
            raise UtilDBError("The object {0} does not exist.".format(object2))

        # Merge types found for both objects
        obj_types = set(obj1_types + obj2_types)

        # Diff objects considering all types found
        result = []
        for obj_type in obj_types:
            res = diff_objects(server1, server2, object1, object2, options,
                               obj_type)
            if res:
                result.append(res)
        return result if len(result) > 0 else None
    else:
        # Diff objects of known type
        return diff_objects(server1, server2, object1, object2, options,
                            object_type)
Пример #10
0
def object_diff(server1_val, server2_val, object1, object2, options,
                object_type=None):
    """diff the definition of two objects

    Find the difference among two object definitions.

    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)
    object_type[in]    type of the objects to be compared (e.g., TABLE,
                       PROCEDURE, etc.). By default None (not defined).

    Returns None = objects are the same, diff[] = tables differ
    """
    server1, server2 = server_connect(server1_val, server2_val,
                                      object1, object2, options)

    force = options.get("force", None)
    # Get the object type if unknown considering that objects of different
    # types can be found with the same name.
    if not object_type:
        # Get object types of object1
        sql_mode = server1.select_variable("SQL_MODE")
        db_name, obj_name = parse_object_name(object1, sql_mode)
        db = Database(server1, db_name, options)
        obj1_types = db.get_object_type(obj_name)
        if not obj1_types:
            msg = "The object {0} does not exist.".format(object1)
            if not force:
                raise UtilDBError(msg)
            print("ERROR: {0}".format(msg))
            return []

        # Get object types of object2
        sql_mode = server2.select_variable("SQL_MODE")
        db_name, obj_name = parse_object_name(object2, sql_mode)
        db = Database(server2, db_name, options)
        obj2_types = db.get_object_type(obj_name)
        if not obj2_types:
            msg = "The object {0} does not exist.".format(object2)
            if not force:
                raise UtilDBError(msg)
            print("ERROR: {0}".format(msg))
            return []

        # Merge types found for both objects
        obj_types = set(obj1_types + obj2_types)

        # Diff objects considering all types found
        result = []
        for obj_type in obj_types:
            res = diff_objects(server1, server2, object1, object2, options,
                               obj_type)
            if res:
                result.append(res)
        return result if len(result) > 0 else None
    else:
        # Diff objects of known type
        return diff_objects(server1, server2, object1, object2, options,
                            object_type)
Пример #11
0
def object_diff(server1_val,
                server2_val,
                object1,
                object2,
                options,
                object_type=None):
    """diff the definition of two objects

    Find the difference among two object definitions.

    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)
    object_type[in]    type of the objects to be compared (e.g., TABLE,
                       PROCEDURE, etc.). By default None (not defined).

    Returns None = objects are the same, diff[] = tables differ
    """
    if isinstance(server1_val, dict):  # dict or common.server.Server object
        server1, server2 = server_connect(server1_val, server2_val, object1,
                                          object2, options)
    else:
        # to save connection
        server1, server2 = server1_val, server2_val

    force = options.get("force", None)

    # compare db's all objects
    include_create = options.get("include_create", False)
    # db1.*:db2.*
    if include_create and object1.endswith('.*') and object2.endswith('.*'):
        direction = options.get("changes-for", None)
        reverse = options.get("reverse", False)

        db_name1, _ = parse_object_name(object1,
                                        server1.select_variable("SQL_MODE"))
        db_name2, _ = parse_object_name(object2,
                                        server2.select_variable("SQL_MODE"))
        in_both, in_db1, in_db2 = get_common_objects(server1, server2,
                                                     db_name1, db_name2, True,
                                                     options)
        # create/alter/drop need all objects compare
        all_object = set(in_both + in_db1 + in_db2)

        # call myself recusively to compare all objects
        for this_obj in all_object:
            object1 = db_name1 + "." + this_obj[1][0]
            object2 = db_name2 + "." + this_obj[1][0]
            # share the same connection in this loop. object_type=None
            object_diff(server1,
                        server2,
                        object1,
                        object2,
                        options,
                        object_type=None)
        return []

    # Get the object type if unknown considering that objects of different
    # types can be found with the same name.
    if not object_type:
        # Get object types of object1
        sql_mode = server1.select_variable("SQL_MODE")
        db_name, obj_name = parse_object_name(object1, sql_mode)
        db = Database(server1, db_name, options)
        obj1_types = db.get_object_type(obj_name)
        if not obj1_types:
            if include_create:
                # if allow generating create object ddl, give 'NULL' object here to tell common.dbcompare.py to handle
                obj1_types = ['NULL']
            else:
                msg = "The object {0} does not exist.".format(object1)
                if not force:
                    raise UtilDBError(msg)
                print("ERROR: {0}".format(msg))
                return []

        # Get object types of object2
        sql_mode = server2.select_variable("SQL_MODE")
        db_name, obj_name = parse_object_name(object2, sql_mode)
        db = Database(server2, db_name, options)
        obj2_types = db.get_object_type(obj_name)
        if not obj2_types:
            if include_create:
                obj2_types = ['NULL']
            else:
                msg = "The object {0} does not exist.".format(object2)
                if not force:
                    raise UtilDBError(msg)
                print("ERROR: {0}".format(msg))
                return []

        # Merge types found for both objects
        obj_types = set(obj1_types + obj2_types)
        if obj_types == set(['NULL']):
            msg = "The object {0} or {1} does not exist in the source side.".format(
                object1, object2)
            if not force:
                raise UtilDBError(msg)
            print("ERROR: {0}".format(msg))
            return []
        elif 'NULL' in obj_types:
            # at least one object exist in db1 and db2
            # new db object like  TABLE-NULL or NULL-TABLE , 'TABLE' is needed for after use in diff_objects()
            obj_types = set(['-'.join(obj1_types + obj2_types)])

        # Diff objects considering all types found
        result = []
        for obj_type in obj_types:
            res = diff_objects(server1, server2, object1, object2, options,
                               obj_type)
            if res:
                result.append(res)
        return result if len(result) > 0 else None
    else:
        # Diff objects of known type
        return diff_objects(server1, server2, object1, object2, options,
                            object_type)
Пример #12
0
def object_diff(server1_val,
                server2_val,
                object1,
                object2,
                options,
                object_type=None):
    """diff the definition of two objects

    Find the difference among two object definitions.

    server1_val[in]    a dictionary containing connection information for the
                       first server including:
                       (user, password, host, port, socket)
    server2_val[in]    a dictionary containing connection information for the
                       second server including:
                       (user, password, host, port, socket)
    object1[in]        the first object in the compare in the form: (db.name)
    object2[in]        the second object in the compare in the form: (db.name)
    options[in]        a dictionary containing the options for the operation:
                       (quiet, verbosity, difftype)
    object_type[in]    type of the objects to be compared (e.g., TABLE,
                       PROCEDURE, etc.). By default None (not defined).

    Returns None = objects are the same, diff[] = tables differ
    """
    objectype = options.get("objectype", 'ALL').upper()
    if not object_type and objectype != 'ALL':
        object_type = objectype
    if object_type and objectype != 'ALL' and object_type != objectype:
        print('The object type {} is skip'.format(object_type))
        return None
    server1, server2 = server_connect(server1_val, server2_val, object1,
                                      object2, options)

    force = options.get("force", None)
    # Get the object type if unknown considering that objects of different
    # types can be found with the same name.
    result = []
    if not object_type:
        # Get object types of object1
        sql_mode = server1.select_variable("SQL_MODE")
        db_name, obj_name = parse_object_name(object1, sql_mode)
        db = Database(server1, db_name, options)
        obj1_types = db.get_object_type(obj_name)
        if not obj1_types:
            msg = "The object {0} does not exist.".format(object1)
            if not force:
                raise UtilDBError(msg)
            print("ERROR: {0}".format(msg))
            return []

        # Get object types of object2
        sql_mode = server2.select_variable("SQL_MODE")
        db_name, obj_name = parse_object_name(object2, sql_mode)
        db = Database(server2, db_name, options)
        obj2_types = db.get_object_type(obj_name)
        if not obj2_types:
            msg = "The object {0} does not exist.".format(object2)
            if not force:
                raise UtilDBError(msg)
            print("ERROR: {0}".format(msg))
            return []

        # Merge types found for both objects
        obj_types = set(obj1_types + obj2_types)

        # Diff objects considering all types found
        for obj_type in obj_types:
            res = diff_objects(server1, server2, object1, object2, options,
                               obj_type)
            if res:
                result.append(res)
    else:
        # Diff objects of known type
        res = diff_objects(server1, server2, object1, object2, options,
                           object_type)
        if res:
            result.append(res)
    if len(result) > 0 and options.get(
            "difftype", None) == 'sql' and options.get(
                "output", None) and options.get("output", '').endswith('.sql'):
        with open(options.get("output"), 'a', encoding='utf8') as fp:
            for res in result:
                if isinstance(res, list):
                    for r in res:
                        if r and r.strip().startswith('#'):
                            continue
                        fp.write('{}\n'.format(r))
                else:
                    fp.write('{}\n'.format(res))
    return result if len(result) > 0 else None