dbstructsync 多套mysql環境表、欄位、索引的差異sql產出(原創)
最近寫了一個工具(比較兩套測試環境資料庫表、表字段、索引的差異)
功能:可以比較兩套環境中mysql指定庫中表、表字段及索引的差異,返回具體需要同步的執行sql
A環境的資料庫db 作為sourcedb, B環境的資料庫db targetdb ,程式碼比較的是 sourcedb 與targetdb 的差異,執行完sql後,保證了sourcedb 包含於targetdb
結果包括新建表sql,修改、增加欄位sql, 刪除、新增索引sql
具體使用方法:
pip install -i https://pypi.python.org/pypi dbstructsync
在程式碼裡引入使用, from DbStructSync import cli
# result=cli.db_sync(sourcedb, targetdb) ,sourcedb,targetdb是兩個dict的引數,具體引數看下面
# sourcedb = {'host':'10.1.1.31','port':33306,'user':'roo1','passwd':'roo2','db':'investment'},
# targetdb = {'host': '10.1.1.32', 'port': 33306, 'user': 'roo1', 'passwd': 'roo2', 'db': 'investment'}
# )
result是一個list,包含sourcedb 與targetdb中不一致的地方,需要將這些語句在targetdb中執行,從而保證兩個環境中的結構一致
同時還支援 cli.db_sync_commandline 操作,程式碼寫入到aa.py程式碼中
result = cli.db_sync_commandline()
python aa.py --source host=10.1.1.32,port=33306,user=root,passwd=root,db=investment --target host=10.1.1.37,port=33306,user=root,passwd=root,db=investment
python x.py --only-index --only-fields --source xx --target xx 其中 --source , --target是必須的引數
[--only-index] [--only-fields] 只對索引比較,只對欄位比較的配置,最終結果也只包含對應的配置項內容
&n