Skip to content

Commit 7c9430a

Browse files
authored
feat: allow non length limited strings and char for postgres (#41800)
1 parent e9940f8 commit 7c9430a

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/Illuminate/Database/Schema/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Builder
3434
/**
3535
* The default string length for migrations.
3636
*
37-
* @var int
37+
* @var int|null
3838
*/
3939
public static $defaultStringLength = 255;
4040

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,11 @@ public function escapeNames($names)
509509
*/
510510
protected function typeChar(Fluent $column)
511511
{
512-
return "char({$column->length})";
512+
if ($column->length) {
513+
return "char({$column->length})";
514+
}
515+
516+
return 'char';
513517
}
514518

515519
/**
@@ -520,7 +524,11 @@ protected function typeChar(Fluent $column)
520524
*/
521525
protected function typeString(Fluent $column)
522526
{
523-
return "varchar({$column->length})";
527+
if ($column->length) {
528+
return "varchar({$column->length})";
529+
}
530+
531+
return 'varchar';
524532
}
525533

526534
/**

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Connection;
66
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Database\Schema\Builder;
78
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
89
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
910
use Mockery as m;
@@ -434,6 +435,52 @@ public function testAddingString()
434435
$this->assertSame('alter table "users" add column "foo" varchar(100) null default \'bar\'', $statements[0]);
435436
}
436437

438+
public function testAddingStringWithoutLengthLimit()
439+
{
440+
$blueprint = new Blueprint('users');
441+
$blueprint->string('foo');
442+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
443+
444+
$this->assertCount(1, $statements);
445+
$this->assertSame('alter table "users" add column "foo" varchar(255) not null', $statements[0]);
446+
447+
Builder::$defaultStringLength = null;
448+
449+
$blueprint = new Blueprint('users');
450+
$blueprint->string('foo');
451+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
452+
453+
try {
454+
$this->assertCount(1, $statements);
455+
$this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
456+
} finally {
457+
Builder::$defaultStringLength = 255;
458+
}
459+
}
460+
461+
public function testAddingCharWithoutLengthLimit()
462+
{
463+
$blueprint = new Blueprint('users');
464+
$blueprint->char('foo');
465+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
466+
467+
$this->assertCount(1, $statements);
468+
$this->assertSame('alter table "users" add column "foo" char(255) not null', $statements[0]);
469+
470+
Builder::$defaultStringLength = null;
471+
472+
$blueprint = new Blueprint('users');
473+
$blueprint->char('foo');
474+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
475+
476+
try {
477+
$this->assertCount(1, $statements);
478+
$this->assertSame('alter table "users" add column "foo" char not null', $statements[0]);
479+
} finally {
480+
Builder::$defaultStringLength = 255;
481+
}
482+
}
483+
437484
public function testAddingText()
438485
{
439486
$blueprint = new Blueprint('users');

0 commit comments

Comments
 (0)