diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 07698e7fc206..38ce4a6c7f66 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -289,6 +289,40 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure } } + /** + * Determine if the given table has a given column. + * + * @param string $table + * @param string $index + * @return bool + */ + public function hasIndex($table, $index) + { + return in_array( + strtolower($index), array_map('strtolower', $this->getIndexListing($table)) + ); + } + + /** + * Determine if the given table has given columns. + * + * @param string $table + * @param array $indexes + * @return bool + */ + public function hasIndexes($table, array $indexes) + { + $tableIndexes = array_map('strtolower', $this->getIndexListing($table)); + + foreach ($indexes as $index) { + if (! in_array(strtolower($index), $tableIndexes)) { + return false; + } + } + + return true; + } + /** * Get the data type for the given column name. * @@ -327,6 +361,17 @@ public function getColumnListing($table) return array_column($this->getColumns($table), 'name'); } + /** + * Get the index listing for a given table. + * + * @param string $table + * @return array + */ + public function getIndexListing($table) + { + return array_column($this->getIndexes($table), 'name'); + } + /** * Get the columns for a given table. * diff --git a/src/Illuminate/Support/Facades/Schema.php b/src/Illuminate/Support/Facades/Schema.php index 1bf87ba9a151..7ecad4df8048 100755 --- a/src/Illuminate/Support/Facades/Schema.php +++ b/src/Illuminate/Support/Facades/Schema.php @@ -16,6 +16,8 @@ * @method static array getViews() * @method static bool hasColumn(string $table, string $column) * @method static bool hasColumns(string $table, array $columns) + * @method static bool hasIndex(string $table, string $column) + * @method static bool hasIndexes(string $table, array $columns) * @method static void whenTableHasColumn(string $table, string $column, \Closure $callback) * @method static void whenTableDoesntHaveColumn(string $table, string $column, \Closure $callback) * @method static string getColumnType(string $table, string $column, bool $fullDefinition = false) diff --git a/tests/Database/DatabaseSchemaBuilderTest.php b/tests/Database/DatabaseSchemaBuilderTest.php index e2619c667447..4a748456bd38 100755 --- a/tests/Database/DatabaseSchemaBuilderTest.php +++ b/tests/Database/DatabaseSchemaBuilderTest.php @@ -71,6 +71,31 @@ public function testTableHasColumns() $this->assertFalse($builder->hasColumns('users', ['id', 'address'])); } + public function testTableHasIndex() + { + $connection = m::mock(Connection::class); + $grammar = m::mock(stdClass::class); + $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); + $builder = m::mock(Builder::class.'[getIndexListing]', [$connection]); + $builder->shouldReceive('getIndexListing')->with('users')->times(3)->andReturn(['id', 'firstname']); + + $this->assertTrue($builder->hasIndex('users', 'id')); + $this->assertTrue($builder->hasIndex('users', 'firstname')); + $this->assertFalse($builder->hasIndex('users', 'address')); + } + + public function testTableHasIndexes() + { + $connection = m::mock(Connection::class); + $grammar = m::mock(stdClass::class); + $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); + $builder = m::mock(Builder::class.'[getIndexListing]', [$connection]); + $builder->shouldReceive('getIndexListing')->with('users')->twice()->andReturn(['id', 'firstname']); + + $this->assertTrue($builder->hasIndexes('users', ['id', 'firstname'])); + $this->assertFalse($builder->hasIndexes('users', ['id', 'address'])); + } + public function testGetColumnTypeAddsPrefix() { $connection = m::mock(Connection::class);