]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/ZZZZ.schema.item_triggered_event_log.sql
LP#1207533: item-oriented Triggered Event Log
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / ZZZZ.schema.item_triggered_event_log.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('ZZZZ', :eg_version);
4
5 INSERT INTO config.workstation_setting_type
6     (name, grp, datatype, label)
7 VALUES (
8     'eg.grid.item.event_grid', 'gui', 'object',
9     oils_i18n_gettext(
10     'eg.grid.item.event_grid',
11     'Grid Config: item.event_grid',
12     'cwst', 'label')
13 ), (
14     'eg.grid.patron.event_grid', 'gui', 'object',
15     oils_i18n_gettext(
16     'eg.grid.patron.event_grid',
17     'Grid Config: patron.event_grid',
18     'cwst', 'label')
19 );
20
21 DROP TRIGGER IF EXISTS action_trigger_event_context_item_trig ON action_trigger.event;
22
23 -- Create a NULLABLE version of the fake-copy-fkey trigger function.
24 CREATE OR REPLACE FUNCTION evergreen.fake_fkey_tgr () RETURNS TRIGGER AS $F$
25 DECLARE
26     copy_id BIGINT;
27 BEGIN
28     EXECUTE 'SELECT ($1).' || quote_ident(TG_ARGV[0]) INTO copy_id USING NEW;
29     IF copy_id IS NOT NULL THEN
30         PERFORM * FROM asset.copy WHERE id = copy_id;
31         IF NOT FOUND THEN
32             RAISE EXCEPTION 'Key (%.%=%) does not exist in asset.copy', TG_TABLE_SCHEMA, TG_TABLE_NAME, copy_id;
33         END IF;
34     END IF;
35     RETURN NULL;
36 END;
37 $F$ LANGUAGE PLPGSQL;
38
39
40 --    context_item_path        TEXT, -- for optimizing action_trigger.event
41 ALTER TABLE action_trigger.event_definition ADD COLUMN context_item_path TEXT;
42
43 --    context_item     BIGINT      REFERENCES asset.copy (id)
44 ALTER TABLE action_trigger.event ADD COLUMN context_item BIGINT;
45 CREATE INDEX atev_context_item ON action_trigger.event (context_item);
46
47 UPDATE
48     action_trigger.event_definition
49 SET
50     context_item_path = 'target_copy'
51 WHERE
52     hook IN (
53         SELECT key FROM action_trigger.hook WHERE core_type = 'circ'
54     )
55 ;
56
57 UPDATE
58     action_trigger.event_definition
59 SET
60     context_item_path = 'current_copy'
61 WHERE
62     hook IN (
63         SELECT key FROM action_trigger.hook WHERE core_type = 'ahr'
64     )
65 ;
66
67 -- Retroactively setting context_item on existing rows in action_trigger.event:
68 -- This is not done by default because it'll likely take a long time depending on the Evergreen
69 -- installation.  You may want to do this out-of-band with the upgrade if you want to do this at all.
70 --
71 -- \pset format unaligned
72 -- \t
73 -- \o update_action_trigger_events_for_circs.sql
74 -- SELECT 'UPDATE action_trigger.event e SET context_item = c.target_copy FROM action.circulation cWHERE c.id = e.target AND e.id = ' || e.id || ' RETURNING ' || e.id || ';' FROM action_trigger.event e, action.circulation c WHERE e.target = c.id AND e.event_def IN (SELECT id FROM action_trigger.event_definition WHERE hook in (SELECT key FROM action_trigger.hook WHERE core_type = 'circ')) ORDER BY e.id DESC;
75 -- \o
76 -- \o update_action_trigger_events_for_holds.sql
77 -- SELECT 'UPDATE action_trigger.event e SET context_item = h.current_copy FROM action.hold_request h WHERE h.id = e.target AND e.id = ' || e.id || ' RETURNING ' || e.id || ';' FROM action_trigger.event e, action.hold_request h WHERE e.target = h.id AND e.event_def IN (SELECT id FROM action_trigger.event_definition WHERE hook in (SELECT key FROM action_trigger.hook WHERE core_type = 'ahr')) ORDER BY e.id DESC;
78 -- \o
79
80 COMMIT;
81
82 CREATE TRIGGER action_trigger_event_context_item_trig
83   AFTER INSERT OR UPDATE ON action_trigger.event
84   FOR EACH ROW EXECUTE PROCEDURE evergreen.fake_fkey_tgr('context_item');
85