Update callbacks to have _commit suffix #158
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ECF reported that they were seeing a disparity between production db numbers and BigQuery arising during a transaction - the event is sending straight away so if the transaction ends up rolling back the analytics event has already been sent to BQ and doesn’t match the db
This PR updates the callbacks (after_create after_update and after_destroy) to be after_create_commit
after_update_commit and after_destroy_commit so that events will only send after the transaction is complete but also if it rolls back the event will not send
ECF did some testing of this update:
The issue was initially detected in the method save_and_dedupe_participant https://github.com/DFE-Digital/early-careers-framework/blob/main/app/models/npq_application.rb#L134. I've tested the fix locally by using some binding.pry statements to highlight where the analytics log events are occurring and if it's where we expect.
Pic 1: Before the change. Here we can see the event is logged before the transaction is complete. We've hit the pry while still in the transaction but the event has already been sent.
Pic 2: Now with the gem update. We hit the first pry we had before inside the transaction without logging an analytics event and we then publish the event before hitting the second pry outside the transaction. This is what we want. After the first, before the second.
Pic 3: Also with the gem update. I raised a rollback error inside the transaction and we completed the transaction without sending any event whatsoever which again is what we want.