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 schema, including columns, indexes, and foreign keys. Works with MariaDB/MySQL, PostgreSQL, SQL Server, and SQLite. Generate all tables or target/ignore specific tables via Artisan.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  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 columns, indexes, and foreign keys.

  3. Where to Look First:

    • Official Documentation
    • php artisan help migrate:generate for CLI options.
    • Default output: database/migrations/[datetime]_create_[table]_table.php.

Implementation Patterns

Core Workflow

  1. Generate All Tables:

    php artisan migrate:generate
    
    • Creates one migration file per table (e.g., 2024_01_01_000000_create_users_table.php).
    • Handles columns, indexes, and constraints.
  2. Selective Generation:

    php artisan migrate:generate --tables="users,posts" --ignore="temp_tables"
    
    • Explicitly include/exclude tables. Useful for large databases or partial schema migrations.
  3. Squashed Migrations:

    php artisan migrate:generate --squash
    
    • Combines all tables into a single migration file (e.g., 2024_01_01_000000_create_all_tables.php).
    • Ideal for legacy databases or CI/CD pipelines where minimal files are preferred.
  4. Custom Paths:

    php artisan migrate:generate --path="database/migrations/custom"
    
    • Output migrations to a non-default directory (e.g., for feature-specific schemas).

Integration Tips

  1. Foreign Key Handling:

    • Ensure referenced tables are generated before their foreign keys. The generator creates foreign key migrations in a separate file (e.g., add_foreign_keys_to_users_table.php).
    • Use --skip-foreign-keys to generate tables first, then manually add constraints later.
  2. Multi-Database Projects:

    php artisan migrate:generate --connection="secondary"
    
    • Generate migrations for a specific database connection (e.g., PostgreSQL for analytics, MySQL for primary).
  3. Date Control:

    php artisan migrate:generate --date="2024-01-01 00:00:00"
    
    • Set a custom timestamp for migrations (useful for backdating or batch processing).
  4. Schema Checks:

    php artisan migrate:generate --with-has-table
    
    • Adds Schema::hasTable() checks to migrations, preventing errors in environments where tables may already exist.
  5. Template Customization:

    • Override default templates by specifying --template-path="path/to/custom/templates".
    • Example: Modify column type mappings (e.g., map DATETIME to timestamp instead of dateTime).
  6. CI/CD Pipelines:

    • Use --skip-log to avoid logging migrations to the migrations table (useful for generated schemas).
    • Combine with --squash for atomic migration files:
      php artisan migrate:generate --squash --skip-log
      

Gotchas and Tips

Pitfalls

  1. Foreign Key Dependencies:

    • Issue: Generating a table without its parent table (referenced by a foreign key) will fail.
    • Fix: Use --tables="parent,child" or generate all tables first, then filter.
  2. Reserved Keywords:

    • Issue: Database column names like order, group, or user may conflict with PHP/Laravel keywords.
    • Fix: Use --default-index-names or --default-fk-names to avoid DB-specific names in migrations.
  3. Collation Mismatches:

    • Issue: Generated migrations may not preserve database collation (e.g., utf8mb4_unicode_ci).
    • Fix: Use --use-db-collation to include collation in migrations.
  4. Views and Stored Procedures:

    • Issue: Views/procedures may not generate correctly if the database schema is complex.
    • Fix: Skip them initially (--skip-views --skip-proc) and generate separately if needed.
  5. Vendor Migrations:

    • Issue: Package migrations (e.g., Laravel Breeze) may be regenerated accidentally.
    • Fix: Use --skip-vendor or exclude vendor tables (e.g., --ignore="migration_locks").

Debugging

  1. Dry Run:

    • Inspect generated SQL without creating files:
      php artisan migrate:generate --path="/tmp"  # Output to temp dir
      
    • Review /tmp/[datetime]_*.php for errors.
  2. Connection Issues:

    • Verify the connection name in config/database.php matches the --connection flag.
    • Test connectivity manually:
      php artisan tinker
      >>> \DB::connection('secondary')->select('SHOW TABLES');
      
  3. Template Errors:

    • Custom templates must extend the base generator. Check for missing methods like getTableBlueprint().
    • Debug by enabling verbose output:
      php artisan migrate:generate -vvv
      

Extension Points

  1. Custom Column Types:

    • Override column type mappings in app/Providers/MigrationsGeneratorServiceProvider.php:
      $generator->columnTypes = [
          'DATETIME' => 'timestamp',
          'TINYINT(1)' => 'boolean',
      ];
      
  2. Post-Generation Hooks:

    • Use Laravel’s registerMigrationsGenerator in AppServiceProvider to modify migrations after generation:
      $generator->afterGenerate(function ($migration) {
          $migration->prepend("// Custom header\n");
      });
      
  3. Batch Processing:

    • Split large schemas into batches using --log-with-batch=0 to control migration order:
      php artisan migrate:generate --log-with-batch=0 --tables="batch1_tables"
      php artisan migrate:generate --log-with-batch=1 --tables="batch2_tables"
      
  4. Environment-Specific Config:

    • Use .env variables to dynamically set options:
      php artisan migrate:generate --date="$(date +'%Y-%m-%d %H:%M:%S')"
      
    • Or configure in config/migrations-generator.php (if extended).

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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor