PHPackages                             9raxdev/mysql-struct-sync - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. 9raxdev/mysql-struct-sync

ActiveLibrary[Database &amp; ORM](/categories/database)

9raxdev/mysql-struct-sync
=========================

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

1.0.3(7mo ago)221176PHPPHP &gt;=5.4.0

Since Jun 14Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/DDZH-DEV/mysql-struct-sync)[ Packagist](https://packagist.org/packages/9raxdev/mysql-struct-sync)[ RSS](/packages/9raxdev-mysql-struct-sync/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

mysql-struct-sync
=================

[](#mysql-struct-sync)

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

### Features

[](#features)

- 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 安装方式

[](#installation-安装方式)

to install this library using composer:

```
composer require ddzh-dev/mysql-struct-sync
```

### New Features in V2

[](#new-features-in-v2)

This library has been significantly updated to support a more robust and efficient workflow for comparing database schemas, especially for database version management systems.

- **Offline Snapshot Comparison**: Instead of comparing two live databases, you can now compare a live database against a pre-generated "structure snapshot". This eliminates the need for temporary databases and makes comparisons much faster.
- **Atomic `ALTER` Statements**: The library now generates a single, atomic `ALTER TABLE` statement for all modifications to a single table. This prevents errors related to operation order (e.g., dropping a primary key before modifying an auto-increment column) and ensures safer migrations.

### New API Methods

[](#new-api-methods)

- `MysqlStructSync::fetchStructureArray(array $db_conf)`: A static method to fetch the schema of a database as a PHP array (a "snapshot").
- `$compare->getGroupedDiffSql()`: Returns an array of SQL statements, with all `ALTER` queries for the same table grouped into a single query. This is the **recommended** way to get the difference script.

### Attention 注意

[](#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

[](#demo)

#### Standard Usage: Comparing Two Live Databases

[](#standard-usage-comparing-two-live-databases)

```
