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

Validate single row count for Row.Scan #460

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stevenh
Copy link

@stevenh stevenh commented Oct 13, 2018

Validate that a single row only is returned by queries used for Row.Scan.

This avoids unexpected results when the query has an issue such as a missing join criteria or limit in conjunction with functions which expect only on row returned e.g. Get(...).

Also:

  • Fixed missing \n's for test output of ConnectAll.

@stevenh stevenh force-pushed the get-multi-rows branch 2 times, most recently from f8641fc to 3def8f8 Compare October 13, 2018 01:01
@coveralls
Copy link

coveralls commented Oct 13, 2018

Pull Request Test Coverage Report for Build 53

  • 4 of 6 (66.67%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 71.761%

Changes Missing Coverage Covered Lines Changed/Added Lines %
sqlx.go 4 6 66.67%
Totals Coverage Status
Change from base Build 32: -0.06%
Covered Lines: 1080
Relevant Lines: 1505

💛 - Coveralls

@coveralls
Copy link

coveralls commented Oct 13, 2018

Pull Request Test Coverage Report for Build 55

  • 4 of 6 (66.67%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 71.761%

Changes Missing Coverage Covered Lines Changed/Added Lines %
sqlx.go 4 6 66.67%
Totals Coverage Status
Change from base Build 32: -0.06%
Covered Lines: 1080
Relevant Lines: 1505

💛 - Coveralls

Copy link

@darkliquid darkliquid left a comment

Choose a reason for hiding this comment

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

Looks good, though this does change behaviour of a core method (to more correct behaviour in my opinion), which has the potential to cause a lot of third-party code to break.

Not sure if it would be better to make this a toggle-able behaviour (like enabling a 'strict' mode), or adding a new method like One() which acts like Get() but performs this stricter validation on the results set, so that it doesn't break existing applications that are (incorrectly) relying on this behaviour.

sqlx.go Outdated
@@ -15,6 +15,12 @@ import (
"github.com/jmoiron/sqlx/reflectx"
)

// ErrMultiRows is returned by functions which are expected to work with result sets
// that only contain a single row but multiple rows where returned.

Choose a reason for hiding this comment

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

where = were

sqlx.go Outdated
@@ -15,6 +15,12 @@ import (
"github.com/jmoiron/sqlx/reflectx"
)

// ErrMultiRows is returned by functions which are expected to work with result sets
// that only contain a single row but multiple rows where returned.

Choose a reason for hiding this comment

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

where should be were

@glaslos
Copy link
Contributor

glaslos commented Oct 15, 2018

How about instead of making this optional you create a 1.2.0 release and merge this after the release? It might still break some users code but it also reminds them to stick to a released version. Otherwise you end up never merging features that fix broken behavior.

@stevenh
Copy link
Author

stevenh commented Oct 15, 2018

I think its important this isn't gated and does cause failures as the users code is currently broken in an unpredictable way and they just don't know it.

With this fix in our code base every single case was a legitimate bug.

Validate that a single row only is returned by queries used for Row.Scan.

This avoids unexpected results when the query has an issue such as a missing join criteria or limit in conjunction with functions which expect only on row returned e.g. Get(...).

Also:
* Fixed missing \n's for test output of ConnectAll.
@stevenh
Copy link
Author

stevenh commented Oct 15, 2018

@glaslos not a fan of that process, as it means no one will notice and fix till the next release.

I would always expect those tracking master to be aware there may be noticeable changes, such as this one, and to avoid those one should be tracking a release branch.

@glaslos
Copy link
Contributor

glaslos commented Oct 15, 2018

@stevenh I very much agree with your sentiment. Something very similar happened to uuid: satori/go.uuid#66 which eventually resulted in the community forking the project, leaving behind a lot of broken code and outrage.

@stevenh
Copy link
Author

stevenh commented Oct 19, 2018

Having read that thread it was an API breaking change where this is more a correction of missing error, which I don't think anyone would object to knowing about.

There's also a very easy fix if you SQL is broken and you don't want to fix it properly add LIMIT 1.

As a way of an update this fix has found more subtle bugs in our codebase since being deployed so continuing to have a positive impact.

@stevenh
Copy link
Author

stevenh commented Oct 15, 2019

I know this is really old, but just checking to see if we can get this one in?

@stevenh
Copy link
Author

stevenh commented Feb 4, 2020

A friendly bump to see if we can get this in @jmoiron ?

@dlsniper
Copy link
Collaborator

dlsniper commented Feb 1, 2024

@ardan-bkennedy this looks like a good change, but probably in a new major release since it breaks behavior. Wdyt?

@dlsniper dlsniper added the requires attention The PR looks like it could potentially break things. Definitely requires more testing label Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
requires attention The PR looks like it could potentially break things. Definitely requires more testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants