Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dev-testing-code/ea50cbd1702245feb00182463853c777 to your computer and use it in GitHub Desktop.
Save dev-testing-code/ea50cbd1702245feb00182463853c777 to your computer and use it in GitHub Desktop.

Revisions

  1. @jadeedward jadeedward revised this gist Aug 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion laravel_conditional_index_migration.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class ConditionalCreateOrRemoveIndex extends Migration
    class LaravelConditionalIndexMigration extends Migration
    {
    /**
    * Run the migrations.
  2. @jadeedward jadeedward renamed this gist Aug 8, 2017. 1 changed file with 0 additions and 0 deletions.
  3. @jadeedward jadeedward created this gist Aug 8, 2017.
    52 changes: 52 additions & 0 deletions laravel_conditional_index_migration.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class ConditionalCreateOrRemoveIndex extends Migration
    {
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {

    Schema::table('tablename', function (Blueprint $table) {
    $sm = Schema::getConnection()->getDoctrineSchemaManager();
    $doctrineTable = $sm->listTableDetails('tablename');

    if (! $doctrineTable->hasIndex('singlecolumnindexname')) {
    $table->index('column1', 'singlecolumnindexname');
    }

    if (! $doctrineTable->hasIndex('multicolumnindexname')) {
    $table->index(['column2', 'column3'], 'multicolumnindexname');
    }
    });
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {

    Schema::table('tablename', function (Blueprint $table) {
    $sm = Schema::getConnection()->getDoctrineSchemaManager();
    $doctrineTable = $sm->listTableDetails('tablename');

    if ($doctrineTable->hasIndex('singlecolumnindexname')) {
    $table->dropIndex('singlecolumnindexname');
    }
    if ($doctrineTable->hasIndex('multicolumnindexname')) {
    $table->dropIndex('multicolumnindexname');
    }
    });

    }
    }