From ee3469eb6bdfbe86eb67b14ac08bf0d9d861bf82 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 16:28:48 -0600 Subject: [PATCH 1/5] mysql fulltext index support --- src/Illuminate/Database/Schema/Blueprint.php | 15 ++++++++++++++- .../Database/Schema/Grammars/MySqlGrammar.php | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index e2b968ab6bd4..8882bff7d4aa 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. @@ -530,6 +530,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/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index b6e4e3568d8e..a3c17f1d5cb8 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. * From 0d4398e5e2970872c0303e6fb262fbe53f67b43b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 16:30:00 -0600 Subject: [PATCH 2/5] fallback --- src/Illuminate/Database/Schema/ColumnDefinition.php | 1 + src/Illuminate/Database/Schema/Grammars/Grammar.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) 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..85d1f7200b22 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -83,6 +83,18 @@ 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 + */ + public function compileFulltext(Blueprint $blueprint, Fluent $command) + { + throw new RuntimeException('This database driver does not support fulltext index creation.'); + } + /** * Compile a foreign key command. * From a21974cb1e271e1dd76ca0dcf8db9ac09c3c17d1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 16:30:22 -0600 Subject: [PATCH 3/5] add doc block --- src/Illuminate/Database/Schema/Grammars/Grammar.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index 85d1f7200b22..a624e59c7103 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -89,6 +89,8 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string + * + * @throws \RuntimeException */ public function compileFulltext(Blueprint $blueprint, Fluent $command) { From a7edf4602a72e603a030254cb458bcf0b67adc9e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 16:36:15 -0600 Subject: [PATCH 4/5] add drop support --- src/Illuminate/Database/Schema/Blueprint.php | 11 +++++++++++ src/Illuminate/Database/Schema/Grammars/Grammar.php | 12 ++++++++++++ .../Database/Schema/Grammars/MySqlGrammar.php | 12 ++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 8882bff7d4aa..954607f3c2b0 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -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. * diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index a624e59c7103..380fcd30da53 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -97,6 +97,18 @@ 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 a3c17f1d5cb8..2eaefd1f5156 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -362,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. * From d7c94c79be73d6527c3d6b24119cef435379c6a9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 20:49:56 -0600 Subject: [PATCH 5/5] add test --- tests/Database/DatabaseMySqlSchemaGrammarTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) 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');