fix: use ALTER COLUMN instead of drop/add for type/length changes to prevent data loss#12032
fix: use ALTER COLUMN instead of drop/add for type/length changes to prevent data loss#12032hruss-software wants to merge 4 commits intotypeorm:masterfrom
Conversation
…prevent data loss Previously, changing a column's type or length caused TypeORM to generate DROP COLUMN followed by ADD COLUMN statements, resulting in data loss. This change uses each database's native ALTER COLUMN syntax instead: - PostgreSQL: ALTER COLUMN TYPE ... USING - MySQL/MariaDB: ALTER TABLE CHANGE - SQL Server: ALTER TABLE ALTER COLUMN - Oracle: ALTER TABLE MODIFY - CockroachDB: ALTER COLUMN TYPE DROP+ADD is preserved only for genuinely destructive changes (array conversion, generated/identity column changes). Closes typeorm#3357
User descriptionProblemWhen changing a column's type or length (e.g., Reported in #3357 (97 👍, open since 2019). SolutionMove type/length changes from the destructive DROP+ADD code path to each database's native ALTER
DROP+ADD is preserved only for genuinely destructive changes:
Why this approach
TestAdded test case verifying:
Closes #3357 Description of changePull-Request Checklist
PR TypeBug fix Description
Diagram Walkthroughflowchart LR
A["Column Type/Length Change"] --> B{Change Type}
B -->|Type or Length| C["Use ALTER COLUMN"]
B -->|Array/Generated/Computed| D["Use DROP+ADD"]
C --> E["PostgreSQL: ALTER COLUMN TYPE USING"]
C --> F["MySQL: ALTER TABLE CHANGE"]
C --> G["SQL Server: ALTER TABLE ALTER COLUMN"]
C --> H["Oracle: ALTER TABLE MODIFY"]
C --> I["CockroachDB: ALTER COLUMN TYPE"]
E --> J["Data Preserved"]
F --> J
G --> J
H --> J
I --> J
D --> K["Column Recreated"]
|
| Relevant files | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Bug fix |
| ||||||||||
| Tests |
|
PR Code Suggestions ✨Latest suggestions up to c46a968
Previous suggestions✅ Suggestions up to commit 5417de9
Suggestions up to commit ffab028
Suggestions up to commit beb14e0
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
/claim #3357 |
Code Review by Qodo
1.
|
- Replace raw SQL with repository API for driver-portable queries - Move cleanup (executeMemoryDownSql, row deletion) to finally block so test artifacts are cleaned up even on early failure
Code Review by Qodo
1. PG type change default risk
|
…ures Address code review feedback: - Drop and recreate DEFAULT around ALTER COLUMN TYPE in PostgreSQL to prevent casting failures (mirrors existing enum type change pattern) - Add type conversion test case (varchar -> text) alongside length change - Add typeorm#3357 issue reference for traceability - Use repository API instead of raw SQL for cross-driver portability - Match existing test cleanup patterns
Code Review by Qodo
1. Postgres enum ALTER TYPE wrong
|
Problem
When changing a column's type or length (e.g.,
varchar(50)→varchar(100)), TypeORM generatesdestructive
DROP COLUMN+ADD COLUMNstatements instead ofALTER COLUMN. This causescomplete data loss for the affected column.
Reported in #3357 (97 👍, open since 2019).
Solution
Move type/length changes from the destructive DROP+ADD code path to each database's native ALTER
COLUMN syntax, which preserves existing data:
ALTER COLUMN "col" TYPE <type> USING "col"::<type>ALTER TABLE CHANGE(already existed in codebase)ALTER TABLE ALTER COLUMN(already existed in codebase)ALTER TABLE MODIFY(already existed in codebase)ALTER COLUMN "col" TYPE <type>DROP+ADD is preserved only for genuinely destructive changes:
isArraychanges)Why this approach
changes; this routes type/length through them
int→text)silently dropping data
Test
Added test case verifying:
changeColumnexecuteMemoryDownSqlCloses #3357
Description of change
Pull-Request Checklist
masterbranchFixes #00000tests/**.test.ts)docs/docs/**.md)