Xethron / migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
3.33k stars 588 forks source link

Partial indices are not generated correctly #165

Closed mfn closed 2 years ago

mfn commented 6 years ago

I've partial indices with custom WHERE conditions, e.g.

CREATE INDEX foo_idx ON TABLE (field1, field2) WHERE (field3=false);

This gets incorrectly generated as:

$table->index(['field1', 'field2'], 'field3);

Note: I'm not ware of a way to generated them using only the Builder/Fluent methods, I currently use Schema::connection('main')->getConnection()->unprepared() for that.

Nevertheless it produces logical errors in the migrations.

h3xx commented 6 years ago

I'm not sure how to do it using the fluent query builder interface, but you could modify the migration script so it calls DB::statement after the creation statement:

    Schema::create('table', function (Blueprint $table) {
        // ... fluent column definitions ...
    });

    DB::statement("
        CREATE INDEX foo_idx ON TABLE (field1, field2) WHERE (field3=false);
    ");

This also lets you use engine-specific language and data types in your WHERE clause.

mfn commented 2 years ago

Closing, no longer using it