Fix RelationCounterFields when IDs are re-used.
Review Request #6814 — Created Jan. 20, 2015 and submitted
RelationCounterField expected that IDs would never be reused. When an instance was first saved to the database, state would be set up for it. If a new instance was later saved with the same ID (due to unit tests, transaction rollbacks, or manually assigning the ID to a new instance), the old state would be reused. Now we handle the first save of an instance through a single callback, instead of one per RelationCounterField on that instance. This resets the old state (since we know that this is a new entry in the database, we can safely nuke any old state). We then loop through all RelationCounterFields and explicitly add them. This approach is safer, cleaner, and reduces the chances of something going wrong. Along with this, we now use a dispatch ID that's less likely to conflict with anything else, to help ensure this all goes smoothly.
Tested first against Barret's repro case. It failed before, but passed
after.Added a new unit test, which simulated Barret's repro case. It failed
before, but passed after.All unit tests pass.