ff : sql mods file repairs
authorBill Erickson <berick@esilibrary.com>
Fri, 25 Oct 2013 16:18:35 +0000 (12:18 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 25 Oct 2013 16:18:35 +0000 (12:18 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/sql/Pg/fulfillment-mods.sql

index 8b85228..2861fa4 100644 (file)
@@ -4,25 +4,39 @@ BEGIN;
 -- Altered Tables ------------------------------------------------------------
 
 ALTER TABLE actor.card
-    ADD COLUMN org INT NOT NULL 
+    ADD COLUMN org INT NOT NULL DEFAULT 1
         REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED;
 
 CREATE UNIQUE INDEX actor_card_barcode_org_idx ON actor.card (barcode, org);
 
 ALTER TABLE asset.copy
-    ADD COLUMN source_lib INT NOT NULL 
+    ADD COLUMN source_lib INT NOT NULL DEFAULT 1
         REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED;
 
-DROP INDEX IF EXISTS copy_barcode_key;
+DROP INDEX IF EXISTS asset.copy_barcode_key;
 CREATE UNIQUE INDEX copy_barcode_key ON asset.copy (barcode, source_lib) 
     WHERE deleted = FALSE OR deleted IS FALSE;
 
-ALTER TABLE biblio.record_entry
-    ADD COLUMN remote_id TEXT;
+ALTER TABLE biblio.record_entry ADD COLUMN remote_id TEXT;
 
 CREATE INDEX biblio_record_entry_remote_id_owner_idx 
-    ON biblio.record_entry ( remote_id, owner );
+    ON biblio.record_entry (remote_id, owner);
 
+-- Altered Tables / Default Values -------------------------------------------
+
+-- update existing seed data with more sane default values for our new org 
+-- unit columns.
+
+-- this should really be the root org unit of the ILL partner, not the 
+-- user's home org unit.
+UPDATE actor.card ac SET org = 
+    (SELECT home_ou FROM actor.usr usr WHERE usr.id = ac.usr);
+
+UPDATE asset.copy acp SET source_lib = 
+    (CASE bre.owner::BOOLEAN WHEN NOT NULL THEN bre.owner ELSE acn.owning_lib END)
+    FROM biblio.record_entry bre 
+    JOIN asset.call_number acn on (bre.id = acn.record)
+    WHERE acp.call_number = acn.id;
 
 -- New Tables ----------------------------------------------------------------
 
@@ -862,3 +876,4 @@ INSERT INTO actor.web_action_print_template
 $$);
 
 COMMIT;
+--ROLLBACK;