Repository navigation
Conversation
`changeColumnQuery` and `renameColumnQuery` wrapped column names in literal quote characters instead of routing them through `quoteIdentifier()`, so a column name containing the dialect's identifier delimiter produced broken (and injectable) DDL. Every neighbouring identifier in the same methods was already escaped correctly. Both the previous name (`attrBefore`) and the new name are now escaped. Affected: mysql, mariadb (both `changeColumn` and `renameColumn`), ibmi (both), snowflake (`renameColumn`) and mssql (`renameColumn`). mssql `sp_rename` takes T-SQL string literals rather than identifiers, so its arguments go through `escapeString()`. snowflake and ibmi `renameColumnQuery` also emitted invalid SQL for perfectly benign input (single quotes / backticks as identifier delimiters, and a trailing column definition that `RENAME COLUMN` does not accept), so they now generate `RENAME COLUMN <old> TO <new>` like the other dialects. Finally, db2's `attributeToSQL` interpolated `attribute.comment` raw and unquoted. `createTableQuery` post-processes and escapes that fragment itself, but `addColumn`/`changeColumn` do not, so it is now escaped for those contexts, mirroring what postgres does. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughColumn alteration and rename query generators now use dialect-aware identifier escaping across several dialects. DB2 column comments are escaped in add/change contexts, and unit tests cover generated SQL and identifiers containing quote or backtick characters. ChangesColumn DDL generation
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/core/test/unit/dialects/db2/query-generator.test.js (1)
54-58: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winAdd coverage for the
addColumncontext.The implementation changed both
addColumnandchangeColumncomment handling, but this test only exerciseschangeColumn. Add an apostrophe-containing comment case foraddColumn(oraddColumnQuery) to prevent regressions in that path.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/test/unit/dialects/db2/query-generator.test.js` around lines 54 - 58, Add a separate apostrophe-containing column comment test for the addColumn/addColumnQuery context alongside the existing changeColumn case, asserting the generated SQL escapes the apostrophe correctly while preserving the current changeColumn coverage.packages/core/test/unit/query-generator/rename-column-query.test.ts (1)
3-6: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winCover apostrophe escaping in MSSQL
sp_renamearguments.The fixture tests identifier delimiters but never includes
', so the newescapeString()behavior could regress without failing this test. Add an MSSQL-specific case containing apostrophes in both old and new names, expecting doubled apostrophes in the generated string literals.Also applies to: 29-40
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/test/unit/query-generator/rename-column-query.test.ts` around lines 3 - 6, Extend the MSSQL-specific rename-column test around the existing injectedColumn and injectedNewColumn fixtures with apostrophes in both names, then assert that generated sp_rename string literals contain doubled apostrophes for each value. Keep the existing delimiter coverage and verify the escaped old and new names independently.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/core/test/unit/dialects/db2/query-generator.test.js`:
- Around line 54-58: Add a separate apostrophe-containing column comment test
for the addColumn/addColumnQuery context alongside the existing changeColumn
case, asserting the generated SQL escapes the apostrophe correctly while
preserving the current changeColumn coverage.
In `@packages/core/test/unit/query-generator/rename-column-query.test.ts`:
- Around line 3-6: Extend the MSSQL-specific rename-column test around the
existing injectedColumn and injectedNewColumn fixtures with apostrophes in both
names, then assert that generated sp_rename string literals contain doubled
apostrophes for each value. Keep the existing delimiter coverage and verify the
escaped old and new names independently.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: bfe9441b-0cfb-4687-96a3-617e79e72e0d
📒 Files selected for processing (9)
packages/core/test/unit/dialects/db2/query-generator.test.jspackages/core/test/unit/query-generator/change-column-query.test.tspackages/core/test/unit/query-generator/rename-column-query.test.tspackages/db2/src/query-generator.jspackages/ibmi/src/query-generator.jspackages/mariadb/src/query-generator.jspackages/mssql/src/query-generator.jspackages/mysql/src/query-generator.jspackages/snowflake/src/query-generator.js
Db2 does not accept an inline `COMMENT` clause in `ALTER TABLE ... ADD` or `ALTER TABLE ... ALTER COLUMN`, so `addColumn`/`changeColumn` generated SQL that db2 rejects for any column that has a comment. `attributeToSQL` now emits a standalone `COMMENT ON COLUMN <table>.<column> IS <comment>` statement for those two contexts, with the table and column quoted as identifiers and the comment escaped as a string literal, mirroring what postgres does. `changeColumnQuery` splits that statement back out of the `ALTER TABLE` rather than folding it into an `ALTER COLUMN` clause, which also fixes the case where the definition is a list (e.g. a type change plus a nullability change) and the comment used to be concatenated onto the stringified array. `createTableQuery` still does its own parsing and escaping of the fragment and is untouched: its output is byte-identical to before. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Added a second commit, What it does
-- before
ALTER TABLE "myTable" ADD "age" INTEGER COMMENT 'Test';
ALTER TABLE "myTable" ALTER COLUMN "age" SET DATA TYPE INTEGER COMMENT 'Test';
-- after
ALTER TABLE "myTable" ADD "age" INTEGER; COMMENT ON COLUMN "myTable"."age" IS 'Test';
ALTER TABLE "myTable" ALTER COLUMN "age" SET DATA TYPE INTEGER; COMMENT ON COLUMN "myTable"."age" IS 'Test';
-- before, for { type: INTEGER, allowNull: true, comment: 'Test' }
ALTER TABLE "myTable" ALTER COLUMN "age" SET DATA TYPE INTEGER,DROP NOT NULL COMMENT 'Test';
-- after
ALTER TABLE "myTable" ALTER COLUMN "age" SET DATA TYPE INTEGER ALTER COLUMN "age" DROP NOT NULL; COMMENT ON COLUMN "myTable"."age" IS 'Test';
Tests — six new cases in One thing for a reviewer to decide. This makes the SQL correct, but db2's I could not run this against a real db2 instance, so the Created by Opus 5 with Claude Code, supervised by @WikiRik. |
The previous commit made `addColumn`/`changeColumn` emit a standalone `COMMENT ON COLUMN` statement appended after the `ALTER TABLE`. The SQL is valid, but it never runs: `Db2Query` calls `connection.prepare(sql)`, and node-ibm_db's `prepare` only executes the first statement of the string it is given, discarding everything after the first `;` without an error (ibmdb/node-ibm_db#319). So `addColumn`/`changeColumn` with a comment succeeded and silently dropped the comment, which is worse than the behaviour before that commit, where Db2 at least rejected the inline `COMMENT` clause outright. The comment is now generated on its own by `Db2QueryGenerator#commentOnColumnQuery` and executed as a second query by `Db2QueryInterface#addColumn`/`#changeColumn`, rather than being folded into the `ALTER TABLE` string. `attributeToSQL` no longer produces a comment fragment for those two contexts at all, which removes the `startsWith(definition, 'COMMENT ON COLUMN ')` sniffing `changeColumnQuery` needed to pull the statement back out of the column definitions: that check ran over user-controlled comment text and had to be ordered ahead of the `REFERENCES` and `DROP ` branches to stay correct. The two statements are not wrapped in a transaction of their own, matching how `dropAllSchemas` and `addConstraint`/`executeTableReorg` already issue follow-up queries in this class. They are passed the caller's `options`, so they do share a transaction when one is supplied. `createTableQuery` is untouched and still renders comments as an inert `-- ...` line comment, so it continues not to apply them; the regression test pinning its output is unchanged. Fixing that is left as a follow-up. Verified against Db2 12.1.4.0 by reading back `SYSCAT.COLUMNS.REMARKS`: comments set by `addColumn` and `changeColumn` (including the type + nullability case) are now stored, a comment containing a single quote round-trips byte for byte without being injectable, and the same operations on the parent commit leave `REMARKS` null. The `should change the comment of column` integration test, which was skipped for Db2 because this did not work, is enabled and passes. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Added a third commit, The problem
What changedThe comment no longer travels with the Where I put the split, and why. Of the three options, I moved the comment out of the generated string entirely rather than having the QueryInterface split it back apart:
Transactionality. The two statements are not wrapped in a transaction of their own; they are passed the caller's
Verification against a real Db2Run against Lines 6a–6c are the important ones, and they pin down all three behaviours on the same table:
Line 3 is the escaping case: a comment containing Tests
The unit tests from the previous commit were rewritten for the new shape: Ran: Remaining limitations
Created by Opus 5 with Claude Code, supervised by @WikiRik. |
Issue COMMENT ON COLUMN from Db2QueryInterface so Query._run does not re-parse SQL, matching the addColumn path in sequelize#18288.
Issue COMMENT ON COLUMN from Db2QueryInterface so Query._run does not re-parse SQL, matching the addColumn path in sequelize#18288.
What & why
changeColumnQueryandrenameColumnQueryhand-wrapped column names in literal quote characters (`,",') instead of routing them throughthis.quoteIdentifier(). A column name containing the dialect's identifier delimiter therefore escapes its own quoting and injects arbitrary DDL into the generated statement. Every neighbouring identifier in the same methods — includingaddColumnQueryright above them — was already escaped correctly, which is what makes this a bug rather than a deliberate choice.Example, before this PR (mysql, column name
id` INT, ADD COLUMN `pwned):Dialect status
changeColumnrenameColumnquoteIdentifierdoubles]sp_rename)The "already safe" entries were checked explicitly so a reviewer does not have to re-derive them; they are deliberately left untouched.
Notes worth a reviewer's attention
attrBeforeis escaped too. A fix that only covers the new column name would be incomplete — the previous name flows into the same statement on every affected dialect.packages/mariadb/src/query-generator.jsandpackages/mysql/src/query-generator.jsboth had to change; patching one is not enough.escapeString(), notquoteIdentifier().sp_renametakes T-SQL string literals, so its arguments are escaped as literals. The object name is still built from quoted identifiers (N'[myTable].[oldName]'); the new name must stay unqualified, matching whatrenameTableQueryalready does.renameColumnQueryemitted invalid SQL even for benign input — snowflake quoted identifiers with', ibmi with`, and both appended a column definition thatRENAME COLUMNdoes not accept. Rather than escaping what was there, both now generateALTER TABLE <t> RENAME COLUMN <old> TO <new>like db2/postgres/oracle. ibmi also no longer emits a trailing;, consistent with the rest of that dialect's generator. This is a correctness fix bundled with the escaping fix; it changes the generated SQL for these two dialects.attributeToSQLinterpolatedattribute.commentraw and unquoted.createTableQueryextracts and escapes that fragment itself, butaddColumn/changeColumndo not, so the raw text reached the generated SQL. It is now escaped in those two contexts, mirroring how postgres handles the same split.createTableQueryoutput is unchanged.Tests
New per-dialect assertions pinning the escaped output — this class of bug recurs precisely because nothing pinned it:
packages/core/test/unit/query-generator/change-column-query.test.tspackages/core/test/unit/query-generator/rename-column-query.test.tsEach file has a benign case (which pins the snowflake/ibmi SQL shape) and an injection case using a column name containing
`,"and]— every dialect's identifier delimiter at once — covering bothattrBeforeand the new name. The db2 comment case is pinned inpackages/core/test/unit/dialects/db2/query-generator.test.js.Verification
Unit tests only, no databases. For each of mysql, mariadb, mssql, db2, ibmi, snowflake, postgres and oracle: the two new files plus
test/unit/sql/change-column.test.js,create-table-query,add-column-query,rename-table-queryand the dialect-specificquery-generator.test.js. All green.eslintandprettierclean on the changed files. The full matrix is left to CI.Deliberately out of scope
attributeToSQLhas the same rawCOMMENT ${attribute.comment}interpolation as db2 did, but every mssql caller consumes and escapes that marker, so it never reaches the generated SQL. Left alone.ALTER TABLE ... RENAME COLUMN, yet the loop still builds a comma-joined list. That predates this PR (and the callers only ever pass one column); flagging it as a known remaining limitation rather than changing it here.COMMENTextraction regexes in db2/mssqlcreateTableQueryare still confused by a comment containing the wordCOMMENT. Pre-existing, unrelated to escaping.Created by Opus 5 with Claude Code, supervised by @WikiRik.
@coderabbitai review
Summary by CodeRabbit