示例#1
0
 def test_schema_sync_generate_args2cmd(self):
     args = {
         "sync-auto-inc":
         True,
         "sync-comments":
         True,
         "tag":
         'tag_v',
         "output-directory":
         '',
         "source":
         r"mysql://{user}:{pwd}@{host}:{port}/{database}".format(
             user='******',
             pwd='123456',
             host='127.0.0.1',
             port=3306,
             database='*'),
         "target":
         r"mysql://{user}:{pwd}@{host}:{port}/{database}".format(
             user='******',
             pwd='123456',
             host='127.0.0.1',
             port=3306,
             database='*')
     }
     self.sys_config.set('schemasync',
                         '/opt/venv4schemasync/bin/schemasync')
     self.sys_config.get_all_config()
     schema_sync = SchemaSync()
     cmd_args = schema_sync.generate_args2cmd(args, False)
     self.assertIsInstance(cmd_args, list)
     cmd_args = schema_sync.generate_args2cmd(args, True)
     self.assertIsInstance(cmd_args, str)
示例#2
0
def schemasync(request):
    """对比实例schema信息"""
    instance_name = request.POST.get('instance_name')
    db_name = request.POST.get('db_name')
    target_instance_name = request.POST.get('target_instance_name')
    target_db_name = request.POST.get('target_db_name')
    sync_auto_inc = True if request.POST.get(
        'sync_auto_inc') == 'true' else False
    sync_comments = True if request.POST.get(
        'sync_comments') == 'true' else False
    result = {
        'status': 0,
        'msg': 'ok',
        'data': {
            'diff_stdout': '',
            'patch_stdout': '',
            'revert_stdout': ''
        }
    }

    # 循环对比全部数据库
    if db_name == 'all' or target_db_name == 'all':
        db_name = '*'
        target_db_name = '*'

    # 取出该实例的连接方式
    instance_info = Instance.objects.get(instance_name=instance_name)
    target_instance_info = Instance.objects.get(
        instance_name=target_instance_name)

    # 检查SchemaSync程序路径
    path = SysConfig().get('schemasync')
    if path is None:
        result['status'] = 1
        result['msg'] = '请配置SchemaSync路径!'
        return HttpResponse(json.dumps(result),
                            content_type='application/json')

    # 提交给SchemaSync获取对比结果
    schema_sync = SchemaSync()
    # 准备参数
    tag = int(time.time())
    output_directory = os.path.join(settings.BASE_DIR, 'downloads/schemasync/')
    args = {
        "sync-auto-inc":
        sync_auto_inc,
        "sync-comments":
        sync_comments,
        "tag":
        tag,
        "output-directory":
        output_directory,
        "source":
        r"mysql://{user}:'{pwd}'@{host}:{port}/{database}".format(
            user=instance_info.user,
            pwd=instance_info.raw_password,
            host=instance_info.host,
            port=instance_info.port,
            database=db_name),
        "target":
        r"mysql://{user}:'{pwd}'@{host}:{port}/{database}".format(
            user=target_instance_info.user,
            pwd=target_instance_info.raw_password,
            host=target_instance_info.host,
            port=target_instance_info.port,
            database=target_db_name)
    }
    # 参数检查
    args_check_result = schema_sync.check_args(args)
    if args_check_result['status'] == 1:
        return HttpResponse(json.dumps(args_check_result),
                            content_type='application/json')
    # 参数转换
    cmd_args = schema_sync.generate_args2cmd(args, shell=True)
    # 执行命令
    try:
        stdout, stderr = schema_sync.execute_cmd(cmd_args,
                                                 shell=True).communicate()
        diff_stdout = f'{stdout}{stderr}'
    except RuntimeError as e:
        diff_stdout = str(e)

    # 非全部数据库对比可以读取对比结果并在前端展示
    if db_name != '*':
        date = time.strftime("%Y%m%d", time.localtime())
        patch_sql_file = '%s%s_%s.%s.patch.sql' % (output_directory,
                                                   target_db_name, tag, date)
        revert_sql_file = '%s%s_%s.%s.revert.sql' % (output_directory,
                                                     target_db_name, tag, date)
        try:
            with open(patch_sql_file, 'r') as f:
                patch_sql = f.read()
        except FileNotFoundError as e:
            patch_sql = str(e)
        try:
            with open(revert_sql_file, 'r') as f:
                revert_sql = f.read()
        except FileNotFoundError as e:
            revert_sql = str(e)
        result['data'] = {
            'diff_stdout': diff_stdout,
            'patch_stdout': patch_sql,
            'revert_stdout': revert_sql
        }
    else:
        result['data'] = {
            'diff_stdout': diff_stdout,
            'patch_stdout': '',
            'revert_stdout': ''
        }

    return HttpResponse(json.dumps(result), content_type='application/json')