Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typeCast): ensure the same behavior for field.string() with query and execute #2820

Merged
merged 2 commits into from
Jul 1, 2024

Conversation

wellwelwel
Copy link
Sponsor Collaborator

@wellwelwel wellwelwel commented Jul 1, 2024

This PR fixes a breaking change reported in Sequelize (sequelize/sequelize#17141) after introducing typeCast for execute method.

Following the same approach of PR #2398, this PR ensures that the typeCast usage for both query and execute methods are strictly equivalent when using field.string().

This isn't a new feature, but a part that was missed in #2398.

To illustrate it better:

// typeCast: (field) => field.string()

assert.strictEqual(query.date, execute.date, 'DATE');
assert.strictEqual(query.time, execute.time, 'TIME');
assert.strictEqual(query.datetime, execute.datetime, 'DATETIME');
assert.strictEqual(query.timestamp, execute.timestamp, 'TIMESTAMP');
assert.strictEqual(query.tiny.signed, execute.tiny.signed, 'TINY (signed)');
assert.strictEqual(query.tiny.unsigned, execute.tiny.unsigned, 'TINY (unsigned)');
  • Other column types didn't show issues with field.string().

From Sequelize

For everyone that uses the bind option, MySQL2 3.9.0 causes a break changing, even if the user doesn't use typeCast:

repro.js
import { Sequelize } from 'sequelize';

const sequelize = new Sequelize('test', 'root', '', {
  host: 'localhost',
  dialect: 'mysql',
  logging: false,
});

const [now] = await sequelize.query('SELECT NOW() as date WHERE 1 = $value;', {
  type: Sequelize.QueryTypes.SELECT,
  bind: {
    value: 1,
  },
});

const [custom] = await sequelize.query(
  'SELECT STR_TO_DATE("2022-06-28 12:34:56", "%Y-%m-%d %H:%i:%s") as date WHERE 1 = $value;',
  {
    type: Sequelize.QueryTypes.SELECT,
    bind: {
      value: 1,
    },
  }
);

const [date] = await sequelize.query(
  'SELECT DATE("2022-06-28") as string WHERE 1 = $value;',
  {
    type: Sequelize.QueryTypes.SELECT,
    bind: {
      value: 1,
    },
  }
);

await sequelize.close();

console.log('Now:', now.date);
console.log('Date:', custom.date);
console.log('Date (as string):', date.string);

Output:

Screenshot 2024-06-30 at 19 17 36
  • There is no possibility for Sequelize users to workaround this while using bind.

From MySQL2

To get the same behavior with MySQL2, just use field.string from typeCast with specific column types:

Current test of this PR

Screenshot 2024-06-30 at 21 14 51

After this PR 🧙🏻

Sequelize + bind option:

Screenshot 2024-06-30 at 19 17 46

MySQL2:

Screenshot 2024-06-30 at 21 15 08

Note

Copy link

codecov bot commented Jul 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.34%. Comparing base (a14f354) to head (6922d63).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2820      +/-   ##
==========================================
+ Coverage   90.33%   90.34%   +0.01%     
==========================================
  Files          71       71              
  Lines       15731    15749      +18     
  Branches     1344     1350       +6     
==========================================
+ Hits        14210    14228      +18     
  Misses       1521     1521              
Flag Coverage Δ
compression-0 90.34% <100.00%> (+0.01%) ⬆️
compression-1 90.34% <100.00%> (+0.01%) ⬆️
tls-0 89.86% <100.00%> (+0.01%) ⬆️
tls-1 90.14% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wellwelwel wellwelwel marked this pull request as ready for review July 1, 2024 00:33
@wellwelwel wellwelwel merged commit 27e38ea into sidorares:master Jul 1, 2024
68 checks passed
@wellwelwel wellwelwel deleted the field-string branch July 1, 2024 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant