Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add ability to create raw unique indexes for postgresql #54254

Open
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Database/Schema/IndexDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
Loading