定制 mathsgod/mysql-schema-migrate 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mathsgod/mysql-schema-migrate

Composer 安装命令:

composer require mathsgod/mysql-schema-migrate

包简介

Export MySQL database schema to JSON format

README 文档

README

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

License: MIT PHP Version

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

  • PHP >= 8.1
  • PDO extension
  • PDO MySQL extension
  • JSON extension

Installation

Via Composer

composer require mathsgod/mysql-schema-migrate

Global Installation

composer global require mathsgod/mysql-schema-migrate

Usage

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

Option Short Description Default
--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 path stdout

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

Option Short Description Default
--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

  • 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

<?php

use MysqlSchemaMigrate\Exporter;
use MysqlSchemaMigrate\Importer;

// Export schema
$exporter = new Exporter(
    host: 'localhost',
    database: 'my_database',
    username: 'root',
    password: 'password',
    port: 3306,
    charset: 'utf8mb4'
);

// Get schema as array
$schema = $exporter->export();

// Get schema as JSON string
$json = $exporter->toJson();

// Save to file
file_put_contents('schema.json', $json);

// Import schema
$importer = new Importer(
    host: 'localhost',
    database: 'target_database',
    username: 'root',
    password: 'password',
    port: 3306,
    charset: 'utf8mb4'
);

// Import from file (dry-run)
$statements = $importer->importFromFile(
    filePath: 'schema.json',
    dryRun: true,
    allowDrop: false,
    keepDefiner: false,
    only: [] // empty = all types
);

// Get formatted SQL for review
$sql = $importer->formatSqlForFile();
file_put_contents('migration.sql', $sql);

// Import and execute
$statements = $importer->importFromFile(
    filePath: 'schema.json',
    dryRun: false,
    allowDrop: true
);

Output Format

The exported JSON contains the following structure:

{
    "tables": [
        {
            "name": "users",
            "columns": [
                {
                    "name": "id",
                    "type": "int",
                    "nullable": false,
                    "default": null,
                    "auto_increment": true,
                    "unsigned": true
                },
                {
                    "name": "email",
                    "type": "varchar",
                    "nullable": false,
                    "default": null,
                    "auto_increment": false,
                    "length": 255,
                    "charset": "utf8mb4",
                    "collation": "utf8mb4_unicode_ci"
                }
            ],
            "primary_key": ["id"],
            "unique_keys": [
                {
                    "name": "users_email_unique",
                    "columns": ["email"]
                }
            ],
            "foreign_keys": [],
            "indexes": [],
            "engine": "InnoDB",
            "charset": "utf8mb4",
            "collation": "utf8mb4_unicode_ci",
            "comment": null
        }
    ],
    "views": [],
    "triggers": [],
    "functions": [],
    "procedures": [],
    "events": []
}

License

MIT License. See LICENSE for more information.

Author

Raymond Chong (mathsgod@yahoo.com)

mathsgod/mysql-schema-migrate 适用场景与选型建议

mathsgod/mysql-schema-migrate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mathsgod/mysql-schema-migrate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mathsgod/mysql-schema-migrate 我们能提供哪些服务?
定制开发 / 二次开发

基于 mathsgod/mysql-schema-migrate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 124
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-05