Skip to content
Merged
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
12 changes: 7 additions & 5 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,12 @@ public function fullText($columns, $name = null, $algorithm = null)
*
* @param string|array $columns
* @param string|null $name
* @param string|null $operatorClass
* @return \Illuminate\Database\Schema\IndexDefinition
*/
public function spatialIndex($columns, $name = null)
public function spatialIndex($columns, $name = null, $operatorClass = null)
{
return $this->indexCommand('spatialIndex', $columns, $name);
return $this->indexCommand('spatialIndex', $columns, $name, null, $operatorClass);
}

/**
Expand Down Expand Up @@ -1641,15 +1642,16 @@ public function comment($comment)
}

/**
* Add a new index command to the blueprint.
* Create a new index command on the blueprint.
*
* @param string $type
* @param string|array $columns
* @param string $index
* @param string|null $algorithm
* @param string|null $operatorClass
* @return \Illuminate\Support\Fluent
*/
protected function indexCommand($type, $columns, $index, $algorithm = null)
protected function indexCommand($type, $columns, $index, $algorithm = null, $operatorClass = null)
{
$columns = (array) $columns;

Expand All @@ -1659,7 +1661,7 @@ protected function indexCommand($type, $columns, $index, $algorithm = null)
$index = $index ?: $this->createIndexName($type, $columns);

return $this->addCommand(
$type, compact('index', 'columns', 'algorithm')
$type, compact('index', 'columns', 'algorithm', 'operatorClass')
);
}

Expand Down
37 changes: 37 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,46 @@ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
{
$command->algorithm = 'gist';

if (! is_null($command->operatorClass)) {
return $this->compileIndexWithOperatorClass($blueprint, $command);
}

return $this->compileIndex($blueprint, $command);
}

/**
* Compile a spatial index with operator class key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
protected function compileIndexWithOperatorClass(Blueprint $blueprint, Fluent $command)
{
$columns = $this->columnizeWithOperatorClass($command->columns, $command->operatorClass);

return sprintf('create index %s on %s%s (%s)',
$this->wrap($command->index),
$this->wrapTable($blueprint),
$command->algorithm ? ' using '.$command->algorithm : '',
$columns
);
}

/**
* Convert an array of column names to a delimited string with operator class.
*
* @param array $columns
* @param string $operatorClass
* @return string
*/
protected function columnizeWithOperatorClass(array $columns, $operatorClass)
{
return implode(', ', array_map(function ($column) use ($operatorClass) {
return $this->wrap($column).' '.$operatorClass;
}, $columns));
}

/**
* Compile a foreign key command.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,26 @@ public function testAddingFluentSpatialIndex()
$this->assertSame('create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")', $statements[1]);
}

public function testAddingSpatialIndexWithOperatorClass()
{
$blueprint = new Blueprint($this->getConnection(), 'geo');
$blueprint->spatialIndex('coordinates', 'my_index', 'point_ops');
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('create index "my_index" on "geo" using gist ("coordinates" point_ops)', $statements[0]);
}

public function testAddingSpatialIndexWithOperatorClassMultipleColumns()
{
$blueprint = new Blueprint($this->getConnection(), 'geo');
$blueprint->spatialIndex(['coordinates', 'location'], 'my_index', 'point_ops');
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('create index "my_index" on "geo" using gist ("coordinates" point_ops, "location" point_ops)', $statements[0]);
}

public function testAddingRawIndex()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand Down