16
16
use Doctrine \DBAL \SQL \Builder \DefaultSelectSQLBuilder ;
17
17
use Doctrine \DBAL \SQL \Builder \SelectSQLBuilder ;
18
18
use Doctrine \DBAL \TransactionIsolationLevel ;
19
+ use Doctrine \DBAL \Types \DateTimeType ;
19
20
use Doctrine \DBAL \Types \Types ;
20
21
21
22
use function array_merge ;
@@ -262,7 +263,6 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
262
263
public function getAlterTableSQL (TableDiff $ diff ): array
263
264
{
264
265
$ sql = [];
265
- $ columnSql = [];
266
266
$ commentsSQL = [];
267
267
268
268
$ tableNameSQL = $ diff ->getOldTable ()->getQuotedName ($ this );
@@ -296,11 +296,13 @@ public function getAlterTableSQL(TableDiff $diff): array
296
296
);
297
297
}
298
298
299
+ $ needsReorg = false ;
299
300
foreach ($ diff ->getDroppedColumns () as $ column ) {
300
301
$ queryParts [] = 'DROP COLUMN ' . $ column ->getQuotedName ($ this );
302
+ $ needsReorg = true ;
301
303
}
302
304
303
- foreach ($ diff ->getModifiedColumns () as $ columnDiff ) {
305
+ foreach ($ diff ->getChangedColumns () as $ columnDiff ) {
304
306
if ($ columnDiff ->hasCommentChanged ()) {
305
307
$ newColumn = $ columnDiff ->getNewColumn ();
306
308
$ commentsSQL [] = $ this ->getCommentOnColumnSQL (
@@ -315,33 +317,25 @@ public function getAlterTableSQL(TableDiff $diff): array
315
317
$ columnDiff ,
316
318
$ sql ,
317
319
$ queryParts ,
320
+ $ needsReorg ,
318
321
);
319
322
}
320
323
321
- foreach ($ diff ->getRenamedColumns () as $ oldColumnName => $ column ) {
322
- $ oldColumnName = new Identifier ($ oldColumnName );
323
-
324
- $ queryParts [] = 'RENAME COLUMN ' . $ oldColumnName ->getQuotedName ($ this ) .
325
- ' TO ' . $ column ->getQuotedName ($ this );
326
- }
327
-
328
324
if (count ($ queryParts ) > 0 ) {
329
325
$ sql [] = 'ALTER TABLE ' . $ tableNameSQL . ' ' . implode (' ' , $ queryParts );
330
326
}
331
327
332
328
// Some table alteration operations require a table reorganization.
333
- if (count ( $ diff -> getDroppedColumns ()) > 0 || count ( $ diff -> getModifiedColumns ()) > 0 ) {
329
+ if ($ needsReorg ) {
334
330
$ sql [] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $ tableNameSQL . "') " ;
335
331
}
336
332
337
- $ sql = array_merge (
333
+ return array_merge (
338
334
$ this ->getPreAlterTableIndexForeignKeySQL ($ diff ),
339
335
$ sql ,
340
336
$ commentsSQL ,
341
337
$ this ->getPostAlterTableIndexForeignKeySQL ($ diff ),
342
338
);
343
-
344
- return array_merge ($ sql , $ columnSql );
345
339
}
346
340
347
341
public function getRenameTableSQL (string $ oldName , string $ newName ): string
@@ -362,10 +356,11 @@ private function gatherAlterColumnSQL(
362
356
ColumnDiff $ columnDiff ,
363
357
array &$ sql ,
364
358
array &$ queryParts ,
359
+ bool &$ needsReorg ,
365
360
): void {
366
- $ alterColumnClauses = $ this ->getAlterColumnClausesSQL ($ columnDiff );
361
+ $ alterColumnClauses = $ this ->getAlterColumnClausesSQL ($ columnDiff, $ needsReorg );
367
362
368
- if (empty ($ alterColumnClauses )) {
363
+ if (count ($ alterColumnClauses ) < 1 ) {
369
364
return ;
370
365
}
371
366
@@ -389,41 +384,56 @@ private function gatherAlterColumnSQL(
389
384
*
390
385
* @return string[]
391
386
*/
392
- private function getAlterColumnClausesSQL (ColumnDiff $ columnDiff ): array
387
+ private function getAlterColumnClausesSQL (ColumnDiff $ columnDiff, bool & $ needsReorg ): array
393
388
{
394
- $ newColumn = $ columnDiff ->getNewColumn ()->toArray ();
389
+ $ newColumn = $ columnDiff ->getNewColumn ();
390
+ $ columnArray = $ newColumn ->toArray ();
391
+
392
+ $ newName = $ columnDiff ->getNewColumn ()->getQuotedName ($ this );
393
+ $ oldName = $ columnDiff ->getOldColumn ()->getQuotedName ($ this );
395
394
396
- $ alterClause = 'ALTER COLUMN ' . $ columnDiff -> getNewColumn ()-> getQuotedName ( $ this ) ;
395
+ $ alterClause = 'ALTER COLUMN ' . $ newName ;
397
396
398
- if ($ newColumn ['columnDefinition ' ] !== null ) {
399
- return [$ alterClause . ' ' . $ newColumn ['columnDefinition ' ]];
397
+ if ($ newColumn ->getColumnDefinition () !== null ) {
398
+ $ needsReorg = true ;
399
+
400
+ return [$ alterClause . ' ' . $ newColumn ->getColumnDefinition ()];
400
401
}
401
402
402
403
$ clauses = [];
403
404
405
+ if ($ columnDiff ->hasNameChanged ()) {
406
+ $ clauses [] = 'RENAME COLUMN ' . $ oldName . ' TO ' . $ newName ;
407
+ }
408
+
404
409
if (
405
410
$ columnDiff ->hasTypeChanged () ||
406
411
$ columnDiff ->hasLengthChanged () ||
407
412
$ columnDiff ->hasPrecisionChanged () ||
408
413
$ columnDiff ->hasScaleChanged () ||
409
414
$ columnDiff ->hasFixedChanged ()
410
415
) {
411
- $ clauses [] = $ alterClause . ' SET DATA TYPE ' . $ newColumn ['type ' ]->getSQLDeclaration ($ newColumn , $ this );
416
+ $ needsReorg = true ;
417
+ $ clauses [] = $ alterClause . ' SET DATA TYPE ' . $ newColumn ->getType ()
418
+ ->getSQLDeclaration ($ columnArray , $ this );
412
419
}
413
420
414
421
if ($ columnDiff ->hasNotNullChanged ()) {
415
- $ clauses [] = $ newColumn ['notnull ' ] ? $ alterClause . ' SET NOT NULL ' : $ alterClause . ' DROP NOT NULL ' ;
422
+ $ needsReorg = true ;
423
+ $ clauses [] = $ newColumn ->getNotnull () ? $ alterClause . ' SET NOT NULL ' : $ alterClause . ' DROP NOT NULL ' ;
416
424
}
417
425
418
426
if ($ columnDiff ->hasDefaultChanged ()) {
419
- if (isset ( $ newColumn[ ' default ' ]) ) {
420
- $ defaultClause = $ this ->getDefaultValueDeclarationSQL ($ newColumn );
427
+ if ($ newColumn-> getDefault () !== null ) {
428
+ $ defaultClause = $ this ->getDefaultValueDeclarationSQL ($ columnArray );
421
429
422
430
if ($ defaultClause !== '' ) {
423
- $ clauses [] = $ alterClause . ' SET ' . $ defaultClause ;
431
+ $ needsReorg = true ;
432
+ $ clauses [] = $ alterClause . ' SET ' . $ defaultClause ;
424
433
}
425
434
} else {
426
- $ clauses [] = $ alterClause . ' DROP DEFAULT ' ;
435
+ $ needsReorg = true ;
436
+ $ clauses [] = $ alterClause . ' DROP DEFAULT ' ;
427
437
}
428
438
}
429
439
@@ -485,12 +495,12 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
485
495
*/
486
496
public function getDefaultValueDeclarationSQL (array $ column ): string
487
497
{
488
- if (! empty ($ column ['autoincrement ' ])) {
498
+ if (isset ($ column ['autoincrement ' ]) && $ column [ ' autoincrement ' ] === true ) {
489
499
return '' ;
490
500
}
491
501
492
- if (! empty ($ column ['version ' ])) {
493
- if (( string ) $ column ['type ' ] !== ' DateTime ' ) {
502
+ if (isset ($ column ['version ' ]) && $ column [ ' version ' ] === true ) {
503
+ if ($ column ['type ' ] instanceof DateTimeType ) {
494
504
$ column ['default ' ] = '1 ' ;
495
505
}
496
506
}
0 commit comments