We had quite a major bug in the app. Some screens wound show that an event occurrence had volunteers or tasks in a list view. But when you would click on it to view or edit it, those details would change. Similarly, volunteers would see different data from organizers. This really caused a lot of trust to be lost in the integrity of our system, and rightfully so!
The core of the problem was that occurrences were generated from events, and events could be pretty easily duplicated. Once there was a duplicate event in the system, trying to generate occurrences from those events was non-deterministic.
Event duplication was happening because their uid (unique identifier) was not actually unique. It was copied over when splitting events.
To fix all this, I'm not collecting events based on their shift. Shift is the key model holding all of these occurrences together. All events and shift_events hang off of a shift. This makes the uid obsolete.
We're still finding buts, but all tests are passing and overall it's been easier to fix those bugs. I've also changed small pieces of the updating code (the code that causes the duplicates) to hopefully not create them in the first place. I have not yet found an algorithm that guarantees that no duplicates will be created.