dotted-ai/laravel-db-introspector

There is no license information available for the latest version (dev-main) of this package.

Generate migrations from existing database schema

dev-main 2025-07-08 19:00 UTC

This package is auto-updated.

Last update: 2025-07-08 19:00:50 UTC


README

A Laravel package to inspect an existing database schema and generate corresponding migration files automatically.

🚀 Installation

  1. Require via Composer:

    composer require dotted-ai/laravel-db-introspector
  2. Publish the stub template (optional):

    php artisan vendor:publish \
        --provider="Vendor\DbIntrospector\DatabaseInspectorServiceProvider" \
        --tag=stubs

🎯 Usage

Run the artisan command to generate migrations:

php artisan db:generate-migrations \
    [--tables=users,posts] \
    [--rewrite] \
    [--output-dir=from_schema]
  • --tables= (optional): Comma-separated list of tables. If omitted, all tables are processed.
  • --rewrite (optional): Overwrite existing migration files instead of skipping.
  • --output-dir= (optional): Specify a subdirectory under database/migrations to place generated files.

Examples

  • Generate migrations for all tables, skip existing:

    php artisan db:generate-migrations
  • Generate only users and posts, overwrite any existing:

    php artisan db:generate-migrations --tables=users,posts --rewrite
  • Output migrations to database/migrations/from_schema:

    php artisan db:generate-migrations --output-dir=from_schema

⚙️ Configuration

You can customize the stub template located at database/stubs/db-introspector/migration.stub after publishing.

The stub uses placeholders:

  • {{class}} — migration class name
  • {{table}} — table name
  • {{fields}} — column definitions

🛠️ How It Works

  1. Introspection: Uses Doctrine DBAL to list tables and columns.
  2. Build: Maps Doctrine column types to Laravel schema methods.
  3. Generate: Writes migration files with timestamps to avoid naming conflicts.

📄 License

MIT © Your Company