定制 zitansmail/migration-orderer 二次开发

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

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

zitansmail/migration-orderer

Composer 安装命令:

composer require zitansmail/migration-orderer

包简介

Automatically order Laravel migrations based on foreign key dependencies to prevent constraint errors

README 文档

README

Laravel Package Tests PHP Version

Automatically analyze and reorder Laravel migrations based on foreign key dependencies. Prevent foreign key constraint errors by ensuring tables are created in the correct order.

composer require zitansmail/migration-orderer --dev

🎯 The Problem

Ever encountered this error when running migrations?

SQLSTATE[HY000]: General error: 1005 Can't create table `posts`
(errno: 150 "Foreign key constraint is incorrectly formed")

This happens when migrations create foreign keys to tables that don't exist yet. Traditional solutions require manually renaming migration files or creating migrations in perfect chronological order.

💡 The Solution

MigrationOrderer automatically:

  • Scans your migrations for foreign key dependencies
  • Builds a dependency graph using topological sorting
  • Reorders files to ensure dependencies come first
  • Shows you exactly what needs to be fixed

🚀 Features

  • 🔍 Smart Detection: Finds foreignId(), constrained(), foreignIdFor(), and legacy foreign keys
  • 🧩 Topological Sort: Uses graph algorithms to compute safe execution order
  • 👁️ Rich Preview: Shows current vs computed positions with dependency details
  • 🛡️ Circular Detection: Identifies and reports circular dependencies with clear error messages
  • 🔄 Safe Reordering: Renames files while maintaining full undo capability
  • 💾 State Management: Tracks changes in database for reliable undo operations
  • 📂 Flexible Paths: Works with custom migration directories and modular setups
  • Production Ready: Comprehensive test coverage and error handling

✅ Requirements

  • PHP 8.1+
  • Laravel 10.x / 11.x / 12.x

📦 Installation

composer require zitansmail/migration-orderer --dev

That's it! The package auto-registers and creates its tracking table automatically when needed. No manual migrations required.

🛠 Usage

1. Preview Dependencies (Safe, No Changes)

php artisan migrate:ordered --preview

Example Output:

+-------------+----------------------+-------------+---------------+------------------+------------------------+
| # (Computed)| Migration            | Current Pos | Status        | Dependencies     | Issue                  |
+-------------+----------------------+-------------+---------------+------------------+------------------------+
| 1           | create_users_table   | 2           | NEEDS REORDER | -                | -                      |
| 2           | create_posts_table   | 1           | NEEDS REORDER | create_users_... | Depends on: create_... |
+-------------+----------------------+-------------+---------------+------------------+------------------------+

⚠️  1 migration(s) need reordering for dependency safety.

2. Reorder Files

php artisan migrate:ordered --reorder

With confirmation bypass (CI/automation):

php artisan migrate:ordered --reorder --force

3. Undo Last Reorder

php artisan migrate:ordered --undo-last

4. Custom Migration Paths

php artisan migrate:ordered --preview --path=modules/Blog/database/migrations

🔍 Supported Foreign Key Patterns

The scanner detects all modern Laravel foreign key patterns:

// ✅ Modern foreignId with implicit constraint
$table->foreignId('user_id')->constrained();

// ✅ Modern foreignId with explicit table
$table->foreignId('author_id')->constrained('users');

// ✅ ForeignIdFor helper
$table->foreignIdFor(User::class);
$table->foreignIdFor(User::class, 'author_id');

// ✅ Legacy foreign key syntax
$table->foreign('user_id')->references('id')->on('users');

// ✅ Legacy unsigned big integer (partial detection)
$table->unsignedBigInteger('user_id');

// ⚠️ Polymorphic relationships (detected but no dependency)
$table->morphs('commentable');
$table->uuidMorphs('taggable');

🔄 Workflow Examples

Basic Workflow

# 1. Check current state
php artisan migrate:ordered --preview

# 2. Fix ordering if needed
php artisan migrate:ordered --reorder

# 3. Run migrations normally
php artisan migrate

# 4. Undo if something goes wrong
php artisan migrate:ordered --undo-last

Team Development

# Before merging a feature branch
git checkout feature/user-system
php artisan migrate:ordered --preview
php artisan migrate:ordered --reorder --force
git add database/migrations/
git commit -m "Fix migration dependency order"

🚨 Error Handling

Circular Dependencies

Migration Orderer Error: Circular dependency detected:
create_users_table.php -> create_roles_table.php -> create_users_table.php

Solution: Break the cycle by:

  • Moving foreign keys to separate migrations
  • Using nullable foreign keys initially
  • Deferring constraints with Schema::enableForeignKeyConstraints()

Missing Dependencies

The preview shows missing tables that your migrations reference but don't create:

Missing: ["external_api_users", "legacy_data"]

🧭 Command Reference

php artisan migrate:ordered [options]

Options:
  --preview         Show dependency analysis without making changes
  --reorder         Rename files to match computed order
  --undo-last       Restore files from last reorder operation
  --path=PATH       Custom migrations directory
  --force           Skip confirmation prompts

🔒 Safety Features

  • Preview First: Always shows what will change before making modifications
  • Atomic Operations: File renames are tracked; failures can be undone
  • State Persistence: Every reorder is logged in the database
  • Backup Strategy: Undo capability restores exact previous state
  • Non-Destructive: Default mode makes no changes to your files
  • Validation: Detects and prevents circular dependencies

🛡️ Best Practices

Development Workflow

  1. Always preview first to understand dependencies
  2. Commit before reordering for easy rollback
  3. Use feature branches for complex schema changes
  4. Test migrations in staging before production

Schema Design

  • Avoid circular foreign key dependencies
  • Consider nullable foreign keys for complex relationships
  • Use pivot tables for many-to-many relationships
  • Plan table creation order during initial design

Team Collaboration

  • Run --preview before pushing migration changes
  • Include reordering in your CI/CD pipeline
  • Document complex dependency relationships
  • Use consistent naming conventions

🧪 Testing

The package includes focused tests covering core functionality:

# Run the test suite
composer test

# With coverage
composer test-coverage

Essential test coverage:

  • Command Interface - Preview, reorder, undo operations
  • Dependency Detection - All foreign key patterns and missing dependencies
  • Circular Dependencies - Detection and error handling
  • File Operations - Safe reordering with automatic table creation

🤝 Contributing

We welcome contributions! Here's how you can help improve MigrationOrderer:

Quick Start

git clone https://github.com/zitansmail/migration-orderer
cd migration-orderer
composer install
composer test

Contributing Guidelines

🐛 Reporting Bugs

  • Check existing issues before creating new ones
  • Include Laravel version, PHP version, and error details
  • Provide minimal reproduction steps

✨ Suggesting Features

  • Open an issue with a clear description
  • Explain the use case and expected behavior
  • Include code examples if applicable

🔧 Code Contributions

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Write tests for new functionality
  4. Ensure all tests pass: composer test
  5. Follow PSR-12 coding standards
  6. Submit a pull request with clear description

📝 Documentation

  • Fix typos and improve clarity
  • Add examples for complex features
  • Update README when adding new functionality

Development Guidelines

  • Write tests for all new features
  • Keep backwards compatibility
  • Follow existing code patterns
  • Add meaningful commit messages

📝 License

MIT License. See LICENSE for details.

📚 Learn More

📖 Technical Deep Dive Read the complete story behind MigrationOrderer's development: Solving Laravel Migration Dependency Hell: Building MigrationOrderer Package

The blog post covers:

  • The problem and motivation behind the package
  • Technical implementation details and algorithms
  • Real-world usage patterns and team workflows
  • Development challenges and lessons learned
  • Future enhancements and roadmap

🙏 Credits

Created by Zitane Smail

Built with ❤️ for the Laravel community.

zitansmail/migration-orderer 适用场景与选型建议

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

它主要适用于以下技术方向: 「database」 「migrations」 「laravel」 「dependencies」 「topological-sort」 「foreign-keys」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 zitansmail/migration-orderer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-20