Skip to content

[10.x] Add hasIndex and hasIndexes methods to Schema Builder #49260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/Database/DatabaseSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down