@@ -170,12 +170,30 @@ protected function renderNoWait() : ?string
170170
171171 /**
172172 * @param callable $definition
173+ * @param bool $ifNotExists
173174 *
174175 * @return static
175176 */
176- public function add (callable $ definition ) : static
177+ public function add (callable $ definition, bool $ ifNotExists = false ) : static
177178 {
178- $ this ->sql ['add ' ] = $ definition ;
179+ $ this ->sql ['add ' ][] = [
180+ 'definition ' => $ definition ,
181+ 'if_not_exists ' => $ ifNotExists ,
182+ ];
183+ return $ this ;
184+ }
185+
186+ /**
187+ * @param callable $definition
188+ *
189+ * @return static
190+ */
191+ public function addIfNotExists (callable $ definition ) : static
192+ {
193+ $ this ->sql ['add ' ][] = [
194+ 'definition ' => $ definition ,
195+ 'if_not_exists ' => true ,
196+ ];
179197 return $ this ;
180198 }
181199
@@ -184,19 +202,42 @@ protected function renderAdd() : ?string
184202 if ( ! isset ($ this ->sql ['add ' ])) {
185203 return null ;
186204 }
187- $ definition = new TableDefinition ($ this ->database );
188- $ this ->sql ['add ' ]($ definition );
189- return $ definition ->sql ('ADD ' ) ?: null ;
205+ $ parts = [];
206+ foreach ($ this ->sql ['add ' ] as $ add ) {
207+ $ definition = new TableDefinition (
208+ $ this ->database ,
209+ $ add ['if_not_exists ' ] ? 'IF NOT EXISTS ' : null
210+ );
211+ $ add ['definition ' ]($ definition );
212+ $ part = $ definition ->sql ('ADD ' );
213+ if ($ part ) {
214+ $ parts [] = $ part ;
215+ }
216+ }
217+ return $ parts ? \implode (', ' . \PHP_EOL , $ parts ) : null ;
190218 }
191219
192220 /**
193221 * @param callable $definition
222+ * @param bool $ifExists
194223 *
195224 * @return static
196225 */
197- public function change (callable $ definition ) : static
226+ public function change (callable $ definition , bool $ ifExists = false ) : static
227+ {
228+ $ this ->sql ['change ' ][] = [
229+ 'definition ' => $ definition ,
230+ 'if_exists ' => $ ifExists ,
231+ ];
232+ return $ this ;
233+ }
234+
235+ public function changeIfExists (callable $ definition ) : static
198236 {
199- $ this ->sql ['change ' ] = $ definition ;
237+ $ this ->sql ['change ' ][] = [
238+ 'definition ' => $ definition ,
239+ 'if_exists ' => true ,
240+ ];
200241 return $ this ;
201242 }
202243
@@ -205,19 +246,42 @@ protected function renderChange() : ?string
205246 if ( ! isset ($ this ->sql ['change ' ])) {
206247 return null ;
207248 }
208- $ definition = new TableDefinition ($ this ->database );
209- $ this ->sql ['change ' ]($ definition );
210- return $ definition ->sql ('CHANGE ' ) ?: null ;
249+ $ parts = [];
250+ foreach ($ this ->sql ['change ' ] as $ change ) {
251+ $ definition = new TableDefinition (
252+ $ this ->database ,
253+ $ change ['if_exists ' ] ? 'IF EXISTS ' : null
254+ );
255+ $ change ['definition ' ]($ definition );
256+ $ part = $ definition ->sql ('CHANGE ' );
257+ if ($ part ) {
258+ $ parts [] = $ part ;
259+ }
260+ }
261+ return $ parts ? \implode (', ' . \PHP_EOL , $ parts ) : null ;
211262 }
212263
213264 /**
214265 * @param callable $definition
266+ * @param bool $ifExists
215267 *
216268 * @return static
217269 */
218- public function modify (callable $ definition ) : static
270+ public function modify (callable $ definition , bool $ ifExists = false ) : static
271+ {
272+ $ this ->sql ['modify ' ][] = [
273+ 'definition ' => $ definition ,
274+ 'if_exists ' => $ ifExists ,
275+ ];
276+ return $ this ;
277+ }
278+
279+ public function modifyIfExists (callable $ definition ) : static
219280 {
220- $ this ->sql ['modify ' ] = $ definition ;
281+ $ this ->sql ['modify ' ][] = [
282+ 'definition ' => $ definition ,
283+ 'if_exists ' => true ,
284+ ];
221285 return $ this ;
222286 }
223287
@@ -226,9 +290,19 @@ protected function renderModify() : ?string
226290 if ( ! isset ($ this ->sql ['modify ' ])) {
227291 return null ;
228292 }
229- $ definition = new TableDefinition ($ this ->database );
230- $ this ->sql ['modify ' ]($ definition );
231- return $ definition ->sql ('MODIFY ' ) ?: null ;
293+ $ parts = [];
294+ foreach ($ this ->sql ['modify ' ] as $ modify ) {
295+ $ definition = new TableDefinition (
296+ $ this ->database ,
297+ $ modify ['if_exists ' ] ? 'IF EXISTS ' : null
298+ );
299+ $ modify ['definition ' ]($ definition );
300+ $ part = $ definition ->sql ('MODIFY ' );
301+ if ($ part ) {
302+ $ parts [] = $ part ;
303+ }
304+ }
305+ return $ parts ? \implode (', ' . \PHP_EOL , $ parts ) : null ;
232306 }
233307
234308 public function dropColumn (string $ name , bool $ ifExists = false ) : static
@@ -652,7 +726,7 @@ protected function joinParts(array $parts) : string
652726 *
653727 * @return int|string The number of affected rows
654728 */
655- public function run () : int | string
729+ public function run () : int | string
656730 {
657731 return $ this ->database ->exec ($ this ->sql ());
658732 }
0 commit comments