Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Migrations Generator Laravel Package

kitloong/laravel-migrations-generator

Generate Laravel migration files from an existing database, including columns, indexes, and foreign keys. Supports MariaDB, MySQL, PostgreSQL, SQL Server, and SQLite. Run artisan migrate:generate to scaffold migrations for all or selected tables.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require --dev kitloong/laravel-migrations-generator
    

    Laravel auto-registers the service provider; Lumen requires manual registration in bootstrap/app.php.

  2. First Use Case: Generate migrations for all tables in your default database:

    php artisan migrate:generate
    

    This creates individual migration files for each table in database/migrations/, including foreign keys.

  3. Key Files to Review:

    • config/database.php (ensure correct connection settings)
    • Generated migrations in database/migrations/ (verify structure and logic)
    • Official Documentation (for advanced options)

Implementation Patterns

Core Workflow

  1. Database Inspection:

    • Run php artisan migrate:generate to scan the database schema.
    • The tool extracts tables, columns, indexes, and foreign keys, then generates corresponding Laravel migrations.
  2. Selective Generation:

    • Target specific tables:
      php artisan migrate:generate --tables="users,posts"
      
    • Exclude tables:
      php artisan migrate:generate --ignore="logs,temp_data"
      
  3. Multi-Database Projects:

    • Use --connection for non-default databases:
      php artisan migrate:generate --connection="secondary"
      
    • Migrations will include Schema::connection('secondary') calls.
  4. Custom Paths:

    • Output to a custom directory (e.g., for feature branches):
      php artisan migrate:generate --path="database/migrations/feature_x"
      
  5. Squashed Migrations:

    • Combine all migrations into a single file for cleaner history:
      php artisan migrate:generate --squash
      
    • Useful for legacy databases or large schemas.
  6. Foreign Key Handling:

    • Foreign keys are generated in a separate migration file (e.g., add_foreign_keys_to_users_table.php).
    • Ensure referenced tables are included in --tables to avoid errors.
  7. Views and Stored Procedures:

    • Generate views/stored procedures with:
      php artisan migrate:generate --skip-views=false --skip-proc=false
      
    • Exclude them with --skip-views or --skip-proc.
  8. Date Control:

    • Set a custom timestamp for migrations (e.g., for backfilling):
      php artisan migrate:generate --date="2024-01-01 00:00:00"
      
    • Views/foreign keys use +1 second to maintain order.

Integration Tips

  1. CI/CD Pipelines:

    • Use --skip-log to avoid logging migrations in automated environments:
      php artisan migrate:generate --skip-log
      
    • Combine with --with-has-table to safely generate migrations in shared environments:
      php artisan migrate:generate --with-has-table
      
  2. Team Collaboration:

    • Standardize naming with --default-index-names and --default-fk-names to avoid DB-specific names:
      php artisan migrate:generate --default-index-names --default-fk-names
      
    • Use --use-db-collation to preserve database collations:
      php artisan migrate:generate --use-db-collation
      
  3. Testing:

    • Generate migrations for test databases:
      php artisan migrate:generate --connection="testing"
      
    • Use --path="tests/migrations" to isolate test migrations.
  4. Legacy Systems:

    • For large schemas, use --squash and manually refactor complex migrations.
    • Audit generated foreign keys to ensure logical relationships.
  5. Custom Templates:

    • Override default templates (e.g., for custom column types or annotations):
      php artisan migrate:generate --template-path="custom/templates"
      
    • Example: Add soft deletes or timestamps automatically.

Gotchas and Tips

Pitfalls

  1. Circular Dependencies:

    • Foreign keys may fail if tables are generated in the wrong order.
    • Fix: Use --squash or manually order migrations.
  2. Reserved Keywords:

    • Database column names like order, group, or user may conflict with PHP/Laravel.
    • Fix: Use --default-index-names and manually rename columns in generated migrations.
  3. Connection Mismatches:

    • Running against the wrong database (e.g., mysql instead of pgsql).
    • Fix: Double-check --connection and verify config/database.php.
  4. Schema Differences:

    • Generated migrations may not match Laravel conventions (e.g., bigIncrements vs. increments).
    • Fix: Post-generate cleanup or use --default-index-names for consistency.
  5. Views/Procedures:

    • Views or stored procedures may not generate correctly if the database lacks metadata.
    • Fix: Use --skip-views or --skip-proc if unsupported.
  6. Timestamp Collisions:

    • Multiple runs with --date may cause duplicate timestamps.
    • Fix: Use --log-with-batch to offset timestamps:
      php artisan migrate:generate --log-with-batch=0
      
  7. Vendor Migrations:

    • Package migrations (e.g., Laravel UI) may be regenerated accidentally.
    • Fix: Use --skip-vendor to exclude them.

Debugging Tips

  1. Dry Runs:

    • Inspect generated SQL without writing files:
      php artisan migrate:generate --path="tmp/"  # Check `tmp/` for output
      
  2. Partial Generation:

    • Generate only foreign keys for debugging:
      php artisan migrate:generate --tables="users" --path="tmp/" --ignore="*" --fk-filename="debug_fks.php"
      
  3. Log Inspection:

    • Check migrations table for existing entries:
      SELECT * FROM migrations WHERE batch = 0;
      
    • Use --skip-log to avoid conflicts during testing.
  4. Template Overrides:

    • Debug custom templates by inspecting the --template-path directory.
    • Example: Add dd() to a template to see generated column data.
  5. Database-Specific Quirks:

    • PostgreSQL: Use --use-db-collation for citext or custom collations.
    • SQL Server: Escape reserved words with [] in generated migrations.

Extension Points

  1. Custom Column Types:

    • Extend the generator by overriding the Column class (e.g., for jsonb or uuid).
    • Example: Add a JsonColumn class in app/Extensions/JsonColumn.php.
  2. Pre/Post-Generation Hooks:

    • Use Laravel events to modify migrations before/after generation:
      // In a service provider
      event(new GeneratingMigrations);
      event(new MigrationsGenerated);
      
    • Listen in app/Providers/EventServiceProvider.php.
  3. Dynamic Table Selection:

    • Filter tables dynamically via a custom command:
      $tables = DB::connection('mysql')->select('SHOW TABLES');
      $this->option('tables', implode(',', $tables));
      
  4. Schema Modifiers:

    • Add a trait to the Blueprint class to auto-add soft deletes or timestamps:
      trait AutoTimestamps {
          public function __construct() {
              $this->timestamps();
          }
      }
      
  5. Multi-Schema Support:

    • Generate migrations for multiple schemas (e.g., public, app in PostgreSQL):
      php artisan migrate:generate --connection="pgsql" --tables="public.users,app.settings"
      

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport