fablabbcn / smartcitizen-api

The Smart Citizen Engine
https://developer.smartcitizen.me
GNU Affero General Public License v3.0
10 stars 4 forks source link

Delete devices job breaks after the postprocessings integration #214

Closed sentry-io[bot] closed 1 year ago

sentry-io[bot] commented 2 years ago

Sentry Issue: SC-API-4X

PG::ForeignKeyViolation: ERROR:  update or delete on table "devices" violates foreign key constraint "fk_rails_f48c216ac2" on table "postprocessings"
DETAIL:  Key (id)=(15485) is still referenced from table "postprocessings".

  app/jobs/delete_archived_devices_job.rb:11:in `block in perform'
    device.destroy!
  app/jobs/delete_archived_devices_job.rb:8:in `perform'
    Device.unscoped.where(workflow_state: "archived").each do |device|
...
(116 additional frame(s) were not displayed)

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  update or delete on table "devices" violates foreign key constraint "fk_rails_f48c216ac2" on table "postprocessings"
DETAIL:  Key (id)=(15485) is still referenced from table "postprocessings".
viktorsmari commented 2 years ago

Chipping in here because I received an email notification :smile:

https://github.com/fablabbcn/smartcitizen-api/blob/1185a9e365ef3da300aea7da978ccd8483c0fc69/app/models/device.rb#L26

This issue might be resolved if we allow a Postprocessing record to be also deleted when a device is deleted.

I have not tested it, but the solution might be like this

 has_one :postprocessing, dependent: :destroy
pral2a commented 2 years ago

Thank you @viktorsmari

We'll work on it next week and test it

oscgonfer commented 1 year ago

Just for traceability, we discussed that it would be a good practice to document better the actual archive->delete process for both, developer and users.

We are also checking wether or not the fix can present any risk on deployment given the large amount of Devices that have been archived but not deleted in production.