3
3
namespace Illuminate \Database \Schema ;
4
4
5
5
use Closure ;
6
+ use BadMethodCallException ;
6
7
use Illuminate \Support \Fluent ;
7
8
use Illuminate \Database \Connection ;
8
9
use Illuminate \Support \Traits \Macroable ;
10
+ use Illuminate \Database \SQLiteConnection ;
9
11
use Illuminate \Database \Schema \Grammars \Grammar ;
10
12
11
13
class Blueprint
@@ -100,22 +102,11 @@ public function toSql(Connection $connection, Grammar $grammar)
100
102
101
103
$ statements = [];
102
104
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
-
116
105
// Each type of command has a corresponding compiler function on the schema
117
106
// grammar which is used to build the necessary SQL statements to build
118
107
// the blueprint element, so we'll just call that compilers function.
108
+ $ this ->ensureCommandsAreValid ($ connection );
109
+
119
110
foreach ($ this ->commands as $ command ) {
120
111
$ method = 'compile ' .ucfirst ($ command ->name );
121
112
@@ -129,6 +120,35 @@ public function toSql(Connection $connection, Grammar $grammar)
129
120
return $ statements ;
130
121
}
131
122
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
+
132
152
/**
133
153
* Add the commands that are implied by the blueprint's state.
134
154
*
0 commit comments