Skip to content

Commit c877cb0

Browse files
committed
refactor
1 parent 57bcf63 commit c877cb0

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

Diff for: src/Illuminate/Database/Schema/Blueprint.php

+33-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Illuminate\Database\Schema;
44

55
use Closure;
6+
use BadMethodCallException;
67
use Illuminate\Support\Fluent;
78
use Illuminate\Database\Connection;
89
use Illuminate\Support\Traits\Macroable;
10+
use Illuminate\Database\SQLiteConnection;
911
use Illuminate\Database\Schema\Grammars\Grammar;
1012

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

101103
$statements = [];
102104

103-
if ($connection instanceof \Illuminate\Database\SQLiteConnection) {
104-
$badDDLCount = collect($this->commands)
105-
->filter(function($item, $key) {
106-
return in_array($item->name, [ 'dropColumn', 'renameColumn' ]);
107-
})
108-
->count();
109-
110-
if ($badDDLCount > 1) {
111-
throw new \BadMethodCallException("Multiple calls to dropColumn/renameColumn in a single table modification are not supported.");
112-
}
113-
}
114-
115-
116105
// Each type of command has a corresponding compiler function on the schema
117106
// grammar which is used to build the necessary SQL statements to build
118107
// the blueprint element, so we'll just call that compilers function.
108+
$this->ensureCommandsAreValid($connection);
109+
119110
foreach ($this->commands as $command) {
120111
$method = 'compile'.ucfirst($command->name);
121112

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

123+
/**
124+
* Ensure the commands on the blueprint are valid for the connection type.
125+
*
126+
* @param \Illuminate\Database\Connection $connection
127+
* @return void
128+
*/
129+
protected function ensureCommandsAreValid(Connection $connection)
130+
{
131+
if ($connection instanceof SQLiteConnection &&
132+
$this->commandsNamed(['dropColumn', 'renameColumn'])->count() > 1) {
133+
throw new BadMethodCallException(
134+
"SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification."
135+
);
136+
}
137+
}
138+
139+
/**
140+
* Get all of the commands matching the given names.
141+
*
142+
* @param array $names
143+
* @return \Illuminate\Support\Collection
144+
*/
145+
protected function commandsNamed(array $names)
146+
{
147+
return collect($this->commands)->filter(function ($command) use ($names) {
148+
return in_array($command->name, $names);
149+
});
150+
}
151+
132152
/**
133153
* Add the commands that are implied by the blueprint's state.
134154
*

0 commit comments

Comments
 (0)