Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bb1f1a

Browse files
committed
Postgres ENUMs are now more truly implemented using 'check' constraints.
1 parent e5e60f1 commit 3bb1f1a

3 files changed

Lines changed: 6 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,9 @@ protected function typeBoolean(Fluent $column)
358358
*/
359359
protected function typeEnum(Fluent $column)
360360
{
361-
return 'varchar(255)';
361+
$allowed = array_map(function($a) { return "'".$a."'"; }, $column->allowed);
362+
363+
return "varchar(255) check ({$column->name} in (".implode(', ', $allowed)."))";
362364
}
363365

364366
/**

src/Illuminate/Foundation/changes.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
{"message": "Allow array sizes to be checked by validator.", "backport": null},
2525
{"message": "Added support for where conditions on unique validation rule.", "backport": null},
2626
{"message": "Restore method on Eloquent models now fires restoring and restored events.", "backport": null},
27-
{"message": "Fixed re-population of radio buttons and checkboxes in FormBuilder.", "backport": null}
27+
{"message": "Fixed re-population of radio buttons and checkboxes in FormBuilder.", "backport": null},
28+
{"message": "Postgres ENUMs are now more truly implemented using 'check' constraints.", "backport": null}
2829
]
2930
}

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function testAddingEnum()
344344
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
345345

346346
$this->assertEquals(1, count($statements));
347-
$this->assertEquals('alter table "users" add column "foo" varchar(255) not null', $statements[0]);
347+
$this->assertEquals('alter table "users" add column "foo" varchar(255) check (foo in (\'bar\', \'baz\')) not null', $statements[0]);
348348
}
349349

350350

0 commit comments

Comments
 (0)