Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 8, 2017
1 parent 57bcf63 commit c877cb0
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Illuminate\Database\Schema;

use Closure;
use BadMethodCallException;
use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\Schema\Grammars\Grammar;

class Blueprint
Expand Down Expand Up @@ -100,22 +102,11 @@ public function toSql(Connection $connection, Grammar $grammar)

$statements = [];

if ($connection instanceof \Illuminate\Database\SQLiteConnection) {
$badDDLCount = collect($this->commands)
->filter(function($item, $key) {
return in_array($item->name, [ 'dropColumn', 'renameColumn' ]);
})
->count();

if ($badDDLCount > 1) {
throw new \BadMethodCallException("Multiple calls to dropColumn/renameColumn in a single table modification are not supported.");
}
}


// Each type of command has a corresponding compiler function on the schema
// grammar which is used to build the necessary SQL statements to build
// the blueprint element, so we'll just call that compilers function.
$this->ensureCommandsAreValid($connection);

foreach ($this->commands as $command) {
$method = 'compile'.ucfirst($command->name);

Expand All @@ -129,6 +120,35 @@ public function toSql(Connection $connection, Grammar $grammar)
return $statements;
}

/**
* Ensure the commands on the blueprint are valid for the connection type.
*
* @param \Illuminate\Database\Connection $connection
* @return void
*/
protected function ensureCommandsAreValid(Connection $connection)
{
if ($connection instanceof SQLiteConnection &&
$this->commandsNamed(['dropColumn', 'renameColumn'])->count() > 1) {
throw new BadMethodCallException(
"SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification."
);
}
}

/**
* Get all of the commands matching the given names.
*
* @param array $names
* @return \Illuminate\Support\Collection
*/
protected function commandsNamed(array $names)
{
return collect($this->commands)->filter(function ($command) use ($names) {
return in_array($command->name, $names);
});
}

/**
* Add the commands that are implied by the blueprint's state.
*
Expand Down

0 comments on commit c877cb0

Please sign in to comment.