Skip to content

fix(mssql): change DATEONLY to parse the value as utc - #11409

Closed
dead wants to merge 1 commit into
sequelize:masterfrom
dead:fix/mssql-dateonly
Closed

dead wants to merge 1 commit into
sequelize:masterfrom
dead:fix/mssql-dateonly

Conversation

@dead

@dead dead commented Sep 9, 2019 •

Copy link
Copy Markdown

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Fix #10982 - Removed the DATAONLY extended class from MSSQL dialect.

@dead

dead commented Sep 9, 2019

Copy link
Copy Markdown
Author

@papb
Two tests stopped working in the appveyor build.
Not sure how to proceed... I'm unable to run npm run test-mssql on my machine, for some reason it is getting timeout in the getAssociations test.

  1) [MSSQL] BelongsToMany
       getAssociations
         supports transactions:
     Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\Gustavo\Desktop\sequelize\test\integration\associations\belongs-to-many.test.js)


  2) "after each" hook for "supports transactions":
     Error: Expected 0 running queries. 1 queries still running in [MSSQL] BelongsToMany getAssociations supports transactions
      at Context.<anonymous> (test\integration\support.js:24:9)

@papb

papb commented Sep 9, 2019

Copy link
Copy Markdown
Member

@dead Looks like the errors you posted aren't the same from the PR build?

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug DEPRECATED: replace with the "bug" issue type labels Sep 9, 2019
@dead
dead force-pushed the fix/mssql-dateonly branch from 16df58b to a1d72e6 Compare September 11, 2019 14:23
@codecov

codecov Bot commented Sep 11, 2019

Copy link
Copy Markdown

Codecov Report

Merging #11409 into master will decrease coverage by 2.91%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11409      +/-   ##
==========================================
- Coverage   92.49%   89.58%   -2.92%     
==========================================
  Files          91       92       +1     
  Lines        8866     9054     +188     
==========================================
- Hits         8201     8111      -90     
- Misses        665      943     +278
Impacted Files Coverage Δ
lib/dialects/mssql/data-types.js 29.62% <ø> (-70.38%) ⬇️
lib/dialects/mssql/query-generator.js 2.63% <0%> (-93.28%) ⬇️
lib/dialects/mssql/query-interface.js 8.69% <0%> (-91.31%) ⬇️
lib/dialects/mssql/query.js 5.58% <0%> (-89.95%) ⬇️
lib/dialects/mssql/resource-lock.js 22.22% <0%> (-77.78%) ⬇️
...dialects/abstract/query-generator/helpers/quote.js 93.33% <0%> (-6.67%) ⬇️
lib/dialects/abstract/query-generator.js 93.8% <0%> (-3.19%) ⬇️
lib/model.js 96.4% <0%> (-0.07%) ⬇️
lib/dialects/mssql/connection-manager.js
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d5e4a10...a1d72e6. Read the comment docs.

@papb

papb commented Sep 12, 2019

Copy link
Copy Markdown
Member

Hi @dead, I'm confused... You said the following in a comment in the issue:

@papb
Test 1 - It worked.
Test 2 - Didn't work, the parsed date was in invalid format.

However your PR does not implement my first suggestion. Why?

@dead

dead commented Sep 12, 2019

Copy link
Copy Markdown
Author

@papb
Sorry for the delayed response.
Yeah, that worked in my use case, but I didn't check for the output type as the tests do. (I was unable to run tests at that time, now I'm able to run it locally)

If you have any other suggestion than using moment.utc and want me to try feel free to ask.

Btw, not sure how to create a test for this. I was able to reproduce the error running the current data-types test with a custom timezone DIALECT=mssql TZ=America/Sao_Paulo mocha --require scripts/mocha-bootload --globals setImmediate,clearImmediate --exit --check-leaks --colors -t 30000 --reporter spec "test/integration/data-types.test.js

@papb

papb commented Sep 12, 2019

Copy link
Copy Markdown
Member

Thanks! The current solution is fine then.

  • Please edit the PR title and first post to reflect the actual change by this PR
  • As for the test, I'm not sure. Please try to find a way to store several dates on the DB and make sure that they all come back equal to the value given in the beginning, even if switching timezones. Probably not so trivial indeed, but no rush. Take your time, thanks again for the help!

@dead dead changed the title fix(mssql): removed the extended DATEONLY class from mssql dialect fix(mssql): change DATEONLY to parse the value as utc Sep 16, 2019
@krazi3

krazi3 commented Oct 7, 2019

Copy link
Copy Markdown
Contributor

@papb @dead This UTC issue with DATEONLY is also occurring for mysql test cases

@emmiranda

Copy link
Copy Markdown

@papb
@dead

This commit solved my problem, in my case. It makes sense to convert without UTC to an attribute that is only date.

class DATEONLY extends BaseTypes.DATEONLY {
    static parse(value) {
      return moment(value).utc(false).format('YYYY-MM-DD');
    }
  }

Using DATEONLY, the date was recorded correctly, for example '2020-5-1'. However, when consulting the object, it resulted in '2020-4-1' (always one day less)
Something important is that I am using timezone setting "-03: 00" (I'm in Brazil).
timezone: '-03:00'

For now I had to change the original file inside node_modules.(\node_modules\sequelize\lib\dialects\mssql\data-types.js )

Could this pull request be approved to have this correction in a next version?

@xjpro

xjpro commented Feb 17, 2020

Copy link
Copy Markdown

👍 This fixes an issue for me. How can we get it merged in?

@bradwallen

Copy link
Copy Markdown

I second @xjpro, this fixes a problem for my team as well.

@requius

requius commented Jun 16, 2020

Copy link
Copy Markdown

This fix all my problems with mssql and Sequelize.DATEONLY.

@xjpro

xjpro commented Jul 7, 2020 •

Copy link
Copy Markdown

@tonyguinta

tonyguinta commented Oct 5, 2020 •

Copy link
Copy Markdown

I just realized that the code posted above by @emmiranda is slightly different than the code in this pull request. I have confirmed that the following from @emmiranda does indeed work:

class DATEONLY extends BaseTypes.DATEONLY {
    static parse(value) {
      return moment(value).utc(false).format('YYYY-MM-DD');
    }
  }

I have also successfully tested the code in this pull request from @dead, which is:

class DATEONLY extends BaseTypes.DATEONLY {
    static parse(value) {
      return moment.utc(value).format('YYYY-MM-DD');
    }
  }

I have successfully used patch-package to implement the code from this pull request as a workaround for now.

@tonyguinta tonyguinta mentioned this pull request Oct 5, 2020
2 of 7 tasks

@tonyguinta tonyguinta left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[comment deleted]

@tonyguinta tonyguinta left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have confirmed that this code solves issue 10982.

@spearmootz

spearmootz commented Nov 12, 2020 •

Copy link
Copy Markdown
Contributor

I have also tested this independently in version 5.22.3

@spearmootz

Copy link
Copy Markdown
Contributor

Meanwhile i have done the following and this will overwrite the parsing function to work correctly.

const moment = require('moment');
const sequelize = require('sequelize');

sequelize.DataTypes.mssql.DATEONLY.parse = function (value) {
return moment.utc(value).format('YYYY-MM-DD');
};

@quex46

quex46 commented Nov 17, 2020 •

Copy link
Copy Markdown

Meanwhile i have done the following and this will overwrite the parsing function to work correctly.

const moment = require('moment');
const sequelize = require('sequelize');

sequelize.DataTypes.mssql.DATEONLY.parse = function (value) {
return moment.utc(value).format('YYYY-MM-DD');
};

Yeah, it works well when we have proper dates in DB and just need to display it properly, but it also might need to patch _stringify and _sanitize methods as well. Otherwise, we will have (date-1d) set in DB in case of negative timezone. For example, if you set date to '2020-11-17' with America/New_York (-05:00) timezone set it will be '2020-11-16' in DB.

So complete solution for me is:

const moment = require('moment');
const sequelize = require('sequelize');

sequelize.DataTypes.mssql.DATEONLY.parse = function (value) {
  return moment.utc(value).format('YYYY-MM-DD');
};

sequelize.DataTypes.mssql.DATEONLY.prototype._stringify= function (value) {
  return moment.utc(value).format('YYYY-MM-DD');
};

sequelize.DataTypes.mssql.DATEONLY.prototype._sanitize= function (value) {
  if ((!options || (options && !options.raw)) && !!value) {
     return moment.utc(value).format('YYYY-MM-DD');
  }
  return value;
};

An alternative approach - set default timezone for moment module used by sequelize:

require("sequelize/node_modules/moment").tz.setDefault('UTC");

so, we won't need to patch any existing methods: internal sequelize moment(...) calls will work same as moment.utc(...)

Both approaches work for sequelize 6.3.5

@sdepold

sdepold commented Oct 24, 2021

Copy link
Copy Markdown
Member

Hey there :)

First of all: Thanks a bunch for your contribution to Sequelize! Much appreciated!
Second: Unfortunately we haven't had the chance to look into this ever since you created it. Sorry for that!

A couple of months ago, we have switched from master to main branch as our primary development branch and hence this PR is now outdated :(

If you still think this change is making sense, please consider recreating the PR against main. Thanks in advance and sorry for the additional work.

✌️

@github-actions github-actions Bot removed the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Oct 24, 2021
@dead dead closed this Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug DEPRECATED: replace with the "bug" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DATEONLY in MSSQL converted to localtime