alphagov/whitehall — pull request #955 by elliotcm (2013-10-15 11:01:51+00:00)
Title: Fix non-deterministically ordered attachments.

https://www.pivotaltracker.com/story/show/58806434

This is a follow-on from https://github.com/alphagov/whitehall/pull/943 as that script assumed all created_at dates would be unique for a given document. This is not the case due to machine-imported documents.

As such, the final ordering would be non-deterministic based on MySQL's uncontrolled return order, leading to attachments switching position.

This new migration uses a previous (temporary) table containing the attachments as they were before the last migration as a key to indicate which attachables need updating. The ordering of the current attachment table is then updated using the tuple of the created_at and attachment ID.

I've not proved this on the example given in the ticket because my backup of the table already had orderings for it, but I've tested a wide range of attachables which were updated, with special focus on attachables with a large number of imported attachments. All were fine.

I've also checked a documents which had explicit ordering of attachments, these have been retained (e.g. https://www.gov.uk/government/publications/afghanistan-progress-reports).