GithubHelp home page GithubHelp logo

yyd2 / mysqldiff Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zhoukang99/mysqldiff

0.0 0.0 0.0 2.22 MB

一款轻量级数据库对比工具,同时支持新增表的默认数据导入!——在实际使用中持续完善

License: Apache License 2.0

Python 100.00%

mysqldiff's Introduction

  mysqldiff是一个能够方便于大家在实际项目中快速生成不同版本数据库之间的差异SQL,同时还能够自动将新版中新增表中默认数据一并导入到旧版本中。

1. 主要功能

mysql虽然提供了mysqldiff工具,但实际使用中还有些不满足要求的地方,且在Linux下一般都带有python环境,所以用python写了一个。

  • 比对两个数据库的差异;
  • 比对两张表的差异;
  • 支持外键的处理;
  • 自动忽略表字段的顺序差异;
  • 支持表或列的重命名;
  • 支持同步新增表中数据;
  • 支持旧表中的数据导入到新增表中;
  • 将生成的差异sql保存到文件或者自动执行;
  • 记录错误日志,出错时方便排查。

2. 使用方法

  使用时可以直接下载源码来执行mysqldiff.py,或者通过pip命令安装:

pip install mysqldiff.py

命令格式:

 python mysqldiff.py [param1] [param2] { [param3]....}

参数说明:

  •  -x
    自动执行差异SQL语句,默认不执行。
  •  -c
    是否插入新增表中的默认数据
  •  s=[user]:[pass]@[host]:[port]
    当两个数据库的连接配置相同时可以用该参数,否则用下面的sn和so分别指明。
        user:用户名
        pass:密码
        host:ip地址
        port:端口
  •  so=[user]:[pass]@[host]:[port]
    数据库配置。
  •  sn=[user]:[pass]@[host]:[port]
    被参照的数据库配置。
  •  [db_new]{.[table_name]}:[db_old]{.[table_name]}
    数据库名.表名,不指名表名时对比整个数据库。
  •  file=[diff_file]
    差异化sql保存位置,默认保存在diff.sql文件中。

示例:
对比db_new和db_old两个数据库中的表结构差异,并将sql语句保存到diff.sql中,同时直接执行sql语句:

python mysqldiff.py -x s=comclay:[email protected]:3306 db_new:db_old file=diff.sql

3. map.config映射关系配置

map.config文件中包含重命名和数据导入的配置
重命名配置用->表示:

# 重命名
# oldtable -> newtable
# oldtable.oldcol -> newtable.newcol
# oldtable.[oldcol1, oldcol12, oldcol3 ......] -> newtable.[newcol1, newcol2, newcol3 ......]

course_old -> course
user.name -> user_new.username

数据导入配置用=>表示:

# 数据导入
# oldtable => newtable
# oldtable.[oldcol1, oldcol12, oldcol3 ......] => newtable.[newcol1, newcol2, newcol3 ......]

course_old.[id, user_id] => user_course.[course_id, user_id]

4. 数据库对比

db_old数据库中只包含user表:

db_new数据库中新增了course,并添加了一个外键约束:


使用下方命令进行差异化对比:

python mysqldiff.py -c s=comclay:[email protected]:3306 db_new:db_old

生成的diff.sql如下:

/****************************** 关闭外键约束 ******************************/
SET FOREIGN_KEY_CHECKS=0;

/****************************** course ******************************/
CREATE TABLE `course` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `course` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
  `grade` int(11) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `fk` (`user_id`),
  CONSTRAINT `fk` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO course (grade, course, user_id, id) VALUES 
	(60, 'english', 1, 1);

/****************************** 开启外键约束 ******************************/
SET FOREIGN_KEY_CHECKS=1;

5. 联系方式

微信:

mysqldiff's People

Contributors

zhoukang99 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.