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

[5.6] Better enumeration columns support #22109

Merged
merged 1 commit into from
Nov 22, 2017
Merged
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
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ public function parameterize(array $values)
return implode(', ', array_map([$this, 'parameter'], $values));
}

/**
* Quote string literals.
*
* @param string|array $value
* @return string
*/
public function quote($value)
{
if (is_array($value)) {
return implode(', ', array_map([$this, 'quote'], $value));
}

return "'$value'";
}

/**
* Get the appropriate query parameter place-holder for a value.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,14 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return "enum('".implode("', '", $column->allowed)."')";
return sprintf('enum(%s)', $this->quote($column->allowed));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,18 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
$allowed = array_map(function ($a) {
return "'{$a}'";
}, $column->allowed);

return "varchar(255) check (\"{$column->name}\" in (".implode(', ', $allowed).'))';
return sprintf(
'varchar(255) check ("%s" in (%s))',
$column->name,
$this->quote($column->allowed)
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,18 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return 'varchar';
return sprintf(
'varchar check ("%s" in (%s))',
$column->name,
$this->quote($column->allowed)
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,18 @@ protected function typeBoolean(Fluent $column)
}

/**
* Create the column definition for an enum type.
* Create the column definition for an enumeration type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeEnum(Fluent $column)
{
return 'nvarchar(255)';
return sprintf(
'nvarchar(255) check ("%s" in (%s))',
$column->name,
$this->quote($column->allowed)
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` add `foo` enum(\'bar\', \'baz\') not null', $statements[0]);
$this->assertEquals('alter table `users` add `role` enum(\'member\', \'admin\') not null', $statements[0]);
}

public function testAddingJson()
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" varchar(255) check ("foo" in (\'bar\', \'baz\')) not null', $statements[0]);
$this->assertEquals('alter table "users" add column "role" varchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
}

public function testAddingDate()
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add column "foo" varchar not null', $statements[0]);
$this->assertEquals('alter table "users" add column "role" varchar check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
}

public function testAddingJson()
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ public function testAddingBoolean()
public function testAddingEnum()
{
$blueprint = new Blueprint('users');
$blueprint->enum('foo', ['bar', 'baz']);
$blueprint->enum('role', ['member', 'admin']);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add "foo" nvarchar(255) not null', $statements[0]);
$this->assertEquals('alter table "users" add "role" nvarchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
}

public function testAddingJson()
Expand Down