Bug #1006466 - action.hold_request.hold_type needs constraint - 2.4.0 (master)
authorKyle Tomita <ktomita@catalystitservices.com>
Fri, 1 Feb 2013 16:56:23 +0000 (08:56 -0800)
committerKyle Tomita <ktomita@catalystitservices.com>
Fri, 1 Feb 2013 16:56:23 +0000 (08:56 -0800)
Script XXXX.action.hold_request.hold_type-foreign-key-constraint.sql
adds the config.hold_type table to use as the constaint and adds the
foreign key relationship to ahr.

The script also includes code to remove the table and constraint.
Along with code to capitalize hold_type's in ahr and a command to
give the hold_types in ahr that are not in config.hold_type.

Signed-off-by: Kyle Tomita <ktomita@catalystitservices.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.action.hold_request.hold_type-foreign-key-constraint.sql [new file with mode: 0644]

diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.action.hold_request.hold_type-foreign-key-constraint.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.action.hold_request.hold_type-foreign-key-constraint.sql
new file mode 100644 (file)
index 0000000..9b09d61
--- /dev/null
@@ -0,0 +1,42 @@
+BEGIN;
+    SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+    --create config.hold_type table
+    CREATE TABLE config.hold_type (
+       type text NOT NULL,
+       name text NOT NULL,
+       description text,
+       CONSTRAINT hold_type_pkey PRIMARY KEY (type)
+    );
+
+    --insert base hold types
+    INSERT INTO config.hold_type (type, name, description) VALUES
+        ('C', 'Copy', 'Copy Level Hold'),
+        ('F', 'Force', 'Copy Level Hold - Bypasses Hold Rules'),
+        ('R', 'Recall', 'Cataloger''s Recall - Copy Level Hold - Bypasses Hold Rules - Goes to Cataloging at capture'),
+        ('T', 'Title', 'Bib Level Hold'),
+        ('M', 'Metarecord', 'Metarecord Level Hold'),
+        ('V', 'Volume', 'Volume (Call Number) Level Hold'),
+        ('I', 'Issuance', 'Issuance Level Hold'),
+        ('P', 'Part', 'Part Level Hold');
+
+    --remove config.hold_type table
+    --DROP TABLE config.hold_type;
+
+    --convert all hold_types in action.hold_request to uppercase
+    --UPDATE action.hold_request
+    --SET hold_type = upper(hold_type)
+    --WHERE hold_type ~ '[a-z]';
+
+    --get all hold_types in action.hold_request that are not in config.hold_type
+    SELECT DISTINCT ahr.hold_type invalid_types
+    FROM action.hold_request AS ahr
+    EXCEPT SELECT type FROM config.hold_type;
+
+    --create foreign key
+    ALTER TABLE action.hold_request ADD CONSTRAINT hold_type_fkey FOREIGN KEY (hold_type) REFERENCES config.hold_type (type) MATCH FULL;
+
+    --remove foreign key
+    --ALTER TABLE action.hold_request DROP CONSTRAINT hold_type_fkey;
+
+COMMIT;