I have an application with a model named appointment. On this model, there is a column with the name event_uid
and a validation like the following:
validates :event_uid, uniqueness: true, allow_nil: true
The unique validation is only on rails application and not in the database (postgresql).
I am using background job with sidekiq on heroku to sync some remote calendars. I am not sure what happened, but it seems like I got multiple records with duplicate event_uid
values. They have been created in the exact same second.
My guess is that something happened on the workers and for some reason they got invoked at the same time or the queue frozen and when it got back it ran the same job twice. I don't understand why rails let the above to pass (maybe because workers run on different threads plays a role?). I added the following migration:
add_index :appointments, [:event_uid], unique: true
With the hope that it won't happen again. Ok so now the questions:
- What do you think, will this be enough?
- Is it dangerous to allow unique / presence validations to exist only on application level if you are using create / update with background jobs?
- Any guess what could have caused the workers to run the same job more than one and exactly the same second?