示例#1
0
def compare(trans_id, source_sid, source_did, source_scid,
            target_sid, target_did, target_scid):
    """
    This function will compare the two schemas.
    """
    # Check the transaction and connection status
    status, error_msg, diff_model_obj, session_obj = \
        check_transaction_status(trans_id)

    if error_msg == gettext('Transaction ID not found in the session.'):
        return make_json_response(success=0, errormsg=error_msg, status=404)

    # Server version compatibility check
    status, msg = check_version_compatibility(source_sid, target_sid)

    if not status:
        return make_json_response(success=0, errormsg=msg, status=428)

    comparison_result = []

    diff_model_obj.set_comparison_info(gettext("Comparing objects..."), 0)
    update_session_diff_transaction(trans_id, session_obj,
                                    diff_model_obj)

    try:
        all_registered_nodes = SchemaDiffRegistry.get_registered_nodes()
        node_percent = round(100 / len(all_registered_nodes))
        total_percent = 0

        for node_name, node_view in all_registered_nodes.items():
            view = SchemaDiffRegistry.get_node_view(node_name)
            if hasattr(view, 'compare'):
                msg = gettext('Comparing {0}').\
                    format(gettext(view.blueprint.COLLECTION_LABEL))
                diff_model_obj.set_comparison_info(msg, total_percent)
                # Update the message and total percentage in session object
                update_session_diff_transaction(trans_id, session_obj,
                                                diff_model_obj)

                res = view.compare(source_sid=source_sid,
                                   source_did=source_did,
                                   source_scid=source_scid,
                                   target_sid=target_sid,
                                   target_did=target_did,
                                   target_scid=target_scid)

                if res is not None:
                    comparison_result = comparison_result + res
            total_percent = total_percent + node_percent

        msg = gettext("Successfully compare the specified schemas.")
        total_percent = 100
        diff_model_obj.set_comparison_info(msg, total_percent)
        # Update the message and total percentage done in session object
        update_session_diff_transaction(trans_id, session_obj, diff_model_obj)

    except Exception as e:
        app.logger.exception(e)

    return make_json_response(data=comparison_result)
示例#2
0
文件: __init__.py 项目: wilj/pgadmin4
def compare_schema_objects(**kwargs):
    """
    This function is used to compare the specified schema and their children.

    :param kwargs:
    :return:
    """
    trans_id = kwargs.get('trans_id')
    session_obj = kwargs.get('session_obj')
    source_sid = kwargs.get('source_sid')
    source_did = kwargs.get('source_did')
    source_scid = kwargs.get('source_scid')
    target_sid = kwargs.get('target_sid')
    target_did = kwargs.get('target_did')
    target_scid = kwargs.get('target_scid')
    schema_name = kwargs.get('schema_name')
    diff_model_obj = kwargs.get('diff_model_obj')
    total_percent = kwargs.get('total_percent')
    node_percent = kwargs.get('node_percent')
    ignore_whitespaces = kwargs.get('ignore_whitespaces')
    is_schema_source_only = kwargs.get('is_schema_source_only', False)
    source_schema_name = None
    if is_schema_source_only:
        driver = get_driver(PG_DEFAULT_DRIVER)
        source_schema_name = driver.qtIdent(None, schema_name)

    comparison_result = []

    all_registered_nodes = SchemaDiffRegistry.get_registered_nodes()
    for node_name, node_view in all_registered_nodes.items():
        view = SchemaDiffRegistry.get_node_view(node_name)
        if hasattr(view, 'compare'):
            msg = gettext('Comparing {0} of schema \'{1}\''). \
                format(gettext(view.blueprint.collection_label),
                       gettext(schema_name))
            diff_model_obj.set_comparison_info(msg, total_percent)
            # Update the message and total percentage in session object
            update_session_diff_transaction(trans_id, session_obj,
                                            diff_model_obj)

            res = view.compare(source_sid=source_sid,
                               source_did=source_did,
                               source_scid=source_scid,
                               target_sid=target_sid,
                               target_did=target_did,
                               target_scid=target_scid,
                               group_name=gettext(schema_name),
                               ignore_whitespaces=ignore_whitespaces,
                               source_schema_name=source_schema_name)

            if res is not None:
                comparison_result = comparison_result + res
        total_percent = total_percent + node_percent
        # if total_percent is more then 100 then set it to less then 100
        if total_percent >= 100:
            total_percent = 96

    return comparison_result, total_percent
示例#3
0
def compare_schema(trans_id, source_sid, source_did, source_scid, target_sid,
                   target_did, target_scid, ignore_owner, ignore_whitespaces):
    """
    This function will compare the two schema.
    """
    # Check the pre validation before compare
    status, error_msg, diff_model_obj, session_obj = \
        compare_pre_validation(trans_id, source_sid, target_sid)
    if not status:
        return error_msg

    comparison_result = []

    diff_model_obj.set_comparison_info(COMPARE_MSG, 0)
    update_session_diff_transaction(trans_id, session_obj, diff_model_obj)
    try:
        ignore_owner = bool(ignore_owner)
        ignore_whitespaces = bool(ignore_whitespaces)
        all_registered_nodes = SchemaDiffRegistry.get_registered_nodes()
        node_percent = round(100 / len(all_registered_nodes))
        total_percent = 0

        comparison_schema_result, total_percent = \
            compare_schema_objects(
                trans_id=trans_id, session_obj=session_obj,
                source_sid=source_sid, source_did=source_did,
                source_scid=source_scid, target_sid=target_sid,
                target_did=target_did, target_scid=target_scid,
                schema_name=gettext('Schema Objects'),
                diff_model_obj=diff_model_obj,
                total_percent=total_percent,
                node_percent=node_percent,
                ignore_owner=ignore_owner,
                ignore_whitespaces=ignore_whitespaces)

        comparison_result = \
            comparison_result + comparison_schema_result

        msg = gettext("Successfully compare the specified schemas.")
        total_percent = 100
        diff_model_obj.set_comparison_info(msg, total_percent)
        # Update the message and total percentage done in session object
        update_session_diff_transaction(trans_id, session_obj, diff_model_obj)

    except Exception as e:
        app.logger.exception(e)

    return make_json_response(data=comparison_result)
示例#4
0
def compare_database_objects(**kwargs):
    """
    This function is used to compare the specified schema and their children.

    :param kwargs:
    :return:
    """
    trans_id = kwargs.get('trans_id')
    session_obj = kwargs.get('session_obj')
    source_sid = kwargs.get('source_sid')
    source_did = kwargs.get('source_did')
    target_sid = kwargs.get('target_sid')
    target_did = kwargs.get('target_did')
    diff_model_obj = kwargs.get('diff_model_obj')
    total_percent = kwargs.get('total_percent')
    node_percent = kwargs.get('node_percent')
    ignore_owner = kwargs.get('ignore_owner')
    ignore_whitespaces = kwargs.get('ignore_whitespaces')
    comparison_result = []

    all_registered_nodes = SchemaDiffRegistry.get_registered_nodes(
        None, 'Database')
    for node_name, node_view in all_registered_nodes.items():
        view = SchemaDiffRegistry.get_node_view(node_name)
        if hasattr(view, 'compare'):
            msg = gettext('Comparing {0}'). \
                format(gettext(view.blueprint.collection_label))
            app.logger.debug(msg)
            diff_model_obj.set_comparison_info(msg, total_percent)
            # Update the message and total percentage in session object
            update_session_diff_transaction(trans_id, session_obj,
                                            diff_model_obj)

            res = view.compare(source_sid=source_sid,
                               source_did=source_did,
                               target_sid=target_sid,
                               target_did=target_did,
                               group_name=gettext('Database Objects'),
                               ignore_owner=ignore_owner,
                               ignore_whitespaces=ignore_whitespaces)

            if res is not None:
                comparison_result = comparison_result + res
        total_percent = total_percent + node_percent

    return comparison_result, total_percent
示例#5
0
文件: __init__.py 项目: yo-1/pgadmin4
def compare(trans_id, source_sid, source_did, target_sid, target_did):
    """
    This function will compare the two schemas.
    """
    # Check the transaction and connection status
    status, error_msg, diff_model_obj, session_obj = \
        check_transaction_status(trans_id)

    if error_msg == ERROR_MSG_TRANS_ID_NOT_FOUND:
        return make_json_response(success=0, errormsg=error_msg, status=404)

    # Server version compatibility check
    status, msg = check_version_compatibility(source_sid, target_sid)

    if not status:
        return make_json_response(success=0, errormsg=msg, status=428)

    comparison_result = []

    diff_model_obj.set_comparison_info(gettext("Comparing objects..."), 0)
    update_session_diff_transaction(trans_id, session_obj,
                                    diff_model_obj)

    try:
        # Fetch all the schemas of source and target database
        # Compare them and get the status.
        schema_result = fetch_compare_schemas(source_sid, source_did,
                                              target_sid, target_did)

        total_schema = len(schema_result['source_only']) + len(
            schema_result['target_only']) + len(
            schema_result['in_both_database'])

        node_percent = 0
        if total_schema > 0:
            node_percent = round(100 / (total_schema * len(
                SchemaDiffRegistry.get_registered_nodes())))
        total_percent = 0

        # Compare Database objects
        comparison_schema_result, total_percent = \
            compare_database_objects(
                trans_id=trans_id, session_obj=session_obj,
                source_sid=source_sid, source_did=source_did,
                target_sid=target_sid, target_did=target_did,
                diff_model_obj=diff_model_obj, total_percent=total_percent,
                node_percent=node_percent)
        comparison_result = \
            comparison_result + comparison_schema_result

        # Compare Schema objects
        if 'source_only' in schema_result and \
                len(schema_result['source_only']) > 0:
            for item in schema_result['source_only']:
                comparison_schema_result, total_percent = \
                    compare_schema_objects(
                        trans_id=trans_id, session_obj=session_obj,
                        source_sid=source_sid, source_did=source_did,
                        source_scid=item['scid'], target_sid=target_sid,
                        target_did=target_did, target_scid=None,
                        schema_name=item['schema_name'],
                        diff_model_obj=diff_model_obj,
                        total_percent=total_percent,
                        node_percent=node_percent,
                        is_schema_source_only=True)

                comparison_result = \
                    comparison_result + comparison_schema_result

        if 'target_only' in schema_result and \
                len(schema_result['target_only']) > 0:
            for item in schema_result['target_only']:
                comparison_schema_result, total_percent = \
                    compare_schema_objects(
                        trans_id=trans_id, session_obj=session_obj,
                        source_sid=source_sid, source_did=source_did,
                        source_scid=None, target_sid=target_sid,
                        target_did=target_did, target_scid=item['scid'],
                        schema_name=item['schema_name'],
                        diff_model_obj=diff_model_obj,
                        total_percent=total_percent,
                        node_percent=node_percent)

                comparison_result = \
                    comparison_result + comparison_schema_result

        # Compare the two schema present in both the databases
        if 'in_both_database' in schema_result and \
                len(schema_result['in_both_database']) > 0:
            for item in schema_result['in_both_database']:
                comparison_schema_result, total_percent = \
                    compare_schema_objects(
                        trans_id=trans_id, session_obj=session_obj,
                        source_sid=source_sid, source_did=source_did,
                        source_scid=item['src_scid'], target_sid=target_sid,
                        target_did=target_did, target_scid=item['tar_scid'],
                        schema_name=item['schema_name'],
                        diff_model_obj=diff_model_obj,
                        total_percent=total_percent,
                        node_percent=node_percent)

                comparison_result = \
                    comparison_result + comparison_schema_result

        msg = gettext("Successfully compare the specified databases.")
        total_percent = 100
        diff_model_obj.set_comparison_info(msg, total_percent)
        # Update the message and total percentage done in session object
        update_session_diff_transaction(trans_id, session_obj, diff_model_obj)

    except Exception as e:
        app.logger.exception(e)

    return make_json_response(data=comparison_result)
示例#6
0
def compare_database(trans_id, source_sid, source_did, target_sid, target_did,
                     ignore_owner, ignore_whitespaces):
    """
    This function will compare the two databases.
    """
    # Check the pre validation before compare
    status, error_msg, diff_model_obj, session_obj = \
        compare_pre_validation(trans_id, source_sid, target_sid)
    if not status:
        return error_msg

    comparison_result = []

    diff_model_obj.set_comparison_info(COMPARE_MSG, 0)
    update_session_diff_transaction(trans_id, session_obj, diff_model_obj)

    try:
        ignore_owner = bool(ignore_owner)
        ignore_whitespaces = bool(ignore_whitespaces)

        # Fetch all the schemas of source and target database
        # Compare them and get the status.
        schema_result = fetch_compare_schemas(source_sid, source_did,
                                              target_sid, target_did)

        total_schema = len(schema_result['source_only']) + len(
            schema_result['target_only']) + len(
                schema_result['in_both_database'])

        node_percent = 0
        if total_schema > 0:
            node_percent = round(
                100 / (total_schema *
                       len(SchemaDiffRegistry.get_registered_nodes())))
        total_percent = 0

        # Compare Database objects
        comparison_schema_result, total_percent = \
            compare_database_objects(
                trans_id=trans_id, session_obj=session_obj,
                source_sid=source_sid, source_did=source_did,
                target_sid=target_sid, target_did=target_did,
                diff_model_obj=diff_model_obj, total_percent=total_percent,
                node_percent=node_percent,
                ignore_owner=ignore_owner,
                ignore_whitespaces=ignore_whitespaces)
        comparison_result = \
            comparison_result + comparison_schema_result

        # Compare Schema objects
        if 'source_only' in schema_result and \
                len(schema_result['source_only']) > 0:
            for item in schema_result['source_only']:
                comparison_schema_result, total_percent = \
                    compare_schema_objects(
                        trans_id=trans_id, session_obj=session_obj,
                        source_sid=source_sid, source_did=source_did,
                        source_scid=item['scid'], target_sid=target_sid,
                        target_did=target_did, target_scid=None,
                        schema_name=item['schema_name'],
                        diff_model_obj=diff_model_obj,
                        total_percent=total_percent,
                        node_percent=node_percent,
                        is_schema_source_only=True,
                        ignore_owner=ignore_owner,
                        ignore_whitespaces=ignore_whitespaces)

                comparison_result = \
                    comparison_result + comparison_schema_result

        if 'target_only' in schema_result and \
                len(schema_result['target_only']) > 0:
            for item in schema_result['target_only']:
                comparison_schema_result, total_percent = \
                    compare_schema_objects(
                        trans_id=trans_id, session_obj=session_obj,
                        source_sid=source_sid, source_did=source_did,
                        source_scid=None, target_sid=target_sid,
                        target_did=target_did, target_scid=item['scid'],
                        schema_name=item['schema_name'],
                        diff_model_obj=diff_model_obj,
                        total_percent=total_percent,
                        node_percent=node_percent,
                        ignore_owner=ignore_owner,
                        ignore_whitespaces=ignore_whitespaces)

                comparison_result = \
                    comparison_result + comparison_schema_result

        # Compare the two schema present in both the databases
        if 'in_both_database' in schema_result and \
                len(schema_result['in_both_database']) > 0:
            for item in schema_result['in_both_database']:
                comparison_schema_result, total_percent = \
                    compare_schema_objects(
                        trans_id=trans_id, session_obj=session_obj,
                        source_sid=source_sid, source_did=source_did,
                        source_scid=item['src_scid'], target_sid=target_sid,
                        target_did=target_did, target_scid=item['tar_scid'],
                        schema_name=item['schema_name'],
                        diff_model_obj=diff_model_obj,
                        total_percent=total_percent,
                        node_percent=node_percent,
                        ignore_owner=ignore_owner,
                        ignore_whitespaces=ignore_whitespaces)

                comparison_result = \
                    comparison_result + comparison_schema_result

        msg = gettext("Successfully compare the specified databases.")
        total_percent = 100
        diff_model_obj.set_comparison_info(msg, total_percent)
        # Update the message and total percentage done in session object
        update_session_diff_transaction(trans_id, session_obj, diff_model_obj)

    except Exception as e:
        app.logger.exception(e)

    return make_json_response(data=comparison_result)