swisstopo / swissgeol-boreholes-suite

Bohrdatenmanagementsystem (BDMS)
MIT License
4 stars 1 forks source link

Missing foreign key constraints on borehole_files composite primary keys #1298

Closed flenny closed 1 month ago

flenny commented 2 months ago

As mentionend in #1044 there are a bunch of outdated records in the _boreholefiles and files table due to missing foreign key constraints.

-- Entries in borehole_files with missing boreholes or files
SELECT * FROM bdms.borehole_files
WHERE
  id_fil_fk NOT IN (SELECT id_fil FROM bdms.files) OR
  id_bho_fk NOT IN (SELECT id_bho FROM bdms.borehole)

-- Obsolete files which can be deleted
SELECT * FROM bdms.files
WHERE id_fil NOT IN (SELECT id_fil_fk FROM bdms.borehole_files)
flenny commented 2 months ago

SQL statement to use to delete outdated borehole_files records.

DELETE FROM bdms.borehole_files
WHERE (id_fil_fk, id_bho_fk) IN (
    SELECT id_fil_fk, id_bho_fk FROM bdms.borehole_files
    WHERE
      id_fil_fk NOT IN (SELECT id_fil FROM bdms.files) OR
      id_bho_fk NOT IN (SELECT id_bho FROM bdms.borehole))

Executed on... DEV 202406240915 INT 202406251323 PROD 202406271254