Skip to content

Commit 2416981

Browse files
thierry2015Thierry Parent
andauthored
Add support for PostgreSQL "unique nulls not distinct" (#55025)
Co-authored-by: Thierry Parent <tparent@quartermaster.house>
1 parent a4435a0 commit 2416981

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,16 @@ public function compilePrimary(Blueprint $blueprint, Fluent $command)
333333
*/
334334
public function compileUnique(Blueprint $blueprint, Fluent $command)
335335
{
336-
$sql = sprintf('alter table %s add constraint %s unique (%s)',
336+
$uniqueStatement = 'unique';
337+
338+
if (! is_null($command->nullsNotDistinct)) {
339+
$uniqueStatement .= ' nulls '.($command->nullsNotDistinct ? 'not distinct' : 'distinct');
340+
}
341+
342+
$sql = sprintf('alter table %s add constraint %s %s (%s)',
337343
$this->wrapTable($blueprint),
338344
$this->wrap($command->index),
345+
$uniqueStatement,
339346
$this->columnize($command->columns)
340347
);
341348

src/Illuminate/Database/Schema/IndexDefinition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @method $this language(string $language) Specify a language for the full text index (PostgreSQL)
1010
* @method $this deferrable(bool $value = true) Specify that the unique index is deferrable (PostgreSQL)
1111
* @method $this initiallyImmediate(bool $value = true) Specify the default time to check the unique index constraint (PostgreSQL)
12+
* @method $this nullsNotDistinct(bool $value = true) Specify that the null values should not be treated as distinct (PostgreSQL)
1213
*/
1314
class IndexDefinition extends Fluent
1415
{

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ public function testAddingUniqueKey()
286286
$this->assertSame('alter table "users" add constraint "bar" unique ("foo")', $statements[0]);
287287
}
288288

289+
public function testAddingUniqueKeyWithNullsNotDistinct()
290+
{
291+
$blueprint = new Blueprint($this->getConnection(), 'users');
292+
$blueprint->unique('foo', 'bar')->nullsNotDistinct();
293+
$statements = $blueprint->toSql();
294+
295+
$this->assertCount(1, $statements);
296+
$this->assertSame('alter table "users" add constraint "bar" unique nulls not distinct ("foo")', $statements[0]);
297+
}
298+
299+
public function testAddingUniqueKeyWithNullsDistinct()
300+
{
301+
$blueprint = new Blueprint($this->getConnection(), 'users');
302+
$blueprint->unique('foo', 'bar')->nullsNotDistinct(false);
303+
$statements = $blueprint->toSql();
304+
305+
$this->assertCount(1, $statements);
306+
$this->assertSame('alter table "users" add constraint "bar" unique nulls distinct ("foo")', $statements[0]);
307+
}
308+
289309
public function testAddingIndex()
290310
{
291311
$blueprint = new Blueprint($this->getConnection(), 'users');

0 commit comments

Comments
 (0)