fpdcc / ccfp-asset-dashboard

CCFP Asset Dashboard
0 stars 1 forks source link

production - phase deletion takes you to error page #215

Closed glw closed 1 year ago

glw commented 1 year ago

When deleting a phase it takes you here; image

Thought it does say that the phase was deleted successfully its has not been removed.

smcalilly commented 1 year ago

@glw I've not been able to recreate this. Do you know which project this was for?

glw commented 1 year ago

try project "test" - ... /projects/85/ Try to remove the phase. It should take you to that same error page. I just tested again today and got the same result.

smcalilly commented 1 year ago

@glw very helpful — thanks!

smcalilly commented 1 year ago

first, a glimpse at the logs

2022-08-08T15:23:32.133686+00:00 app[web.1]: [2022-08-08 15:23:32 +0000] [20] [DEBUG] POST /projects/phases/delete/126/
2022-08-08T15:23:32.277375+00:00 app[web.1]: Internal Server Error: /projects/phases/delete/126/
2022-08-08T15:23:32.277377+00:00 app[web.1]: Traceback (most recent call last):
2022-08-08T15:23:32.277378+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 242, in _commit
2022-08-08T15:23:32.277378+00:00 app[web.1]: return self.connection.commit()
2022-08-08T15:23:32.277379+00:00 app[web.1]: psycopg2.errors.ForeignKeyViolation: insert or update on table "asset_dashboard_phasezonedistribution" violates foreign key constraint "asset_dashboard_phas_phase_id_535d6e36_fk_asset_das"
2022-08-08T15:23:32.277379+00:00 app[web.1]: DETAIL:  Key (phase_id)=(126) is not present in table "asset_dashboard_phase".
...
2022-08-08T15:23:32.277394+00:00 app[web.1]: django.db.utils.IntegrityError: insert or update on table "asset_dashboard_phasezonedistribution" violates foreign key constraint "asset_dashboard_phas_phase_id_535d6e36_fk_asset_das"
2022-08-08T15:23:32.277394+00:00 app[web.1]: DETAIL:  Key (phase_id)=(126) is not present in table "asset_dashboard_phase".

i think this is some sort of race condition. we calculate the PhaseZoneDistribution values whenever a LocalAsset is created or deleted. by the time Django executes the queries for the PhaseZoneDistribution, the Phase is already deleted. this causes the foreign key constraint, since PhaseZoneDistribution has a fk for Phase.

if this is true, then this would happen only when deleting a phase that has gis assets.

visually:

some observations:

todo:

smcalilly commented 1 year ago

here's a gist with logs from stepping through the code with the debugger: https://gist.github.com/smcalilly/e4c198d81fbe3577ba858a44eeee712f