9raxdev/mysql-struct-sync

There is no license information available for the latest version (1.0.0) of this package.

可用于帮助开发人员比较两个数据库之间的差异(表,列,约束,事件,函数,存储过程,触发器,视图),并生成更新语句。Can be used to synchronize database structures, compare differences(table,column,constraints,events,functions,procedures,triggers,views) between databases and generating queries.

1.0.0 2019-06-14 04:51 UTC

README

可用于帮助开发人员比较两个数据库之间的差异(表,列,约束,事件,函数,存储过程,触发器,视图),并生成更新语句。 Can be used to synchronize database structures, compare differences(table,column,constraints,events,functions,procedures,triggers,views) between databases and generating queries.

TODOs 功能

  • Handle Create Tables
  • Handle Alter Tables
  • Handle Drop table Queries
  • Handle constraints (PK,FK,index, ... etc)
  • Handle events
  • Handle functions
  • Handle procedures
  • Handle triggers
  • Handle views

Installation 安装方式

to install this library using composer:

composer require 9raxdev/mysql-struct-sync

Attention 注意

1.无法识别rename字段,更改数据库字段名称在代码中的体现为:先删除命名前的字段,再增加命名后的字段。The rename field is not recognized. Changing the database field name in the code is as follows: first delete the field before the name, and then add the named field.

2.advanceDiff()必须基于baseDiff()前提下,因为储存过程,触发器,函数等特性都依赖数据表。advanceDiff() must be based on baseDiff(), because procedures, triggers, functions, etc. depend on the tables.

Demo

<?php

include __DIR__.'/MysqlStructSync.php'; 

function p($arr)
{
    echo '<pre>' . print_r($arr, true) . '</pre>';
}

$local_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'test'];

$develop_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'sakila'];

//把local数据库结构更新为develop数据库结构
$compare=new \linge\MysqlStructSync($local_database_config,$develop_database_config);

$compare->removeAutoIncrement();

$compare->baseDiff(); //TABLE COLUMNS(ADD,DROP,MODIFY) CONSTRAINTS(PK,FK,index, ... etc)

$compare->advanceDiff(); //VIEW TRIGGER EVENT FUNCTION PROCEDURE (ADD,DROP)

$diff_sql=$compare->getDiffSql();
//p($diff_sql);

/*******************************************/
//用法一:自动执行全部差异语句,更新结构
//$execute_sql_stat=$compare->execute();
//p($execute_sql_stat);


//用法二:手动选择要执行的差异语句,记住:选择储存过程,函数等请确保数据库表已经同步
$compare->manuallySelectUpdates();

manuallySelectUpdates