sufyandev/laravel-migration-linter

A lightweight Laravel package that analyzes and warns about risky database migration changes before they reach production.

Installs: 414

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 0

Forks: 1

Open Issues: 0

pkg:composer/sufyandev/laravel-migration-linter

2.1.0 2025-12-24 11:26 UTC

README

Laravel Migration Linter report

Docs Migration Linter Latest Version on Packagist Total Downloads Laravel Version PHP Version Version License

A lightweight Laravel package that analyzes your database migrations and warns you about risky schema changes β€” before they reach production.

πŸš€ Features

  • πŸ” Detects dangerous migration patterns:
    • Non-nullable columns without defaults
    • Missing indexes on foreign keys
    • Unsafe column drops
    • Risky unique constraints
    • Floating-point money fields
    • Unsafe column renaming (table locks)
    • Column type changes on large tables (table locks)
    • Soft deletes on large tables
  • βš™οΈ Configurable rule severities (info, warning, error)
  • πŸ’‘ Actionable suggestions β€” Each warning includes fix recommendations with grouped display
  • πŸ”— Documentation links β€” Every suggestion has a link to detailed docs
  • 🧠 Baseline support to ignore legacy issues
  • 🧾 JSON or table output for CI/CD (with suggestions included)
  • πŸ“Š HTML Reports β€” Generate beautiful, interactive HTML reports with charts and filtering
  • 🎨 Clean output β€” Suggestions grouped by rule type to avoid repetition
  • 🧩 Fully documented & tested (84 tests, 200 assertions)

πŸ“˜ Read full rule docs:
πŸ‘‰ https://muhammad-sufyan5.github.io/sufyan-laravel-migration-lint-package/

πŸ“¦ Installation

Install via Composer:

composer require sufyandev/laravel-migration-linter --dev

The package will auto-register via Laravel’s package discovery.

🧩 Usage

Run the built-in Artisan command to lint all migration files:

php artisan migrate:lint

Common Options

Option Description
--json Output structured JSON (great for CI)
--html= Generate interactive HTML report (default: storage/app/...)
--path= Lint a specific migration file/folder
--baseline Use a custom baseline file
--generate-baseline Create a baseline to ignore current issues
--rules View all rules and their enabled status
--summary Display summary footer in output
--no-suggestions Hide suggestions (show only warnings table)

Example Usage

Lint all migrations and export JSON:

php artisan migrate:lint --json > storage/lint-report.json

Generate a new baseline file (ignore current issues):

php artisan migrate:lint --generate-baseline

Sample warning output:

[warning] AddUniqueConstraintOnNonEmptyColumn
β†’ Adding unique constraint to 'email' may fail if duplicates exist in 'users'.

πŸ“‹ Scope & Limitations

What We Analyze

βœ… Laravel Schema Builder Operations β€” All $table-> method calls
βœ… Schema::create() and Schema::table() methods
βœ… Column modifications via ->change()
βœ… Foreign keys, indexes, constraints, timestamps

What We Don't Analyze (By Design)

⚠️ Raw SQL queries (DB::statement(), DB::raw(), etc.)
⚠️ Direct Eloquent operations (User::update(), model factories)
⚠️ Model traits and properties
⚠️ Data seeding operations

Reason: The linter focuses on statically analyzing schema builder patterns, which represent 99% of migration files. Raw SQL analysis requires different tooling.

βš™οΈ Configuration

You can publish the configuration file to customize rule settings:

php artisan vendor:publish --tag="migration-linter-config"

The config file (config/migration-linter.php) allows you to:

  • Set severity threshold (info, warning, error)
  • Enable/disable specific rules
  • Customize rule severity levels

Example configuration:

return [
    'severity_threshold' => 'warning',
    'rules' => [
        'AddNonNullableColumnWithoutDefault' => ['enabled' => true, 'severity' => 'warning'],
        'MissingIndexOnForeignKey'           => ['enabled' => true, 'severity' => 'warning'],
        // ... other rules
    ],
];

🧾 CI/CD Integration (GitHub Actions)

name: Laravel Migration Linter
on: [push, pull_request]
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: 8.3
      - run: composer install --no-interaction
      - run: php artisan migrate:lint --json > lint-report.json

✨ What's New in v2.0.0

Complete SOLID Principles Refactoring

  • πŸ—οΈ 8 Core Interfaces β€” Dependency injection contracts for all components
  • πŸ”§ 3 Service Classes β€” Reusable business logic layer (LaravelConfigProvider, SeverityResolver, LintService)
  • 🎨 5 Formatter Classes β€” Modular output system (Table, JSON, Compact, Summary, Base)
  • πŸ”Œ Dependency Injection β€” Automatic container bindings via service provider
  • πŸ“Š Enhanced Architecture β€” SOLID principles throughout (SRP, OCP, LSP, ISP, DIP)
  • βœ… 144 tests passing β€” Comprehensive test coverage with 259 assertions
  • πŸ”„ 100% backward compatible β€” No breaking changes, all v1.4.0 commands work identically
  • 🎯 Better table formatting β€” Fixed Symfony Table component for proper alignment

Migration from v1.4.0: All commands work exactly the same. No changes needed:

php artisan migrate:lint                  # Still works
php artisan migrate:lint --json           # Still works
php artisan migrate:lint --generate-baseline  # Still works

✨ What Was New in v1.4.0

  • Suggestions System β€” Each warning includes actionable fix recommendations
  • Documentation Links β€” Every issue links to detailed docs for the rule
  • SoftDeletesOnProduction β€” New rule for detecting unsafe soft deletes on large tables
  • Enhanced Reporter β€” JSON and table output include suggestions
  • 100% test coverage with 43 passing tests

πŸ§‘β€πŸ’» Contributing

Contributions are welcome! If you have an idea for a useful rule or enhancement, feel free to open a PR or issue.

🧾 License

Released under the MIT License. Β© 2025 Sufyan. All rights reserved.

🧠 Author

Muhammad Sufyan πŸ“§ muhammadsufyanwebdeveloper@gmail.com πŸ™ GitHub: @muhammad-sufyan5

β€œSmart developers don’t debug production β€” they lint migrations.”

πŸ“˜ Full Documentation:
πŸ‘‰ https://muhammad-sufyan5.github.io/sufyan-laravel-migration-lint-package/