annotate OrthancServer/Upgrade4To5.sql @ 2532:c35202c8c0db

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Apr 2018 17:07:37 +0200
parents f24e04838054
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 -- This SQLite script updates the version of the Orthanc database from 4 to 5.
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3
1159
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
4 -- Remove 2 indexes to speed up
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
5
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
6 DROP INDEX MainDicomTagsIndex2;
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
7 DROP INDEX MainDicomTagsIndexValues;
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8
1159
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
9
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
10 -- Add a new table to index the DICOM identifiers
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
11
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
12 CREATE TABLE DicomIdentifiers(
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 id INTEGER REFERENCES Resources(internalId) ON DELETE CASCADE,
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 tagGroup INTEGER,
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 tagElement INTEGER,
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 value TEXT,
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 PRIMARY KEY(id, tagGroup, tagElement)
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 );
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
1159
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
20 CREATE INDEX DicomIdentifiersIndex1 ON DicomIdentifiers(id);
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
21 CREATE INDEX DicomIdentifiersIndex2 ON DicomIdentifiers(tagGroup, tagElement);
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
22 CREATE INDEX DicomIdentifiersIndexValues ON DicomIdentifiers(value COLLATE BINARY);
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 -- Migrate data from MainDicomTags to MainResourcesTags and MainInstancesTags
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
1159
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
27 INSERT INTO DicomIdentifiers SELECT * FROM MainDicomTags
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
28 WHERE ((tagGroup = 16 AND tagElement = 32) OR -- PatientID (0x0010, 0x0020)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
29 (tagGroup = 32 AND tagElement = 13) OR -- StudyInstanceUID (0x0020, 0x000d)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
30 (tagGroup = 8 AND tagElement = 80) OR -- AccessionNumber (0x0008, 0x0050)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
31 (tagGroup = 32 AND tagElement = 14) OR -- SeriesInstanceUID (0x0020, 0x000e)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
32 (tagGroup = 8 AND tagElement = 24)); -- SOPInstanceUID (0x0008, 0x0018)
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
1159
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
34 DELETE FROM MainDicomTags
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
35 WHERE ((tagGroup = 16 AND tagElement = 32) OR -- PatientID (0x0010, 0x0020)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
36 (tagGroup = 32 AND tagElement = 13) OR -- StudyInstanceUID (0x0020, 0x000d)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
37 (tagGroup = 8 AND tagElement = 80) OR -- AccessionNumber (0x0008, 0x0050)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
38 (tagGroup = 32 AND tagElement = 14) OR -- SeriesInstanceUID (0x0020, 0x000e)
8f9d49192815 speeding up db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1158
diff changeset
39 (tagGroup = 8 AND tagElement = 24)); -- SOPInstanceUID (0x0008, 0x0018)
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 -- Upgrade the "ResourceDeleted" trigger
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 DROP TRIGGER ResourceDeleted;
1176
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
45 DROP TRIGGER ResourceDeletedParentCleaning;
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 CREATE TRIGGER ResourceDeleted
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 AFTER DELETE ON Resources
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 BEGIN
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 SELECT SignalResourceDeleted(old.publicId, old.resourceType);
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 SELECT SignalRemainingAncestor(parent.publicId, parent.resourceType)
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 FROM Resources AS parent WHERE internalId = old.parentId;
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 END;
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
1176
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
55 CREATE TRIGGER ResourceDeletedParentCleaning
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
56 AFTER DELETE ON Resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
57 FOR EACH ROW WHEN (SELECT COUNT(*) FROM Resources WHERE parentId = old.parentId) = 0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
58 BEGIN
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
59 DELETE FROM Resources WHERE internalId = old.parentId;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
60 END;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1159
diff changeset
61
1158
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 -- Change the database version
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 -- The "1" corresponds to the "GlobalProperty_DatabaseSchemaVersion" enumeration
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
badc14fee61f speed up db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 UPDATE GlobalProperties SET value="5" WHERE property=1;