Skip to content

MSSQL dialect timestamp conflict - #7787

Closed
getglad wants to merge 14 commits into
sequelize:masterfrom
getglad:master
Closed

getglad wants to merge 14 commits into
sequelize:masterfrom
getglad:master

Conversation

@getglad

@getglad getglad commented Jun 13, 2017 •

Copy link
Copy Markdown

If dialect is MSSQL, use SYSDATETIMEOFFSET() instead of CURRENT_TIMESTAMP as the deletedAtDefaultValue

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)?
    Note: rpm run test-mssql reports 751 passing on the first step. There is a second step that wants to use the connection variables, but I am using the msnodesqlv8 + a connection string, and I'm not sure how to pass that into the tests.
  • Does your issue contain a link to 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)?
  • Have you added an entry under Future in the changelog?

NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.

Description of change

When sequelize sets the deletedAt time stamp in paranoid mode, it submits a time stamp in the below format:
... SET [deletedAt]=N'2017-06-13 11:35:41.000 +00:00'

When sequelize then runs findAll against the records, it submits
... WHERE ([table].[deletedAt] > CURRENT_TIMESTAMP OR [table].[deletedAt] IS NULL);

On MS SQL, CURRENT_TIMESTAMP returns:
2017-06-13 11:35:41.000

This results in returning all records.

While SYSDATETIMEOFFSET() returns:
2017-06-13 11:40:11.8163142 -00:00

This results in returning all the undeleted records.

It's a minor change, but the only use of CURRENT_TIMESTAMP I can find in the project.

Closes #7419

If dialect is MSSQL, use SYSDATETIMEOFFSET() instead of CURRENT_TIMESTAMP as the deletedAtDefaultValue
@codecov

codecov Bot commented Jun 13, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #7787 into master will decrease coverage by 0.06%.
The diff coverage is 100%.

@harshithkashyap harshithkashyap 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.

@getglad Thank you for the PR. Could you add a test which was failing earlier with paranoid mode?

@getglad

getglad commented Jun 15, 2017 •

Copy link
Copy Markdown
Author

@harshithkashyap I have submitted a test, but the paranoid flag on the model does not seem to alter the query output when I run it. Wouldn't mind getting an extra set of eyes on this.

In my application code I can have something like:

sequelize.define('User', {
  username: Sequelize.STRING,
}, {
  paranoid: true,
});

And my findAll queries produce:
SELECT [id], [username], [createdAt], [updatedAt], [deletedAt] FROM [Users] AS [User] WHERE ([User].[deletedAt] > SYSDATETIMEOFFSET() OR [User].[deletedAt] IS NULL);

Am I missing something in the test setup?

@getglad

getglad commented Jun 22, 2017

Copy link
Copy Markdown
Author

@harshithkashyap just thought I'd bump you - see if you had any input

@harshithkashyap

Copy link
Copy Markdown

@getglad

  1. We are not using mocha's TDD interface anymore. You'd need to use describe and it blocks for your tests.
  2. You could add an unit test for this function and assert the return value instead of asserting SQL query. SQL query assertions are done for methods from queryGenerator.
  3. Since you are modifying model.js, add an integration test similar to this.

SYSDATETIMEOFFSET() would return the datetime with timezone. Our MSSQL CI Appveyor is likely configured to be in UTC timezone. For the test, you can initialize sequelize to use a different timezone. Sequelize would insert deletedAt values with the above specified timezone. You can assert the number of records returned. Since we have specified timezone with sequelize, deletedAt column would have timezone embedded such as 2012-10-25 12:32:10.1474881 +10:00 and SYSDATETIMEOFFSET() would return 2007-04-30 13:10:02.0474381 +00:00 UTC datetimeoffset. This would perform datetime comparison considering the timezone and would return correct results.

@felixfbecker

Copy link
Copy Markdown
Contributor

Will review once build is green.

Comment thread lib/model.js Outdated
let deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;

deletedAtDefaultValue = deletedAtDefaultValue || { $or: { $gt: model.sequelize.literal('CURRENT_TIMESTAMP'), $eq: null } };
deletedAtDefaultValue = deletedAtDefaultValue || { $or: { $gt: this.sequelize.options.dialect === 'mssql' ? model.sequelize.literal('SYSDATETIMEOFFSET()') : model.sequelize.literal('CURRENT_TIMESTAMP'), $eq: null } };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would wrap this line

@getglad getglad Jun 23, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment thread test/integration/model/find.test.js Outdated
expect(users.length).to.be.eql(1);
});

if (dialect === 'sqlite')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please put braces for multiline statements.
Why is there different behaviour for SQLite?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will do on braces.

On SQLite:
The only difference is a .delay() after deleting a record. There appears to be some sort of race condition when deleting the record (https://travis-ci.org/sequelize/sequelize/builds/245954861) and immediately querying it back, which is what I was doing in my test. Only saw it on SQLite, but if I created 2 records, soft delete 1, and then findAll, I would get both records back. A brief delay seems to fix on SQLite, but sometimes caused an Error: Expected 0 running queries. 1 queries still running in [POSTGRES] Model constructor should allow the user to specify indexes in options, especially on Postgres test - thus only tested it on SQLite. It may be worth extending the delay some to get a more reliable result, but 500 seemed okay for my test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A timeout sounds very brittle, especially if you don't know why it appears and why it only appears only for SQLite. We have some flakiness in tests in general which are solved when restarting jobs, but if it happens consistently, it indicates some bug in the code...

@getglad getglad Jun 23, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm pretty sure it is just that the write (destory but with paranoid mode on) doesn't complete before the read (findAll) on SQLite. It isn't a timeout issue, just an opportunity for SQLite to fully register the change. All I meant to say was that lengthening the wait time reduces the brittleness, which I have done, so it should be strong from here on out. It may represent a problem with the SQLite syntax (honestly, I think it's just a virtue of SQLite itself), but this PR is to fix the syntax for MSSQL on reading over paranoid deleted rows. The SQLite fix in my test reliably resolves the issue for me over several tests.

The failure in the last travis ci build seems to be related to this test -

it('allows me to return values from a custom parse function', () => {
- far as I can tell unrelated to my PR.

Other failures that have popped up inconsistently, but I don't believe are related to this PR:

Thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All I'm saying is there is probably a better way to fix this than a timeout, a real application wouldn't do a timeout either. The fulfilled promise of the destroy query should mean that the query finished.
@sushantdhiman @janmeier

@stale

stale Bot commented Aug 28, 2017

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

@stale stale Bot closed this Sep 4, 2017
@sushantdhiman sushantdhiman reopened this Sep 5, 2017
@stale stale Bot removed the stale label Sep 5, 2017
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.

4 participants