Skip to content

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

Merged
taylorotwell merged 4 commits into
laravel:masterfrom
abdumu:SQLiteDelete
Dec 6, 2017
Merged

[5.6] Fix SQLite delete for complex delete statements#22298
taylorotwell merged 4 commits into
laravel:masterfrom
abdumu:SQLiteDelete

Conversation

@abdumu

@abdumu abdumu commented Dec 4, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo

@deleugpn

deleugpn commented Dec 4, 2017

Copy link
Copy Markdown
Contributor

Cool solution.

@themsaid themsaid left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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