最近写了一个工具(比较两套测试环境mysql数据库中表、表字段、索引的差异,基于python)通过文章简单介绍下工具的相关内容

  1. 工具名称
  2. 主要功能
  3. 具体使用方法
  4. 部分实现代码
  5. 后续

一、工具名称:

dbstructsync (python库)

二、主要功能:

比较两套环境中mysql指定库中表、表字段及索引的差异,返回同步的sql ,里面包含建表,修改索引,修改字段的sql .

A环境的数据库db 作为sourcedb, B环境的数据库db targetdb ,程序逻辑比较的是 sourcedb 与targetdb 的差异,然后返回一个list的数据类型 

list中包含新建表sql,修改、增加字段sql, 删除、新增索引sql 

现在总共有3个版本,0.0.1 和0.0.2 存在一定的bug, 所以请使用最新的0.0.3版本

 

 

其他说明:由于是刚完成不久的程序,所以暂时不对最终结果sql进行执行,避免对使用过程中产生不好的影响,这个版本大家可以通过python 自行选择需要执行哪些操作;随着之后程序的逐步深入修改和演变,会将执行sql这一步也都加进去

同时也会优化使用方式,让使用这个工具的小伙伴更容易操作

三、具体使用方法:

1、 pip install  -i https://pypi.python.org/pypi  dbstructsync   

在代码里引入使用,

复制代码
from DbStructSync import cli   result=cli.db_sync(sourcedb, targetdb)  #sourcedb,targetdb是两个dict的参数,具体参数看下面 # 这里得到的    result = ['use 库;',  #             'CREATE TABLE `test_async` (\n  `test_async` #varchar(30) NOT NULL,\n  `aa` varchar(400) DEFAULT NULL,\n  #PRIMARY KEY (`test_async`)\n) ENGINE=InnoDB DEFAULT #CHARSET=utf8;', #             'drop index `index_chaxx` on chanxx_auto_puxx_conf;', #             'create index `index_chaxx` on #chanxx_auto_puxx_conf(`channel_nxx`,`channel_prxx`) USING #BTREE;'] #result 中包含  use 库; #              如果有少的表,会有 create table的数据; 如果有不同的索引,会#存在drop index 和create index的sql; #              如果有不同的字段,会有alter table的sql ; #只需要对这个结果,再通过pymysql的一些数据库操作就可以保证 sourcedb #的内容与taragetdb一致。
复制代码
 

2、同时还支持命令行操作,代码写入到x.py代码中

复制代码
result = cli.db_sync_commandline() python x.py --source  host=10.1.1.x,port=3306,user=root,passwd=root,db=investx --target host=10.1.1.x,port=3306,user=root,passwd=root,db=investx
复制代码

 

命令行中  --source  key=value;key2=value2;key3=value3  --target key=value;key2=value2;key3=value3

--source, --target 是两给必输的参数,后续的值会作为一个dict类型传入程序。 --source是源库的信息, --target是目标库的信息

还包括其他几个命令参数 --only-index , --only-fields ; --only-index 只比较索引差异, --only-fields 只比较字段差异, 非必填,默认都为False 

四、部分实现代码:

 

复制代码
def diff_tables(sourcetable, targettable):     '''      :param sourcetable:  源数据库的表名列表     :param targettable:  目的数据库的表名列表     :return: 返回dict,包含三种结果,源库多的表,目标库多的表,相同的表     '''     logger.info('开始比较两个库中表的差异,源库表{},目标库表{}'.format(sourcetable, targettable))     table_result={}     if not isinstance(sourcetable, list) or not isinstance(targettable, list):          raise  TypeError('sourcetable , targettable的类型不是list')     source_diff = set(sourcetable) - set(targettable)     target_diff = set(targettable) - set(sourcetable)     same_tables = set(sourcetable)& set(targettable)     table_result['source'] = source_diff     table_result['target'] = target_diff     table_result['same'] = same_tables     logger.info('两个库中表的差异结果{}'.format(table_result))     return  table_result    def  diff_indexs_fields(sourcesql, targetsql, type=1):     '''     :param sourcesql: 源数据库表的创建sql     :param targetsql: 目标数据表的创建sql     :return: 记录下索引不一致的地方     '''     result = {}     logger.info('解析语句中的索引字段,并进行比较索引')     sourcesql = parse_str(sourcesql)  # 从括号中提取需要的内容    #logger.info('从括号中提取出来的信息数据{}'.format(sourcesql))    sourcesql = lists2str(sourcesql)  #将list转换为str,并对数据的空格数据进行处理    logger.info('解析完的数据的信息{}'.format(sourcesql))     sourcesql = sourcesql.sp