Laravel 마이그레이션 작성시 index 존재 여부 확인하는 방법

Laravel에 테이블이나 컬럼이 존재하는지 확인하는 메소드는 있는데 index 존재 여부를 확인하는 메소드는 지원하지 않아서 다소 아쉬운 면이 있었습니다. 찾아보니 doctrine schema manager 를 사용하면 확인이 가능하더군요. Laravel로 마이그레이션 작성해보신 분들은 아래 예제 코드 보시면 바로 이해가 되실거에요. 아마 doctrine/dbal 패키지를 설치가 필요할 거에요.(확인해보진 않았습니다 ^^ 어차피 renameColumn 하려면 필요하니까 걍 설치 고고)

Schema::table('articles', function($table)
{
    $conn = Schema::getConnection();
    $dbSchemaManager = $conn->getDoctrineSchemaManager();
    $doctrineTable = $dbSchemaManager->listTableDetails('articles');

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

 

leaderboard-728x90

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.