Skip to content

Commit

Permalink
Fulltext Index (#39821)
Browse files Browse the repository at this point in the history
* mysql fulltext index support

* fallback

* add doc block

* add drop support

* add test
  • Loading branch information
taylorotwell committed Nov 30, 2021
1 parent 48a53be commit af807eb
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Illuminate/Database/Schema/Blueprint.php
Expand Up @@ -208,7 +208,7 @@ protected function addImpliedCommands(Grammar $grammar)
protected function addFluentIndexes()
{
foreach ($this->columns as $column) {
foreach (['primary', 'unique', 'index', 'spatialIndex'] as $index) {
foreach (['primary', 'unique', 'index', 'fulltext', 'spatialIndex'] as $index) {
// If the index has been specified on the given column, but is simply equal
// to "true" (boolean), no name has been specified for this index so the
// index method can be called without a name and it will generate one.
Expand Down Expand Up @@ -367,6 +367,17 @@ public function dropIndex($index)
return $this->dropIndexCommand('dropIndex', 'index', $index);
}

/**
* Indicate that the given fulltext index should be dropped.
*
* @param string|array $index
* @return \Illuminate\Support\Fluent
*/
public function dropFulltext($index)
{
return $this->dropIndexCommand('dropFulltext', 'fulltext', $index);
}

/**
* Indicate that the given spatial index should be dropped.
*
Expand Down Expand Up @@ -530,6 +541,19 @@ public function index($columns, $name = null, $algorithm = null)
return $this->indexCommand('index', $columns, $name, $algorithm);
}

/**
* Specify an fulltext for the table.
*
* @param string|array $columns
* @param string|null $name
* @param string|null $algorithm
* @return \Illuminate\Support\Fluent
*/
public function fulltext($columns, $name = null, $algorithm = null)
{
return $this->indexCommand('fulltext', $columns, $name, $algorithm);
}

/**
* Specify a spatial index for the table.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Database/Schema/ColumnDefinition.php
Expand Up @@ -20,6 +20,7 @@
* @method $this nullable(bool $value = true) Allow NULL values to be inserted into the column
* @method $this persisted() Mark the computed generated column as persistent (SQL Server)
* @method $this primary() Add a primary index
* @method $this fulltext() Add a fulltext index
* @method $this spatialIndex() Add a spatial index
* @method $this startingValue(int $startingValue) Set the starting value of an auto-incrementing field (MySQL/PostgreSQL)
* @method $this storedAs(string $expression) Create a stored generated column (MySQL/PostgreSQL/SQLite)
Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Expand Up @@ -83,6 +83,32 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
return ChangeColumn::compile($this, $blueprint, $command, $connection);
}

/**
* Compile a fulltext index key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*
* @throws \RuntimeException
*/
public function compileFulltext(Blueprint $blueprint, Fluent $command)
{
throw new RuntimeException('This database driver does not support fulltext index creation.');
}

/**
* Compile a drop fulltext index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileDropFulltext(Blueprint $blueprint, Fluent $command)
{
throw new RuntimeException('This database driver does not support fulltext index creation.');
}

/**
* Compile a foreign key command.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Expand Up @@ -241,6 +241,18 @@ public function compileIndex(Blueprint $blueprint, Fluent $command)
return $this->compileKey($blueprint, $command, 'index');
}

/**
* Compile a fulltext index key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileFulltext(Blueprint $blueprint, Fluent $command)
{
return $this->compileKey($blueprint, $command, 'fulltext');
}

/**
* Compile a spatial index key command.
*
Expand Down Expand Up @@ -350,6 +362,18 @@ public function compileDropIndex(Blueprint $blueprint, Fluent $command)
return "alter table {$this->wrapTable($blueprint)} drop index {$index}";
}

/**
* Compile a drop fulltext index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileDropFulltext(Blueprint $blueprint, Fluent $command)
{
return $this->compileDropIndex($blueprint, $command);
}

/**
* Compile a drop spatial index command.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Expand Up @@ -362,6 +362,16 @@ public function testAddingIndexWithAlgorithm()
$this->assertSame('alter table `users` add index `baz` using hash(`foo`, `bar`)', $statements[0]);
}

public function testAddingFulltextIndex()
{
$blueprint = new Blueprint('users');
$blueprint->fulltext('body');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add fulltext `users_body_fulltext`(`body`)', $statements[0]);
}

public function testAddingSpatialIndex()
{
$blueprint = new Blueprint('geo');
Expand Down

0 comments on commit af807eb

Please sign in to comment.