Skip to content

Commit d4169a5

Browse files
Anton Komarevtaylorotwell
Anton Komarev
authored andcommitted
Add spatialIndex method to Blueprint and in MySqlGrammar (#21070)
1 parent ac95aa4 commit d4169a5

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function addImpliedCommands()
142142
protected function addFluentIndexes()
143143
{
144144
foreach ($this->columns as $column) {
145-
foreach (['primary', 'unique', 'index'] as $index) {
145+
foreach (['primary', 'unique', 'index', 'spatialIndex'] as $index) {
146146
// If the index has been specified on the given column, but is simply equal
147147
// to "true" (boolean), no name has been specified for this index so the
148148
// index method can be called without a name and it will generate one.
@@ -385,6 +385,18 @@ public function index($columns, $name = null, $algorithm = null)
385385
return $this->indexCommand('index', $columns, $name, $algorithm);
386386
}
387387

388+
/**
389+
* Specify a spatial index for the table.
390+
*
391+
* @param string|array $columns
392+
* @param string $name
393+
* @return \Illuminate\Support\Fluent
394+
*/
395+
public function spatialIndex($columns, $name = null)
396+
{
397+
return $this->indexCommand('spatialIndex', $columns, $name);
398+
}
399+
388400
/**
389401
* Specify a foreign key for the table.
390402
*

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ public function compileIndex(Blueprint $blueprint, Fluent $command)
193193
return $this->compileKey($blueprint, $command, 'index');
194194
}
195195

196+
/**
197+
* Compile a spatial index key command.
198+
*
199+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
200+
* @param \Illuminate\Support\Fluent $command
201+
* @return string
202+
*/
203+
public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
204+
{
205+
return $this->compileKey($blueprint, $command, 'spatial index');
206+
}
207+
196208
/**
197209
* Compile an index creation command.
198210
*

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,26 @@ public function testAddingIndexWithAlgorithm()
291291
$this->assertEquals('alter table `users` add index `baz` using hash(`foo`, `bar`)', $statements[0]);
292292
}
293293

294+
public function testAddingSpatialIndex()
295+
{
296+
$blueprint = new Blueprint('users');
297+
$blueprint->spatialIndex('foo', 'baz');
298+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
299+
300+
$this->assertCount(1, $statements);
301+
$this->assertEquals('alter table `users` add spatial index `baz`(`foo`)', $statements[0]);
302+
}
303+
304+
public function testAddingFluentSpatialIndex()
305+
{
306+
$blueprint = new Blueprint('users');
307+
$blueprint->point('foo')->spatialIndex('baz');
308+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
309+
310+
$this->assertCount(2, $statements);
311+
$this->assertEquals('alter table `users` add spatial index `baz`(`foo`)', $statements[1]);
312+
}
313+
294314
public function testAddingForeignKey()
295315
{
296316
$blueprint = new Blueprint('users');

0 commit comments

Comments
 (0)