From c877cb0cdc44243c691eb8507616a4c21a28599f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 8 Dec 2017 16:15:37 -0600 Subject: [PATCH] refactor --- src/Illuminate/Database/Schema/Blueprint.php | 46 ++++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 9b5e128525ba..2eec637a8b49 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -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 @@ -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); @@ -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. *