Mercurial > hg > orthanc-databases
view PostgreSQL/Plugins/SQL/Upgrades/Rev4ToRev99.sql @ 677:5c8ed62ccbc5 sql-opti
re-implemented PatientRecyclingOrder with metadata
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Tue, 03 Jun 2025 17:55:51 +0200 |
parents | PostgreSQL/Plugins/SQL/Upgrades/Rev4ToRev5.sql@bfa993553ed9 |
children | 0ce2725836c6 |
line wrap: on
line source
CREATE SEQUENCE IF NOT EXISTS PatientRecyclingOrderSequence INCREMENT 1 START 1; -- the protection mechanisms changed in rev 99. We now use a metadata (18: IsProtected) -- while, in the past, patients where protected by not appearing in the PatientRecyclingOrder -- Step 1: Identify all patients that are not in PatientRecyclingOrder (those are the protected patients) -- The "0" corresponds to "OrthancPluginResourceType_Patient" WITH ProtectedPatients AS ( SELECT r.internalId AS internalId FROM Resources r LEFT JOIN PatientRecyclingOrder pro ON r.internalId = pro.patientId WHERE r.resourceType = 0 AND pro.patientId IS NULL ) , UnprotectedPatients AS ( SELECT patientId AS internalId FROM PatientRecyclingOrder ORDER BY seq ASC ) -- Step 2: Prepare the data for both metadata types , MetadataToInsert AS ( -- mark protected patient in 18: IsProtected SELECT internalId, 18 AS metadataType, 'true' AS metadataValue FROM ProtectedPatients UNION ALL -- copy previous recycling order in 19: RecyclingOrder SELECT internalId, 19 AS metadataType, nextval('PatientRecyclingOrderSequence')::TEXT AS metadataValue FROM UnprotectedPatients ) -- Step 3: Insert the Metadata (since the metadata are new, there should not be any conflicts) INSERT INTO Metadata (id, type, value, revision) SELECT internalId, metadataType, metadataValue, 0 FROM MetadataToInsert ON CONFLICT (id, type) DO UPDATE SET value = EXCLUDED.value, revision = EXCLUDED.revision; -- The PatientRecyclingOrder table can now be removed DROP TABLE PatientRecyclingOrder; DROP TRIGGER IF EXISTS PatientAdded on Resources; DROP FUNCTION IF EXISTS PatientAddedFunc; DROP FUNCTION IF EXISTS PatientAddedOrUpdated;