PHPackages                             mathsgod/mysql-schema-migrate - 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. mathsgod/mysql-schema-migrate

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

mathsgod/mysql-schema-migrate
=============================

Export MySQL database schema to JSON format

1.0.1(2mo ago)0401MITPHPPHP &gt;=8.1

Since Dec 5Pushed 2mo agoCompare

[ Source](https://github.com/mathsgod/mysql-schema-migrate)[ Packagist](https://packagist.org/packages/mathsgod/mysql-schema-migrate)[ RSS](/packages/mathsgod-mysql-schema-migrate/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (1)

MySQL Schema Migrate
====================

[](#mysql-schema-migrate)

Export and import MySQL database schema to/from JSON format.

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)[![PHP Version](https://camo.githubusercontent.com/88b464e5614cf654f181925115d47b523dc429fcfe41d59565e42e757f306f29/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344382e312d3838393242462e737667)](https://www.php.net/)

Features
--------

[](#features)

- Export complete MySQL database schema to JSON format
- Import schema from JSON with intelligent diff detection
- Supports:
    - Tables (with columns, primary keys, unique keys, foreign keys, indexes)
    - Views
    - Triggers
    - Functions
    - Procedures
    - Events
- Smart schema diff: uses `ALTER TABLE` for existing tables instead of recreating
- Dry-run mode to preview SQL without executing
- CLI tool with easy-to-use options
- Can be used as a PHP library

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- PDO extension
- PDO MySQL extension
- JSON extension

Installation
------------

[](#installation)

### Via Composer

[](#via-composer)

```
composer require mathsgod/mysql-schema-migrate
```

### Global Installation

[](#global-installation)

```
composer global require mathsgod/mysql-schema-migrate
```

Usage
-----

[](#usage)

### Export Command

[](#export-command)

```
# Basic usage
./vendor/bin/mysql-schema-migrate export -u username -d database_name

# With password
./vendor/bin/mysql-schema-migrate export -u username -p password -d database_name

# Specify host and port
./vendor/bin/mysql-schema-migrate export -H localhost -P 3306 -u username -p password -d database_name

# Output to file
./vendor/bin/mysql-schema-migrate export -u username -d database_name -o schema.json

# With custom charset
./vendor/bin/mysql-schema-migrate export -u username -d database_name -c utf8mb4
```

#### Export Options

[](#export-options)

OptionShortDescriptionDefault`--host``-H`MySQL host`localhost``--port``-P`MySQL port`3306``--database``-d`Database name(required)`--username``-u`MySQL username(required)`--password``-p`MySQL password(empty)`--charset``-c`Connection charset`utf8mb4``--output``-o`Output file pathstdout### Import Command

[](#import-command)

```
# Basic import
./vendor/bin/mysql-schema-migrate import -u username -d database_name schema.json

# Dry-run mode (preview SQL without executing)
./vendor/bin/mysql-schema-migrate import --dry-run -u username -d database_name schema.json

# Dry-run with SQL output to file
./vendor/bin/mysql-schema-migrate import --dry-run -o output.sql -u username -d database_name schema.json

# Allow dropping columns/tables/objects
./vendor/bin/mysql-schema-migrate import --allow-drop -u username -d database_name schema.json

# Keep original DEFINER (default: reset to CURRENT_USER)
./vendor/bin/mysql-schema-migrate import --keep-definer -u username -d database_name schema.json

# Import only specific object types
./vendor/bin/mysql-schema-migrate import --only=tables,views -u username -d database_name schema.json
```

#### Import Options

[](#import-options)

OptionShortDescriptionDefault`--host``-H`MySQL host`localhost``--port``-P`MySQL port`3306``--database``-d`Database name(required)`--username``-u`MySQL username(required)`--password``-p`MySQL password(empty)`--charset``-c`Connection charset`utf8mb4``--dry-run`Only generate SQL without executing`false``--output-file``-o`Output SQL to file (use with --dry-run)`--allow-drop`Allow dropping columns, tables, and objects`false``--keep-definer`Keep original DEFINER`false``--only`Only process specific types (comma-separated)all#### Import Behavior

[](#import-behavior)

- **Tables**: Uses `ALTER TABLE` for existing tables (ADD/MODIFY/DROP COLUMN, index changes)
- **Views**: Uses `CREATE OR REPLACE VIEW`
- **Functions/Procedures/Triggers/Events**: Uses `DROP IF EXISTS` + `CREATE`
- **DEFINER**: Reset to `CURRENT_USER` by default (use `--keep-definer` to preserve)
- **Foreign Keys**: Handled separately to avoid dependency issues

### As a Library

[](#as-a-library)

```
