Fix RelationCounterFields when IDs are re-used.

Review Request #6814 — Created Jan. 20, 2015 and submitted

Information

Djblets
release-0.8.x
93ee3f1...

Reviewers

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.

reviewbot
  1. Tool: Pyflakes
    Processed Files:
        djblets/db/tests/test_relation_counter_field.py
        djblets/db/fields.py
    
    
    
    Tool: PEP8 Style Checker
    Processed Files:
        djblets/db/tests/test_relation_counter_field.py
        djblets/db/fields.py
    
    
  2. 
      
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-0.8.x (1c1dde3)