From dce043245a6c403152e04ddce14d786e9532c2ca Mon Sep 17 00:00:00 2001 From: Felix Lebel Date: Sat, 18 Jan 2025 17:55:13 -0500 Subject: [PATCH 1/2] Add raw index creation on the schema blueprint for **postgresql** via `$table->rawIndex("sql here", "index_name")` and `$table->index([DB::raw(...)], 'index_name')`. --- src/Illuminate/Database/Schema/Blueprint.php | 12 ++++++++++++ .../Database/Schema/Grammars/PostgresGrammar.php | 8 ++++++++ src/Illuminate/Database/Schema/IndexDefinition.php | 1 + 3 files changed, 21 insertions(+) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index ca2eed4eb55b..e5f316ddb4b0 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -716,6 +716,18 @@ public function rawIndex($expression, $name) return $this->index([new Expression($expression)], $name); } + /** + * Specify a raw unique index for the table. + * + * @param string $expression + * @param string $name + * @return \Illuminate\Database\Schema\IndexDefinition + */ + public function rawUnique($expression, $name) + { + return $this->unique([new Expression($expression)], $name); + } + /** * Specify a foreign key for the table. * diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index c9a579fdad2e..85132736621d 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -314,6 +314,14 @@ public function compilePrimary(Blueprint $blueprint, Fluent $command) */ public function compileUnique(Blueprint $blueprint, Fluent $command) { + if ($command->useCreateIndex) { + return sprintf('create unique index %s on %s (%s)', + $this->wrap($command->index), + $this->wrapTable($blueprint), + $this->columnize($command->columns), + ); + } + $sql = sprintf('alter table %s add constraint %s unique (%s)', $this->wrapTable($blueprint), $this->wrap($command->index), diff --git a/src/Illuminate/Database/Schema/IndexDefinition.php b/src/Illuminate/Database/Schema/IndexDefinition.php index fc5d78e5b92f..a961a2662d6a 100644 --- a/src/Illuminate/Database/Schema/IndexDefinition.php +++ b/src/Illuminate/Database/Schema/IndexDefinition.php @@ -9,6 +9,7 @@ * @method $this language(string $language) Specify a language for the full text index (PostgreSQL) * @method $this deferrable(bool $value = true) Specify that the unique index is deferrable (PostgreSQL) * @method $this initiallyImmediate(bool $value = true) Specify the default time to check the unique index constraint (PostgreSQL) + * @method $this useCreateIndex(bool $value = true) Specify if the unique index will be created using CREATE UNIQUE INDEX (PostgreSQL) */ class IndexDefinition extends Fluent { From 857d0224888b93219ce7f273b37982854d8565d6 Mon Sep 17 00:00:00 2001 From: Felix Lebel Date: Sat, 18 Jan 2025 18:11:56 -0500 Subject: [PATCH 2/2] Add tests --- .../DatabasePostgresSchemaGrammarTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 097f1a7f7c3d..a0a1ce94a527 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -274,6 +274,16 @@ public function testAddingUniqueKey() $this->assertSame('alter table "users" add constraint "bar" unique ("foo")', $statements[0]); } + public function testAddingUniqueKeyViaUseCreateIndex() + { + $blueprint = new Blueprint('users'); + $blueprint->unique('foo', 'bar')->useCreateIndex(); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertSame('create unique index "bar" on "users" ("foo")', $statements[0]); + } + public function testAddingIndex() { $blueprint = new Blueprint('users'); @@ -364,6 +374,26 @@ public function testAddingRawIndex() $this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]); } + public function testAddingRawUniqueKey() + { + $blueprint = new Blueprint('users'); + $blueprint->rawUnique('foo,bar', 'baz'); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertSame('alter table "users" add constraint "baz" unique (foo,bar)', $statements[0]); + } + + public function testAddingRawUniqueKeyViaUseCreateIndex() + { + $blueprint = new Blueprint('users'); + $blueprint->rawUnique('function(column)', 'baz')->useCreateIndex(); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertSame('create unique index "baz" on "users" (function(column))', $statements[0]); + } + public function testAddingIncrementingID() { $blueprint = new Blueprint('users');