Skip to content
Closed
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
40 changes: 34 additions & 6 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,6 @@ protected function ensureCommandsAreValid(Connection $connection)
"SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification."
);
}

if ($this->commandsNamed(['dropForeign'])->count() > 0) {
throw new BadMethodCallException(
"SQLite doesn't support dropping foreign keys (you would need to re-create the table)."
);
}
}
}

Expand Down Expand Up @@ -452,6 +446,28 @@ public function dropConstrainedForeignIdFor($model, $column = null)
return $this->dropConstrainedForeignId($column ?: $model->getForeignKey());
}

/**
* Indicate that the given constraint should be dropped.
*
* @param string $name
* @return \Illuminate\Support\Fluent
*/
public function dropConstraint($name)
{
return $this->addCommand('dropConstraint', ['constraint' => $name]);
}

/**
* Indicate that the given check constraint should be dropped.
*
* @param string $name
* @return \Illuminate\Support\Fluent
*/
public function dropCheck($name)
{
return $this->dropConstraint($name);
}

/**
* Indicate that the given indexes should be renamed.
*
Expand Down Expand Up @@ -635,6 +651,18 @@ public function foreign($columns, $name = null)
return $command;
}

/**
* Specify a check constraint for the table.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $expression
* @param string|null $name
* @return \Illuminate\Support\Fluent
*/
public function check($expression, $name = null)
{
return $this->addCommand('check', ['expression' => $expression, 'constraint' => $name]);
}

/**
* Create a new auto-incrementing big integer (8-byte) column on the table.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Database/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
* @method $this autoIncrement() Set INTEGER columns as auto-increment (primary key)
* @method $this change() Change the column
* @method $this charset(string $charset) Specify a character set for the column (MySQL)
* @method $this check(\Illuminate\Contracts\Database\Query\Expression|string $expression) Specify a check constraint for the column
* @method $this collation(string $collation) Specify a collation for the column (MySQL/PostgreSQL/SQL Server)
* @method $this comment(string $comment) Add a comment to the column (MySQL/PostgreSQL)
* @method $this default(mixed $value) Specify a "default" value for the column
* @method $this first() Place the column "first" in the table (MySQL)
* @method $this from(int $startingValue) Set the starting value of an auto-incrementing field (MySQL / PostgreSQL)
* @method $this generatedAs(string|Expression $expression = null) Create a SQL compliant identity column (PostgreSQL)
* @method $this generatedAs(\Illuminate\Contracts\Database\Query\Expression|string $expression = null) Create a SQL compliant identity column (PostgreSQL)
* @method $this index(string $indexName = null) Add an index
* @method $this invisible() Specify that the column should be invisible to "SELECT *" (MySQL)
* @method $this nullable(bool $value = true) Allow NULL values to be inserted into the column
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,37 @@ public function compileForeign(Blueprint $blueprint, Fluent $command)
return $sql;
}

/**
* Compile a check constraint command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileCheck(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter table %s add%s check (%s)',
$this->wrapTable($blueprint),
$command->constraint ? ' constraint '.$this->wrap($command->constraint) : '',
$this->getValue($command->expression)
);
}

/**
* Compile a drop constraint command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileDropConstraint(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter table %s drop constraint %s',
$this->wrapTable($blueprint),
$this->wrap($command->constraint)
);
}

/**
* Compile the blueprint's added column definitions.
*
Expand Down
20 changes: 16 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MySqlGrammar extends Grammar
*/
protected $modifiers = [
'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable',
'Srid', 'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First',
'Srid', 'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'Check', 'After', 'First',
];

/**
Expand Down Expand Up @@ -401,9 +401,7 @@ public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
*/
public function compileDropUnique(Blueprint $blueprint, Fluent $command)
{
$index = $this->wrap($command->index);

return "alter table {$this->wrapTable($blueprint)} drop index {$index}";
return $this->compileDropIndex($blueprint, $command);
}

/**
Expand Down Expand Up @@ -1250,6 +1248,20 @@ protected function modifySrid(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a check constraint column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifyCheck(Blueprint $blueprint, Fluent $column)
{
if (! is_null($column->check)) {
return ' check ('.$this->getValue($column->check).')';
}
}

/**
* Wrap a single string in keyword identifiers.
*
Expand Down
41 changes: 30 additions & 11 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PostgresGrammar extends Grammar
*
* @var string[]
*/
protected $modifiers = ['Collate', 'Nullable', 'Default', 'VirtualAs', 'StoredAs', 'GeneratedAs', 'Increment'];
protected $modifiers = ['Collate', 'Nullable', 'Check', 'Default', 'VirtualAs', 'StoredAs', 'GeneratedAs', 'Increment'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -427,9 +427,9 @@ public function compileDropColumn(Blueprint $blueprint, Fluent $command)
*/
public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
{
$index = $this->wrap("{$blueprint->getTable()}_pkey");
$command->constraint = "{$blueprint->getTable()}_pkey";

return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}";
return $this->compileDropConstraint($blueprint, $command);
}

/**
Expand All @@ -441,9 +441,11 @@ public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
*/
public function compileDropUnique(Blueprint $blueprint, Fluent $command)
{
$index = $this->wrap($command->index);
$command->constraint = $command->index;

return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
$command->index = null;

return $this->compileDropConstraint($blueprint, $command);
}

/**
Expand Down Expand Up @@ -491,9 +493,11 @@ public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
*/
public function compileDropForeign(Blueprint $blueprint, Fluent $command)
{
$index = $this->wrap($command->index);
$command->constraint = $command->index;

$command->index = null;

return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
return $this->compileDropConstraint($blueprint, $command);
}

/**
Expand Down Expand Up @@ -785,11 +789,12 @@ protected function typeBoolean(Fluent $column)
*/
protected function typeEnum(Fluent $column)
{
return sprintf(
'varchar(255) check ("%s" in (%s))',
$column->name,
$column->check(sprintf('%s in (%s)',
$this->wrap($column),
$this->quoteString($column->allowed)
);
));

return 'varchar(255)';
}

/**
Expand Down Expand Up @@ -1216,4 +1221,18 @@ protected function modifyGeneratedAs(Blueprint $blueprint, Fluent $column)

return $sql;
}

/**
* Get the SQL for a check constraint column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifyCheck(Blueprint $blueprint, Fluent $column)
{
if (! $column->change && ! is_null($column->check)) {
return ' check ('.$this->getValue($column->check).')';
}
}
}
91 changes: 81 additions & 10 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Schema\Grammars;

use BadMethodCallException;
use Doctrine\DBAL\Schema\Index;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
Expand All @@ -17,7 +18,7 @@ class SQLiteGrammar extends Grammar
*
* @var string[]
*/
protected $modifiers = ['Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs'];
protected $modifiers = ['Increment', 'Nullable', 'Check', 'Default', 'VirtualAs', 'StoredAs'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -56,12 +57,13 @@ public function compileColumnListing($table)
*/
public function compileCreate(Blueprint $blueprint, Fluent $command)
{
return sprintf('%s table %s (%s%s%s)',
return sprintf('%s table %s (%s%s%s%s)',
$blueprint->temporary ? 'create temporary' : 'create',
$this->wrapTable($blueprint),
implode(', ', $this->getColumns($blueprint)),
(string) $this->addForeignKeys($blueprint),
(string) $this->addPrimaryKeys($blueprint)
(string) $this->addPrimaryKeys($blueprint),
(string) $this->addCheckConstraints($blueprint),
);
}

Expand Down Expand Up @@ -127,6 +129,24 @@ protected function addPrimaryKeys(Blueprint $blueprint)
}
}

/**
* Get the check constraint syntax for a table creation statement.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @return string|null
*/
protected function addCheckConstraints(Blueprint $blueprint)
{
$constraints = $this->getCommandsByName($blueprint, 'check');

return collect($constraints)->reduce(function ($sql, $constraint) {
return $sql.sprintf(',%s check (%s)',
$constraint->constraint ? ' constraint '.$this->wrap($constraint->constraint) : '',
$this->getValue($constraint->expression)
);
}, '');
}

/**
* Compile alter table commands for adding columns.
*
Expand Down Expand Up @@ -222,6 +242,18 @@ public function compileForeign(Blueprint $blueprint, Fluent $command)
// Handled on table creation...
}

/**
* Compile a check constraint command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string|null
*/
public function compileCheck(Blueprint $blueprint, Fluent $command)
{
// Handled on table creation...
}

/**
* Compile a drop table command.
*
Expand Down Expand Up @@ -337,9 +369,7 @@ public function compileDropColumn(Blueprint $blueprint, Fluent $command, Connect
*/
public function compileDropUnique(Blueprint $blueprint, Fluent $command)
{
$index = $this->wrap($command->index);

return "drop index {$index}";
return $this->compileDropIndex($blueprint, $command);
}

/**
Expand Down Expand Up @@ -370,6 +400,32 @@ public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
throw new RuntimeException('The database driver in use does not support spatial indexes.');
}

/**
* Compile a drop foreign key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileDropForeign(Blueprint $blueprint, Fluent $command)
{
throw new BadMethodCallException(
"SQLite doesn't support dropping foreign keys (you would need to re-create the table)."
);
}

/**
* Compile a drop constraint command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileDropConstraint(Blueprint $blueprint, Fluent $command)
{
throw new RuntimeException('This database driver does not support dropping constraints.');
}

/**
* Compile a rename table command.
*
Expand Down Expand Up @@ -632,11 +688,12 @@ protected function typeBoolean(Fluent $column)
*/
protected function typeEnum(Fluent $column)
{
return sprintf(
'varchar check ("%s" in (%s))',
$column->name,
$column->check(sprintf('%s in (%s)',
$this->wrap($column),
$this->quoteString($column->allowed)
);
));

return 'varchar';
}

/**
Expand Down Expand Up @@ -995,6 +1052,20 @@ protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a check constraint column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function modifyCheck(Blueprint $blueprint, Fluent $column)
{
if (! is_null($column->check)) {
return ' check ('.$this->getValue($column->check).')';
}
}

/**
* Wrap the given JSON selector.
*
Expand Down
Loading