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

No need for validation query unless relevant fields were changed #2905

Merged
merged 1 commit into from
Feb 4, 2019
Merged

No need for validation query unless relevant fields were changed #2905

merged 1 commit into from
Feb 4, 2019

Conversation

nathanl
Copy link
Contributor

@nathanl nathanl commented Feb 1, 2019

unsafe_validate_unique/3 performs a query to ensure that the specified
fields represent a unique combination of values in the repo. For
example, perhaps the combination of city and state must be unique.
But there's no need to perform this query if the changeset is not making
any change to city or state.

Fixes #2901

@nathanl
Copy link
Contributor Author

nathanl commented Feb 1, 2019

If you set the diff to hide white space changes, you'll see that the change here is very small. The function is now even longer and uglier with the additional nesting. 😆 This might be a nice place for someone to contribute a refactor.

lib/ecto/changeset.ex Outdated Show resolved Hide resolved
lib/ecto/changeset.ex Outdated Show resolved Hide resolved
@josevalim
Copy link
Member

Thank you so much @nathanl! I have dropped some comments. We just need some tests too and we should be good to go!

@nathanl
Copy link
Contributor Author

nathanl commented Feb 1, 2019

@josevalim Thanks for the feedback. I noticed that mix test doesn't run the integration tests. I think make test used to do that, but I don't see how now.

@josevalim
Copy link
Member

@nathanl the integration tests are now part of ecto_sql. I think we can probably unit test this change in particular (i.e. you can pass a fake Repo as it should never be invoked). Otherwise ecto_sql is the way to go!

@nathanl
Copy link
Contributor Author

nathanl commented Feb 1, 2019

Hmm. I'm confused because integration_test/cases/repo.exs is still in this project and has tests referring to this validation, but putting assert true == false in one of them doesn't cause a failure.

@josevalim
Copy link
Member

@nathanl the integration tests are still shipped as part of ecto, because they are shared across adapters, but there is no adapter in ecto per se for us to run them against.

unsafe_validate_unique/3 performs a query to ensure that the specified
fields represent a unique combination of values in the repo. For
example, perhaps the combination of city and state must be unique.
But there's no need to perform this query if the changeset is not making
any change to city or state.

Fixes #2901
@nathanl
Copy link
Contributor Author

nathanl commented Feb 4, 2019

@josevalim Thanks for your feedback and explanation. I've updated this PR and also created #2908 and elixir-ecto/ecto_sql#79 to document what you explained about integration tests.

Any more feedback?

assert_receive [function: :one, query: �to.Query{}]

unsafe_validate_unique(body_change, :title, DetectionRepo)
refute_receive [function: :one, query: �to.Query{}]
Copy link
Member

Choose a reason for hiding this comment

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

I was going to say we should break this apart but given everything is together for now, then it is best to leave it for another PR. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@josevalim Here's a PR to break apart the test: #2909

@josevalim josevalim merged commit 6363b9a into elixir-ecto:master Feb 4, 2019
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@nathanl nathanl deleted the nathanl/lazy-unsafe-validate-unique branch February 5, 2019 13:37
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.

None yet

2 participants