]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/1.4-shadow_full_rec-upgrade-db.sql
New table: actor.usr_saved_search
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 1.4-shadow_full_rec-upgrade-db.sql
1 /*
2  * Copyright (C) 2008  Equinox Software, Inc.
3  * Mike Rylander <miker@esilibrary.com> 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17
18 BEGIN;
19
20 -- To avoid any updates while we're doin' our thing...
21 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
22
23 -- This index, right here, is the reason for this change.
24 DROP INDEX metabib.metabib_full_rec_value_idx;
25
26 -- So, on to it.
27 -- Move the table out of the way ...
28 ALTER TABLE metabib.full_rec RENAME TO real_full_rec;
29
30 -- ... and let the trigger management functions know about the change ...
31 CREATE OR REPLACE FUNCTION reporter.disable_materialized_simple_record_trigger () RETURNS VOID AS $$
32     DROP TRIGGER zzz_update_materialized_simple_record_tgr ON metabib.real_full_rec;
33 $$ LANGUAGE SQL;
34
35 CREATE OR REPLACE FUNCTION reporter.enable_materialized_simple_record_trigger () RETURNS VOID AS $$
36
37     TRUNCATE TABLE reporter.materialized_simple_record;
38
39     INSERT INTO reporter.materialized_simple_record
40         (id,fingerprint,quality,tcn_source,tcn_value,title,author,publisher,pubdate,isbn,issn)
41         SELECT DISTINCT ON (id) * FROM reporter.old_super_simple_record;
42
43     CREATE TRIGGER zzz_update_materialized_simple_record_tgr
44         AFTER INSERT OR UPDATE OR DELETE ON metabib.real_full_rec
45         FOR EACH ROW EXECUTE PROCEDURE reporter.simple_rec_sync();
46
47 $$ LANGUAGE SQL;
48
49 -- ... replace the table with a suitable view, which applies the index contstraint we'll use ...
50 CREATE OR REPLACE VIEW metabib.full_rec AS
51     SELECT  id,
52             record,
53             tag,
54             ind1,
55             ind2,
56             subfield,
57             SUBSTRING(value,1,1024) AS value,
58             index_vector
59       FROM  metabib.real_full_rec;
60
61 -- ... now some rules to transform DML against the view into DML against the underlying table ...
62 CREATE OR REPLACE RULE metabib_full_rec_insert_rule
63     AS ON INSERT TO metabib.full_rec
64     DO INSTEAD
65     INSERT INTO metabib.real_full_rec VALUES (
66         COALESCE(NEW.id, NEXTVAL('metabib.full_rec_id_seq'::REGCLASS)),
67         NEW.record,
68         NEW.tag,
69         NEW.ind1,
70         NEW.ind2,
71         NEW.subfield,
72         NEW.value,
73         NEW.index_vector
74     );
75
76 CREATE OR REPLACE RULE metabib_full_rec_update_rule
77     AS ON UPDATE TO metabib.full_rec
78     DO INSTEAD
79     UPDATE  metabib.real_full_rec SET
80         id = NEW.id,
81         record = NEW.record,
82         tag = NEW.tag,
83         ind1 = NEW.ind1,
84         ind2 = NEW.ind2,
85         subfield = NEW.subfield,
86         value = NEW.value,
87         index_vector = NEW.index_vector
88       WHERE id = OLD.id;
89
90 CREATE OR REPLACE RULE metabib_full_rec_delete_rule
91     AS ON DELETE TO metabib.full_rec
92     DO INSTEAD
93     DELETE FROM metabib.real_full_rec WHERE id = OLD.id;
94
95 -- ... and last, but not least, create a fore-shortened index on the value column.
96 CREATE INDEX metabib_full_rec_value_idx ON metabib.real_full_rec (substring(value,1,1024));
97
98 -- Wheeee...
99 COMMIT;
100