zitansmail/migration-orderer

Order Laravel migrations before execution

dev-main 2025-09-20 18:07 UTC

This package is auto-updated.

Last update: 2025-09-20 18:07:41 UTC


README

Laravel Package Topological Sorting

Automatically orders Laravel migration files based on database dependency relationships to eliminate foreign key constraint errors

composer require zitansmail/migration-orderer

🚀 Key Features

  • 🔍 Dependency Scanning - Detects foreign keys across migration files
  • 🧠 Graph Processing - Builds dependency graph with topological sorting
  • 🔄 File Renaming - Rewrites timestamps to enforce execution order (--reorder)
  • 📋 Database Sync - Updates migrations table when files change
  • ⚙️ Migration Execution - Runs migrations in resolved order (--run)
  • 👁️ Dry Run Previews - Simulate changes without modifications (--dry-run)
  • 📊 JSON Output - Machine-readable dependency graph (--json)
  • 📂 Custom Paths - Processes non-standard directories (--path=)

📦 Installation

  1. Install via Composer:
composer require zitansmail/migration-orderer
  1. If Laravel doesn't auto-discover the package, register the service provider in config/app.php:
'providers' => [
    // Other service providers...
    MigrationOrderer\MigrationOrdererServiceProvider::class,
],

🛠 Usage Examples

Preview migration order (no changes)

php artisan migrate:ordered --dry-run

Generate dependency graph (JSON format)

php artisan migrate:ordered --dry-run --json

Process migrations in custom directory

php artisan migrate:ordered --path=database/migrations/v2 --dry-run

Reorder & rename migrations

php artisan migrate:ordered --reorder

Execute migrations in resolved order

php artisan migrate:ordered --run

⚠️ Best Practices

  1. Always preview with --dry-run before executing changes
  2. Commit your work before using --reorder or --run
  3. Test thoroughly after reordering migrations
  4. Document custom paths for team members
  5. Resolve circular dependencies before processing
  6. Avoid production use - Recommended for development only

📝 License

MIT License - https://opensource.org/licenses/MIT

⚙️ Contributing

PRs welcome! See Contribution Guide for:

  • Bug reports
  • Feature requests
  • Code style requirements
  • Testing guidelines

Pro Tip: Combine with Laravel's migration commands:

php artisan migrate:ordered --reorder
php artisan migrate:fresh --seed
php artisan migrate:refresh --seed

MigrationOrderer is not affiliated with The Laravel Framework. Laravel is a trademark of Taylor Otwell.