Geo: Add missing FK constraint on geo_event_log.hashed_storage_attachments_event_id
In https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/7990 we added a foreign key constraint on geo_event_log.hashed_storage_attachments_event_id. But when deploying on GitLab.com we ran into geo_event_log rows that violated the constraint, described in https://gitlab.com/gitlab-org/gitlab-ee/issues/8302.
We created https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8257 to address that issue by removing the orphaned rows just before adding the FK.
But this is vulnerable to a race condition, from https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8257#note_115230385:
- project with id
123is migrated to hashed storage- for this project a
geo_hashed_storage_attachments_eventsrow is created withproject_id = 123. This row has id555- together with that, also a
geo_event_logrow is created, withhashed_storage_attachments_event_id = 555- now migration
ee/db/migrate/20181017131623_add_missing_geo_even_log_indexes.rbis started- the code in
#delete_missing_foreign_key_rowsruns, but nothing is done to the rowgeo_hashed_storage_attachments_eventswith id555- (here is the critical part) right before the FK constraint is added, project
123is deleted- the foreign key constraint kicks in and deletes
geo_hashed_storage_attachments_eventswith id555- there is no FK constraint between
geo_event_logandgeo_hashed_storage_attachments_events(yet), so thegeo_event_logrow withhashed_storage_attachments_event_id = 555remains- then the
add_concurrent_foreign_keyin the migration runs, and finds the violating row💥 💥 💥
So we decided to rollback the adding of the FK, and limit the migration to adding indexes.
This issue addresses proper adding the FK on geo_event_log.hashed_storage_attachments_event_id.