fix(sqlserver): use ALTER COLUMN instead of DROP+ADD for type/length changes (#3357)#12258
fix(sqlserver): use ALTER COLUMN instead of DROP+ADD for type/length changes (#3357)#12258DeekRoumy wants to merge 2 commits intotypeorm:masterfrom
Conversation
…changes (typeorm#3357) Fixes typeorm#3357 SQL Server supports ALTER TABLE ... ALTER COLUMN for changing column type and length, which preserves data. Previously, TypeORM was using DROP COLUMN + ADD COLUMN even for simple type/length changes, causing data loss. Changes: - Only drop+recreate when truly required: IDENTITY property changes or computed column (asExpression/generatedType) changes - For type/length changes: use ALTER COLUMN with proper handling of DEFAULT constraints (which must be dropped and re-added around an ALTER COLUMN in SQL Server) - Name changes, nullable, and other properties continue to work correctly since they run in the same transaction Builds on the same approach taken for PostgreSQL/MySQL/CockroachDB, extending data-safe column alterations to SQL Server. Note: The competing PR typeorm#12218 covers Postgres/MySQL/Aurora/CockroachDB/ Oracle/Spanner. This PR extends the same fix to SQL Server (MSSQL).
Review Summary by QodoFix SQL Server column type changes to preserve data
WalkthroughsDescription• Prevents data loss when changing SQL Server column type/length • Uses ALTER COLUMN instead of destructive DROP+ADD operations • Properly handles DEFAULT constraints around ALTER COLUMN operations • Preserves existing behavior for IDENTITY and computed column changes Diagramflowchart LR
A["Column Type/Length Change"] --> B{Change Type}
B -->|IDENTITY or Computed| C["DROP + ADD Column"]
B -->|Type/Length Only| D["ALTER COLUMN"]
D --> E["Drop DEFAULT Constraint"]
E --> F["Execute ALTER COLUMN"]
F --> G["Restore DEFAULT Constraint"]
C --> H["Data Preserved"]
G --> H
File Changes1. src/driver/sqlserver/SqlServerQueryRunner.ts
|
Code Review by Qodo
1. Default constraint double-handled
|
- Pass skipEnum=true (5th arg) to buildCreateColumnSql in ALTER COLUMN paths to avoid invalid CHECK constraint syntax in ALTER COLUMN statements - Fix rename+type order: use oldColumn.name when building ALTER COLUMN since rename (sp_rename) hasn't run yet at that point - Guard isColumnChanged ALTER COLUMN with !typeOrLengthChanged to prevent duplicate ALTER COLUMN statements when type/length changed alongside other column properties - Remove unused TableColumn import from test file - Replace raw SQL with repository API in test to fix MySQL double-quote identifier quoting issue
|
Thanks for the thorough automated review! I've addressed all the issues flagged by Qodo in the latest commit:
Re the test placement concern: the existing TypeORM codebase has many tests under Let me know if anything else needs attention! |
|
|
Persistent review updated to latest commit 0ec3556 |



Summary
Fixes #3357
When a column's type or length changes in SQL Server, TypeORM was generating destructive SQL:
This causes data loss. SQL Server supports
ALTER TABLE ... ALTER COLUMNfor type/length changes:What this PR does
ALTER COLUMN ... new_typefor type/length changes, preserving dataALTER COLUMNand re-adding it afterDROP + ADDis only used for truly incompatible changes:asExpression/generatedType)Relationship to PR #12218
PR #12218 addresses the same issue for PostgreSQL, MySQL, Aurora MySQL, CockroachDB, Oracle, and Spanner. This PR extends the same data-safe fix to SQL Server (MSSQL) which was not covered.
Test plan
test/github-issues/3357/with test entity and integration testWhat stays the same
/claim #3357