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

[9.x] Improve observer strategy #490

Merged
merged 4 commits into from
Jul 6, 2021

Conversation

ksntcrq
Copy link
Contributor

@ksntcrq ksntcrq commented Jul 1, 2021

Problem

The current ModelObserver implementation calls unsearchable every time a model gets deleted. Practically, it means that the underlying engine (in our case, at StuDocu, Algolia) would be asked to delete the model from its index regardless of it actually being in the index or not.

Solution

Two methods named wasSearchableBeforeUpdate and wasSearchableBeforeDelete are implemented to respectively check if a model was searchable before being updated or deleted. ModelObserver gets smarter and first checks if a model was actually searchable before calling unsearchable, thanks to one or the other method.

Now, the flow is as follow:

  • When a model gets updated and is not searchable, we first check if it was searchable using wasSearchableBeforeUpdate. If it was, it is made unsearchable. If it wasn't, there is no need to make it unsearchable.
  • When a model gets deleted, we first check if it was searchable using wasSearchableBeforeDelete. If it was, it is made unsearchable. If it wasn't, there is no need to make it unsearchable.

Additional fix

The ModelObserverTest unit tests were not asserting anything and were actually broken after calling disableSyncingFor in test_saved_handler_doesnt_make_model_searchable_when_disabled. They're now fixed and extended to cover the use cases described above.

@driesvints driesvints changed the title Feature/improve observer strategy [9.x] Improve observer strategy Jul 1, 2021
@taylorotwell taylorotwell merged commit 7b0235c into laravel:9.x Jul 6, 2021
@taylorotwell
Copy link
Member

I changed the wasSearchable... methods to always return true. You can override them to provide your own opinions on how they should work.

@ksntcrq
Copy link
Contributor Author

ksntcrq commented Jul 6, 2021

Thank you, @taylorotwell.

I was wondering - will you guys include the suggested wasSearchable... implementations in the documentation as an example, so users won't have to figure it out themselves?

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

3 participants