Skip to content

feat(sqlite/query-generator): sqlite delete supporting limit via rowid - #8494

Merged
sushantdhiman merged 2 commits into
sequelize:masterfrom
netsgnut:sqlite-delete-limit
Oct 23, 2017
Merged

sushantdhiman merged 2 commits into
sequelize:masterfrom
netsgnut:sqlite-delete-limit

Conversation

@netsgnut

Copy link
Copy Markdown
Contributor

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 follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

By default SQLite is compiled with SQLITE_ENABLE_UPDATE_DELETE_LIMIT off. That means the DELETE statements would not support an optional LIMIT clause. By using DELETE ... WHERE rowid IN (SELECT rowid FROM ... WHERE ... LIMIT n) instead it can provide support in environments without such feature.

(This is a companion PR to #8450.)

@netsgnut
netsgnut force-pushed the sqlite-delete-limit branch from 13488e7 to 2eb8dc1 Compare October 17, 2017 09:04
@codecov

codecov Bot commented Oct 17, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #8494 into master will increase coverage by <.01%.
The diff coverage is 100%.

@netsgnut
netsgnut force-pushed the sqlite-delete-limit branch from 2eb8dc1 to 83f2aa7 Compare October 17, 2017 09:51
@eseliger

Copy link
Copy Markdown
Member

@netsgnut Some tests fail, but overall looks good to me

@netsgnut
netsgnut force-pushed the sqlite-delete-limit branch from 83f2aa7 to f254ddb Compare October 17, 2017 10:47
@netsgnut

netsgnut commented Oct 17, 2017 •

Copy link
Copy Markdown
Contributor Author

@eseliger, thanks for the heads-up. I think the tests should be fine now with f254ddb.

Please see if there are other cases I have missed as well. Thanks.

@eseliger

Copy link
Copy Markdown
Member

How about a integration test that verifies the functionality?

@eseliger
eseliger requested review from a team, janmeier and sushantdhiman October 17, 2017 10:57
@netsgnut

netsgnut commented Oct 17, 2017 •

Copy link
Copy Markdown
Contributor Author

How about a integration test that verifies the functionality?

Sounds like a good idea, although frankly I am not sure how to implement a proper integration test apart from the modified test cases...

@sushantdhiman sushantdhiman left a comment

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.

Dont set limit if its undefined.

You need to add a unit test for both cases

  1. limit is set, rowId based query
  2. limit is not set, old delete query

Comment thread lib/dialects/sqlite/query-generator.js Outdated
if (options.limit) {
return `DELETE FROM ${this.quoteTable(tableName)} WHERE rowid IN (SELECT rowid FROM ${this.quoteTable(tableName)}${whereClause} LIMIT ${this.escape(options.limit)})`;
} else {
return `DELETE FROM ${this.quoteTable(tableName)}${whereClause}`;

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.

This case will never run, if limit is undefined you are setting is to 1, which will always trigger if case.

I suggest just remove options.limit/isUndefined part, switch query only when limit is supplied by user

@netsgnut netsgnut Oct 22, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK. Although I am still curious on why we would need the options.limit/isUndefined part from dialect/mysql.js, I guess we really should drop the conditional here. Updating it.

That said, is there a case where limit is 0, though? If so that would be a case that triggers the else clause.

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.

In that case it would be better to check for isDefined in if expression, because we want to limit DELETE even if its set to 0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In that case it would be better to check for isDefined in if expression, because we want to limit DELETE even if its set to 0.

Do you want that to reflect on other dialects as well? Because I am afraid this is not how the current implementation for all other dialects is. That includes MySQL, PostgreSQL and MSSQL.

@netsgnut
netsgnut force-pushed the sqlite-delete-limit branch from 16cab14 to 9366da0 Compare October 22, 2017 13:57
By default SQLite is compiled with SQLITE_ENABLE_UPDATE_DELETE_LIMIT
off. That means the DELETE statements would not support an optional
LIMIT clause. By using `DELETE ... WHERE rowid IN (SELECT rowid FROM
... WHERE ... LIMIT n)` instead it can provide support in environments
without such feature.
@netsgnut
netsgnut force-pushed the sqlite-delete-limit branch from 9366da0 to 3001741 Compare October 22, 2017 14:10
@netsgnut

Copy link
Copy Markdown
Contributor Author

I have updated the PR for a little refactoring of the code (3001741), but on second thought in order to maintain the consistency with other implementations here I did not change much of the logic.

Regarding @sushantdhiman's comments:

Dont set limit if its undefined.

This is essential for the SQLite implementation to behave like other dialects. Without setting the limit the delete when the primary key has a different field name test would fail.

You need to add a unit test for both cases

  1. limit is set, rowId based query
  2. limit is not set, old delete query

The cases are already covered with the current test delete without limit (Case 1) and delete with limit (Case 2), but I added the SQLite-specific line for Case 1 as well just to be explicit - it works without it as the default line already captures it.


That said, I think there should be a standard behavior defined for all the dialects facing the common cases where the limit is not given, 0, as well as null. Those, IMHO, should go in another PR as it potentially changes/breaks current usage. If that behavior is desired, I am happy to make another PR for that including the necessary documentation change - given there is a consensus on how their behavior should be.

@netsgnut

Copy link
Copy Markdown
Contributor Author

(Not sure why the AppVeyor build failed though for 3001741...)

@sushantdhiman

Copy link
Copy Markdown
Contributor

@netsgnut Its clear that if you don't supply limit, whatever the where condition may be code will end up deleting only first row. No matter how I process it, it feels wrong.

Investigated and found this line has been saving us till now.

I agree changing other dialect is not a part of this PR, but this is a potential mishap in waiting.

This code section is old so I dont know the reason behind this, probably implementer(s) just copied code from first dialect to others.

Since we default to limit null and we have a safe guard test (unit) against this issue I am ready to move forward with logic adopted from other dialects

@sushantdhiman
sushantdhiman merged commit bf837ec into sequelize:master Oct 23, 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