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

[5.6] Fix SQLite delete for complex delete statements #22298

Merged
merged 4 commits into from
Dec 6, 2017
Merged

[5.6] Fix SQLite delete for complex delete statements #22298

merged 4 commits into from
Dec 6, 2017

Conversation

abdumu
Copy link
Contributor

@abdumu abdumu commented Dec 4, 2017

Current SQLite delete implementation lack the support for complex statement like joins, or limits. This may lead to serious loss of data.

Current approach of SQLite delete:

> App\Post::latest('id')->limit(1)->delete();
=> 48 (deleted rows)

=> "query" => "delete from "posts""

new approach:

> App\Post::latest('id')->limit(1)->delete();
=> 1 (deleted rows)

=> "query" => "delete from "posts" where "rowid" in (select "posts"."rowid" from "posts" order by "id" desc limit 1)"

Also, this fixes the #22239 for SQLite.

@GrahamCampbell GrahamCampbell changed the title [5.5] Fix SQLite delete for complex delete statements [5.6] Fix SQLite delete for complex delete statements Dec 4, 2017
public function compileDelete(Builder $query)
{
// SQLite delete statment doesn't fully support complex keywords like joins ..
// We use select statment as a subquery to overcome this delimma.
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo

@deleugpn
Copy link
Contributor

deleugpn commented Dec 4, 2017

Cool solution.

Copy link
Member

@themsaid themsaid left a comment

Choose a reason for hiding this comment

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

Since it's sqlite it's pretty easy to add integration tests, please add a test in tests/Integration/Database with the cases this PR fixes.

// we use it in select sub-query and in delete where-in statement.
$selectSql = parent::compileSelect($query->select("{$query->from}.rowid"));

return trim(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this trim is needed

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.

5 participants