-- 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 ----------------------------------------------------------------
$$);
COMMIT;
+--ROLLBACK;