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

Throw exception from lastInsertId() if there is no identity value #4324

Merged
merged 1 commit into from
Jul 18, 2021

Conversation

BenMorel
Copy link
Contributor

@BenMorel BenMorel commented Oct 4, 2020

Q A
Type improvement
BC Break yes
Fixed issues N/A

Summary

Supersedes #3759 and #3368.

Proposed changes

This PR splits the existing lastInsertId(?string $name = null): string method into 2 methods:

  • getLastInsertId() : int|string
  • getSequenceNumber(string $name): int|string

Tests have been extended to cover the following scenarios:

  • getLastInsertId() on supported platforms
  • getLastInsertId() on supported platforms, on a fresh connection, i.e. with no ID available (NoIdentityValue exception)
  • getLastInsertId() on unsupported platforms (IdentityColumnsNotSupported exception)
  • getSequenceNumber() with a known sequence name on supported platforms
  • getSequenceNumber() with an unknown sequence name on supported platforms (SequenceDoesNotExist exception)
  • getSequenceNumber() on unsupported platforms (SequencesNotSupported exception)

@BenMorel BenMorel force-pushed the insert-id-sequence-number2 branch 3 times, most recently from 4330d28 to 83e4265 Compare October 5, 2020 13:16
@BenMorel
Copy link
Contributor Author

BenMorel commented Oct 5, 2020

@morozov Ready for discussion!

src/Connection.php Outdated Show resolved Hide resolved
src/Connection.php Outdated Show resolved Hide resolved
src/Connection.php Outdated Show resolved Hide resolved
src/Driver/PDO/Connection.php Outdated Show resolved Hide resolved
src/Driver/PDO/Connection.php Show resolved Hide resolved
src/Driver/PDO/Connection.php Outdated Show resolved Hide resolved
src/Driver/PDO/SQLSrv/Connection.php Outdated Show resolved Hide resolved
src/Driver/PDO/SQLite/Connection.php Outdated Show resolved Hide resolved
@BenMorel BenMorel force-pushed the insert-id-sequence-number2 branch 2 times, most recently from 7fa3308 to b6f1c06 Compare October 8, 2020 16:45
@BenMorel
Copy link
Contributor Author

@morozov I'd like to come back to this. Before diving again in this PR, should I retarget 3.0.x instead of master?

Copy link
Member

@morozov morozov left a comment

Choose a reason for hiding this comment

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

I'd like to come back to this.

That would be awesome!

Before diving again in this PR, should I retarget 3.0.x instead of master?

3.0.0 is already released.

src/Driver/Mysqli/Connection.php Outdated Show resolved Hide resolved
@BenMorel
Copy link
Contributor Author

3.0.0 is already released.

Oh wow, I didn't expect it anytime soon. I had quite a few things I would have liked to propose for 3.0, but looks like they'll have to wait for 4.0 now. Any rough ETA on this one? I mean, can we expect it in a reasonable timeframe like a year from now, or will we have to wait for multiple years like Doctrine 2 to 3?

@BenMorel
Copy link
Contributor Author

@morozov Ready for review again. The main points that you may want to have a look at are:

  • PDO driver-specific checks in Driver\PDO\Connection::getLastInsertId() and getSequenceNumber(), rather than creating a decorator for each PDO driver just to throw an exception
  • PGSQL-specific exception checks that we cannot move to the ExceptionConverter without modifying its signature somehow to accept a context (the same error code can mean 2 different things depending on the operation that was being performed when it was thrown); I think that adding these 2 checks right in the Driver\PDO\Connection class is a pragmatic way to avoid adding unnecessary complexity to the ExceptionConverter
  • Check for underlying driver exception in tests, as you don't want to introduce wrapper-level exceptions for identity / sequence errors, but do want to introduce driver-level exceptions to be used in tests
  • settle on whether an exception should be thrown when no identity value / sequence number is available (Throw exception from lastInsertId() if there is no identity value #4324 (comment))
  • and finally naming: I suggest using getLastInsertId() instead of lastInsertId(), which is already reflected in the code

@morozov
Copy link
Member

morozov commented Nov 18, 2020

@BenMorel thanks for the update. I've been short on time for OSS lately and I cannot promise I'll look at it immediately but I'll do as soon as I can.

psalm.xml Outdated Show resolved Hide resolved
src/Driver/PDO/Connection.php Outdated Show resolved Hide resolved
src/Driver/PDO/Connection.php Outdated Show resolved Hide resolved
src/Driver/PDO/Connection.php Outdated Show resolved Hide resolved
Base automatically changed from master to 4.0.x January 22, 2021 07:44
@morozov morozov force-pushed the insert-id-sequence-number2 branch from da1fd37 to b3db903 Compare June 26, 2021 16:20
@morozov
Copy link
Member

morozov commented Jun 27, 2021

@BenMorel, just FYI, I squashed and rebased your PR to accommodate the changes from #4691. This way, the scope of this PR boils down to changing the return type of lastInsertId() (we could probably leave the method unchanged) and improving error handling.

I have a couple more ideas on improving the code, will document later.

@morozov morozov linked an issue Jun 29, 2021 that may be closed by this pull request
@morozov morozov added this to the 4.0.0 milestone Jun 29, 2021
@morozov morozov force-pushed the insert-id-sequence-number2 branch 2 times, most recently from 48580a2 to f56c2c0 Compare July 18, 2021 18:47
@morozov morozov changed the title Extract getSequenceNumber() from lastInsertId() Throw exception from lastInsertId() if there is no identity value Jul 18, 2021
Co-authored-by: Sergei Morozov <morozov@tut.by>
@morozov morozov force-pushed the insert-id-sequence-number2 branch from f56c2c0 to a32a18c Compare July 18, 2021 18:59
@morozov
Copy link
Member

morozov commented Jul 18, 2021

@BenMorel I did some more cleanup, and with the reduced scope, I believe it's good to go. The part about exceptions could still use some improvement but it's orthogonal to this patch, but it's orthogonal to this patch and should be taken care of by #3166.

@morozov morozov merged commit 086f91b into doctrine:4.0.x Jul 18, 2021
@morozov
Copy link
Member

morozov commented Jul 18, 2021

Thanks for the patch!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2022
@morozov morozov removed this from the 4.0.0 milestone Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework Connection::lastInsertId()
2 participants