Problem/Motivation

The values returned by ::execute() DML methods (INSERT, UPDATE, UPSERT, DELETE, MERGE) and from the TRUNCATE DDL method are inconsistent and fragile.

See discussions in #3567868: \Drupal\Core\Database\Query\Update::execute has wrong documentation for return type and #2547493: Add support for unique / primary key constraints composed of multiple fields for Upsert queries.

Proposed resolution

For UPSERT, returning a value from Upsert::execute() seems irrelevant, also. In core, the returned value seems only used in tests, where we can check results of the operation in alternative ways.

  1. Deprecate UPSERT ::execute() returning a value, will implement QueryExecutionVoidInterface in D13
  2. Allow returning the current rowcount with an opt-in toggle that will throw the deprecation
  3. Adjust consuming code not to rely on the returned value

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3569213

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Status: Active » Needs review
ghost of drupal past’s picture

This is not necessary. We have solid affected rows support everywhere. Yes in upsert too see my comments there.

Yes. This is a repeated comment from #3568486: [meta] Change return of ::execute() DML/DDL methods (INSERT, UPDATE, UPSERT, DELETE, MERGE, TRUNCATE) to void but since it got ignored I need to repeat it.

#3567868-17: \Drupal\Core\Database\Query\Update::execute has wrong documentation for return type was also ignored. Let me copy-paste that too

Could you please direct me to the documentation for [numbers may be impacted by concurrent operations]? I couldn't find it in the PostgreSQL docs.

I am trying not to be just a nuisance here. I know I have not been the most cooperativee in the upsert issue but when you stated you are not interested in my idea I turned to the MR and I am trying to improve it as much as I can. If there really is an affected rows problem then yes please stop relying on it but the IS only says "see discussions" but I do not see anything. Care to point where the problems are actually shown?

andypost’s picture

Status: Needs review » Needs work
Related issues: +#3575577: Add [#NoDiscard] to database methods

Yes, this methods must return value as all databases I know return number of affected rows (sometimes even for DDL)

Moreover I'm trying to add attribute to this methods to make sure the result is used via #3575577: Add [#NoDiscard] to database methods

mondrake’s picture

Title: Deprecate usage of UPSERT ::execute() method » Deprecate UPSERT ::execute() method returning a value
Issue summary: View changes
mondrake’s picture

Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record updates

Looks good to me.
Just missing a test that Upsert->execute() returns NULL.
The CR needs to be updated.

mondrake’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record updates

rebased, made some fixes based on reviews (thanks!), fleshed out the CR.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.
The IS and the CR are in order.
There is enough testing.
For me it is RTBC.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community

rebased after removal of the shortcut module

longwave’s picture

Status: Reviewed & tested by the community » Needs work

Can we name the flag after what it does, something like return_affected_rows, the current name is confusing when you read it out of context.

I wish there was a better way to do this, but I don't see one. We could use attributes on the calling method instead, but that's just something else to be removed later. Technically you could parse the code and look to see if the result is passed to something or assigned to a variable, but that's way too expensive at runtime.

mondrake’s picture

Status: Needs work » Needs review

Thanks for input, done.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The remark of @longwave has been addressed.
Back to RTBC.

mondrake’s picture

Updated CR.

longwave’s picture

Status: Reviewed & tested by the community » Needs review

This will conflict with #3575577: Add [#NoDiscard] to database methods as noted in #5 - @mondrake @daffie please can you answer the questions from @ghost of drupal past and @andypost here as to why this is necessary?

mondrake’s picture

In general, I do not think it's necessary for calling code to inspect the returned value. When you upsert, and it succeeds, the result is that at the end all the records exists, either because they are updated or because they are added. If the upsert fails, conversely, it's going to throw an exception for the error encountered, not return a partial rowcount.

In a specific case, #3586760-21: Use composite key Upsert queries in core, we already had to work around mysqli and pdo mysql returning different values for the affected rowcount.

mondrake’s picture

I think #3575577: Add [#NoDiscard] to database methods is a different thing than this, and the two things are independent. Please see #3575577-15: Add [#NoDiscard] to database methods.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The question of @longwave has been answered.
Back to RTBC.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community

rebased and bumped deprecation version to 12.1