From af807ebc0de78e04db83ee1a578660f629fec833 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 30 Nov 2021 09:21:26 -0600 Subject: [PATCH] Fulltext Index (#39821) * mysql fulltext index support * fallback * add doc block * add drop support * add test --- src/Illuminate/Database/Schema/Blueprint.php | 26 ++++++++++++++++++- .../Database/Schema/ColumnDefinition.php | 1 + .../Database/Schema/Grammars/Grammar.php | 26 +++++++++++++++++++ .../Database/Schema/Grammars/MySqlGrammar.php | 24 +++++++++++++++++ .../DatabaseMySqlSchemaGrammarTest.php | 10 +++++++ 5 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index e2b968ab6bd4..954607f3c2b0 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -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. @@ -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. * @@ -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. * diff --git a/src/Illuminate/Database/Schema/ColumnDefinition.php b/src/Illuminate/Database/Schema/ColumnDefinition.php index 9a55732a959b..5f3be6170114 100644 --- a/src/Illuminate/Database/Schema/ColumnDefinition.php +++ b/src/Illuminate/Database/Schema/ColumnDefinition.php @@ -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) diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index 2acaa76a8f1f..380fcd30da53 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index b6e4e3568d8e..2eaefd1f5156 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -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. * @@ -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. * diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 05e92a085435..26c8bffa3c3c 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -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');