Repository navigation
fix: virtual property handling in schema builder - #11000
Conversation
a95a772 to
0f0cba3
Compare
0f0cba3 to
04c5bc9
Compare
359c3dd to
04c5bc9
Compare
04c5bc9 to
d3e913b
Compare
There was a problem hiding this comment.
Pull Request Overview
Fixes virtual property handling in schema builder to prevent incorrect SQL generation when dropping columns. The issue occurred when virtual columns were being incorrectly included in drop column operations.
- Fixed virtual property filtering logic in RdbmsSchemaBuilder to properly exclude virtual columns from drop operations
- Added comprehensive test coverage reproducing the issue with migration SQL generation
- Created test entity with virtual column to validate the fix
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/schema-builder/RdbmsSchemaBuilder.ts | Fixed boolean logic for filtering virtual properties when dropping columns |
| test/github-issues/10999/entity/entity-with-virtual-column.ts | Added test entity with virtual column to reproduce the issue |
| test/github-issues/10999/issue-10999.ts | Added test case validating correct DROP/ADD column SQL generation |
| test/github-issues/10999/migration/1721739455264-init.ts | Added migration file for test setup |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
acb7bc2 to
cb9247f
Compare
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughTightens dropRemovedColumns logic in RdbmsSchemaBuilder to stop treating virtual properties as dropped. Adds tests for issue #10999: a new entity with a virtual column, an initial migration creating a real column Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant SB as SchemaBuilder
participant MD as EntityMetadata
participant DB as ExistingTable
SB->>MD: Load entity columns (virtual + physical)
SB->>DB: Load table columns
note right of SB: Compare table columns to metadata by databaseName
SB->>SB: Exclude virtual columns from "removed" candidates
alt physical column missing in MD
SB->>DB: Emit DROP COLUMN for that physical column
else only virtual column missing in MD
SB--xDB: Do not emit DROP (virtuals ignored)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (3)
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/schema-builder/RdbmsSchemaBuilder.ts (2)
322-357: renameColumns length check should ignore virtual propertiesThe early length check counts virtual properties and can incorrectly skip rename detection when a table has virtual columns. Compare only real (non‑virtual) columns and reuse the filtered list below.
Apply this diff:
- if (metadata.columns.length !== table.columns.length) continue + const metadataNonVirtualColumns = metadata.columns.filter( + (c) => !c.isVirtualProperty, + ) + if (metadataNonVirtualColumns.length !== table.columns.length) + continue @@ - const renamedMetadataColumns = metadata.columns - .filter((c) => !c.isVirtualProperty) - .filter((column) => { + const renamedMetadataColumns = metadataNonVirtualColumns.filter( + (column) => { return !table.columns.find((tableColumn) => { return ( tableColumn.name === column.databaseName && tableColumn.type === this.connection.driver.normalizeType(column) && tableColumn.isNullable === column.isNullable && tableColumn.isUnique === this.connection.driver.normalizeIsUnique(column) ) }) - }) + }, + ) @@ - const renamedTableColumns = table.columns.filter((tableColumn) => { - return !metadata.columns.find((column) => { + const renamedTableColumns = table.columns.filter((tableColumn) => { + return !metadataNonVirtualColumns.find((column) => { return ( - !column.isVirtualProperty && column.databaseName === tableColumn.name && this.connection.driver.normalizeType(column) === tableColumn.type && column.isNullable === tableColumn.isNullable && this.connection.driver.normalizeIsUnique(column) === tableColumn.isUnique ) }) })
783-791: Minor perf/readability: precompute real column names SetAvoid N×M scans by building a Set of non‑virtual metadata column names once per table.
Apply this diff:
- const droppedTableColumns = table.columns.filter((tableColumn) => { - return !metadata.columns.find( - (columnMetadata) => - !columnMetadata.isVirtualProperty && - columnMetadata.databaseName === tableColumn.name, - ) - }) + const realColumnNames = new Set( + metadata.columns + .filter((c) => !c.isVirtualProperty) + .map((c) => c.databaseName), + ) + const droppedTableColumns = table.columns.filter( + (tableColumn) => !realColumnNames.has(tableColumn.name), + )test/github-issues/10999/issue-10999.ts (1)
33-38: Make assertions resilient to query orderingRelying on index 0 is brittle. Search for the expected SQL instead.
Apply this diff:
- expect(sqlInMemory.upQueries[0].query).to.eql( - `ALTER TABLE "entity_with_virtual_column" DROP COLUMN "foo"`, - ) - expect(sqlInMemory.downQueries[0].query).to.eql( - `ALTER TABLE "entity_with_virtual_column" ADD "foo" integer NOT NULL`, - ) + const dropFoo = sqlInMemory.upQueries.find( + (q) => + q.query === + `ALTER TABLE "entity_with_virtual_column" DROP COLUMN "foo"`, + ) + expect(dropFoo, "expected DROP COLUMN foo").to.not.be.undefined + + const addFoo = sqlInMemory.downQueries.find( + (q) => + q.query === + `ALTER TABLE "entity_with_virtual_column" ADD "foo" integer NOT NULL`, + ) + expect(addFoo, "expected ADD COLUMN foo").to.not.be.undefined
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/schema-builder/RdbmsSchemaBuilder.ts(1 hunks)test/github-issues/10999/entity/entity-with-virtual-column.ts(1 hunks)test/github-issues/10999/issue-10999.ts(1 hunks)test/github-issues/10999/migration/1721739455264-init.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
test/github-issues/10999/entity/entity-with-virtual-column.ts (2)
src/decorator/columns/PrimaryGeneratedColumn.ts (1)
PrimaryGeneratedColumn(55-119)src/decorator/columns/VirtualColumn.ts (1)
VirtualColumn(22-74)
test/github-issues/10999/issue-10999.ts (1)
test/utils/test-utils.ts (2)
createTestingConnections(388-482)closeTestingConnections(487-499)
🔇 Additional comments (3)
src/schema-builder/RdbmsSchemaBuilder.ts (1)
784-790: Correctly excluding virtual properties from drop detection — LGTMThis fixes the bug where any virtual column could mask a real DB column from being dropped. Matches the pattern already used in addNewColumns.
test/github-issues/10999/migration/1721739455264-init.ts (1)
6-14: Migration looks correctCreates the initial table with a real column “foo” for the test setup; down drops the table. No issues.
test/github-issues/10999/entity/entity-with-virtual-column.ts (1)
12-24: VirtualColumn usage is appropriateGood use of alias in the query; no persistence mapping leakage. All good.
- correctly filter out virtual properties when dropping columns - add tests reproducing issue typeorm#10999 related to column drop SQL generation - ensure migration generates appropriate SQL for creating and dropping columns
cb9247f to
e4deb18
Compare
PR Code Suggestions ✨No code suggestions found for the PR. |
commit: |
PR Code Suggestions ✨No code suggestions found for the PR. |
PR Code Suggestions ✨No code suggestions found for the PR. |
This reverts commit 5101ff3.
PR Code Suggestions ✨No code suggestions found for the PR. |
PR Code Suggestions ✨No code suggestions found for the PR. |
--- Co-authored-by: gioboa <[email protected]>
--- Co-authored-by: gioboa <[email protected]>
--- Co-authored-by: gioboa <[email protected]>
Description of change
Fixes #10999
migration:generateCan't Generate a Drop SQL Command #10999 related to column drop SQL generationPull-Request Checklist
masterbranchnpm run formatto apply prettier formattingnpm run testpasses with this changeFixes #0000Summary by CodeRabbit
Bug Fixes
Tests