22
22
use Doctrine \DBAL \Types \Types ;
23
23
use InvalidArgumentException ;
24
24
25
+ use function array_map ;
25
26
use function array_merge ;
26
27
use function array_unique ;
27
28
use function array_values ;
@@ -406,18 +407,20 @@ public function getAlterTableSQL(TableDiff $diff): array
406
407
$ tableNameSQL = $ table ->getQuotedName ($ this );
407
408
408
409
foreach ($ diff ->getChangedColumns () as $ columnDiff ) {
409
- $ newColumn = $ columnDiff ->getNewColumn ();
410
- $ newColumnName = $ newColumn ->getQuotedName ($ this );
410
+ $ newColumn = $ columnDiff ->getNewColumn ();
411
+ $ oldColumn = $ columnDiff ->getOldColumn ();
412
+ $ nameChanged = $ columnDiff ->hasNameChanged ();
411
413
412
- $ oldColumn = $ columnDiff ->getOldColumn ();
413
- $ oldColumnName = $ oldColumn ->getQuotedName ($ this );
414
- $ nameChanged = $ columnDiff ->hasNameChanged ();
415
-
416
- // Column names in SQL server are case insensitive and automatically uppercased on the server.
417
414
if ($ nameChanged ) {
415
+ // sp_rename accepts the old name as a qualified name, so it should be quoted.
416
+ $ oldColumnNameSQL = $ oldColumn ->getQuotedName ($ this );
417
+
418
+ // sp_rename accepts the new name as a literal value, so it cannot be quoted.
419
+ $ newColumnName = $ newColumn ->getName ();
420
+
418
421
$ sql = array_merge (
419
422
$ sql ,
420
- $ this ->getRenameColumnSQL ($ tableNameSQL , $ oldColumnName , $ newColumnName ),
423
+ $ this ->getRenameColumnSQL ($ tableNameSQL , $ oldColumnNameSQL , $ newColumnName ),
421
424
);
422
425
}
423
426
@@ -492,11 +495,7 @@ public function getAlterTableSQL(TableDiff $diff): array
492
495
493
496
public function getRenameTableSQL (string $ oldName , string $ newName ): string
494
497
{
495
- return sprintf (
496
- 'sp_rename %s, %s ' ,
497
- $ this ->quoteStringLiteral ($ oldName ),
498
- $ this ->quoteStringLiteral ($ newName ),
499
- );
498
+ return $ this ->getRenameSQL ($ oldName , $ newName );
500
499
}
501
500
502
501
/**
@@ -630,13 +629,7 @@ protected function getDropColumnCommentSQL(string $tableName, string $columnName
630
629
*/
631
630
protected function getRenameIndexSQL (string $ oldIndexName , Index $ index , string $ tableName ): array
632
631
{
633
- return [sprintf (
634
- "EXEC sp_rename N'%s.%s', N'%s', N'INDEX' " ,
635
- $ tableName ,
636
- $ oldIndexName ,
637
- $ index ->getQuotedName ($ this ),
638
- ),
639
- ];
632
+ return [$ this ->getRenameSQL ($ tableName . '. ' . $ oldIndexName , $ index ->getName (), 'INDEX ' )];
640
633
}
641
634
642
635
/**
@@ -650,12 +643,18 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
650
643
*/
651
644
protected function getRenameColumnSQL (string $ tableName , string $ oldColumnName , string $ newColumnName ): array
652
645
{
653
- return [sprintf (
654
- "EXEC sp_rename %s, %s, 'COLUMN' " ,
655
- $ this ->quoteStringLiteral ($ tableName . '. ' . $ oldColumnName ),
656
- $ this ->quoteStringLiteral ($ newColumnName ),
657
- ),
658
- ];
646
+ return [$ this ->getRenameSQL ($ tableName . '. ' . $ oldColumnName , $ newColumnName )];
647
+ }
648
+
649
+ /**
650
+ * Returns the SQL statement that will execute sp_rename with the given arguments.
651
+ */
652
+ private function getRenameSQL (string ...$ arguments ): string
653
+ {
654
+ return 'EXEC sp_rename '
655
+ . implode (', ' , array_map (function (string $ argument ): string {
656
+ return 'N ' . $ this ->quoteStringLiteral ($ argument );
657
+ }, $ arguments ));
659
658
}
660
659
661
660
/**
0 commit comments