Laravel Migrations Generator

repository·7.x·Indexed 25 days ago

https://github.com/kitloong/laravel-migrations-generator

A tool to reverse-engineer existing databases into Laravel migration files. It supports MariaDB, MySQL, PostgreSQL, SQL Server, and SQLite, with the ability to generate migrations for tables, indexes, foreign keys, views, and stored procedures using the `migrate:generate` Artisan command.

Tokens
6.2K
Snippets
28
Records
38
Agent score
83%

What's inside laravel-migrations-generator

  1. Understand migration file generation order

    7.x

    When generating migrations from an existing schema, the tool follows a specific order to ensure database integrity:

    1. Table Creation: Individual migration files are generated for each table (e.g., create_users_table.php).
    2. Foreign Keys: The migration file for adding foreign keys is always generated last. This ensures that the tables being referenced by the foreign keys already exist before the constraints are applied.
  2. Generate Migrations from Database

    7.x

    Ensure your database is configured in config/database.php. Use the migrate:generate Artisan command to create migrations.

    Basic Usage: Generate migrations for all tables:

    php artisan migrate:generate

    Filtering Tables: Specify specific tables to include:

    php artisan migrate:generate --tables="table1,table2,table3"

    Ignore specific tables:

    php artisan migrate:generate --ignore="table3,table4,table5"

    Using a Specific Connection: If you are not using the default connection:

    php artisan migrate:generate --connection="connection_name"

    Note on Foreign Keys: The generator creates all tables, columns, and indexes first, then sets up foreign key constraints. Ensure all tables referenced by foreign keys are included in the generation process.

  3. Handle SQLite foreign key limitations during migration generation

    7.x

    When using the generator with SQLite, be aware that SQLite does not support adding foreign keys to existing tables via ALTER TABLE.

    To accommodate this, the generator follows a two-step process:

    1. It generates all tables first.
    2. It generates separate *_add_foreign_keys_* migrations to add foreign keys to existing tables.

    If you are migrating to a SQLite database, these *_add_foreign_keys_* migrations will be automatically omitted to prevent errors, as SQLite only supports foreign key definitions during initial table creation.

  4. Handle User-Defined Type (UDT) columns in generated migrations

    7.x

    When the generator encounters user-defined types in your database schema, it cannot represent them using standard Laravel Blueprint methods. Instead, it generates a standard Schema::create block followed by a raw DB::statement to add the custom column via an ALTER TABLE command.

    Important Note on Column Ordering: Because custom columns are added via ALTER TABLE after the initial table creation, these columns will always appear at the end of the table structure in the generated migration. This means the column order in the migration file may differ from the actual order in your database schema.

    public function up()
    {
        Schema::create('table', function (Blueprint $table) {
            ...
        });
        DB::statement("ALTER TABLE table ADD column custom_type NOT NULL");
    }
  5. Generate migrations for all tables

    7.x

    To generate Laravel migration files for all tables in your current database schema, use the migrate:generate Artisan command.

    During the process, the CLI will prompt you with the following:

    1. Log migrations in the migrations table: If you choose yes, the generated migrations will be recorded in your database's migrations table. This is useful if you want the generated migrations to be treated as part of the existing migration history.
    2. Batch Number: If you choose to log migrations, you will be asked for a batch number. It is recommended to use 0 so that these migrations are treated as the "first" migration in the history.
    php artisan migrate:generate
  6. Install Laravel Migrations Generator

    7.x

    Install the package via Composer as a development dependency. This tool allows you to generate Laravel migrations from an existing database, including indexes and foreign keys.

    Supported databases:

    • MariaDB
    • MySQL
    • PostgreSQL
    • SQL Server
    • SQLite
    composer require --dev kitloong/laravel-migrations-generator
  7. Configure Lumen Setup

    7.x

    Lumen does not support auto-discovery. You must manually enable facades and register the service provider in bootstrap/app.php.

    1. Enable Facades: Uncomment $app->withFacades();.

    2. Register the Provider: Add the following line to the Register Service Providers section:

    $app->register(\KitLoong\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);

    // bootstrap/app.php
    
    $app->withFacades();
    
    $app->register(\KitLoong\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);