]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/999.functions.global.sql
Update the arguments for the target function of the comment that was generating an...
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 999.functions.global.sql
1 /*
2  * Copyright (C) 2008 Equinox Software, Inc.
3  * Bill Erickson <erickson@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 CREATE OR REPLACE FUNCTION actor.usr_merge_rows( table_name TEXT, col_name TEXT, src_usr INT, dest_usr INT ) RETURNS VOID AS $$
18 DECLARE
19     sel TEXT;
20     upd TEXT;
21     del TEXT;
22     cur_row RECORD;
23 BEGIN
24     sel := 'SELECT id::BIGINT FROM ' || table_name || ' WHERE ' || quote_ident(col_name) || ' = ' || quote_literal(src_usr);
25     upd := 'UPDATE ' || table_name || ' SET ' || quote_ident(col_name) || ' = ' || quote_literal(dest_usr) || ' WHERE id = ';
26     del := 'DELETE FROM ' || table_name || ' WHERE id = ';
27     FOR cur_row IN EXECUTE sel LOOP
28         BEGIN
29             --RAISE NOTICE 'Attempting to merge % %', table_name, cur_row.id;
30             EXECUTE upd || cur_row.id;
31         EXCEPTION WHEN unique_violation THEN
32             --RAISE NOTICE 'Deleting conflicting % %', table_name, cur_row.id;
33             EXECUTE del || cur_row.id;
34         END;
35     END LOOP;
36 END;
37 $$ LANGUAGE plpgsql;
38
39 COMMENT ON FUNCTION actor.usr_merge_rows(TEXT, TEXT, INT, INT) IS $$
40 /**
41  * Attempts to move each row of the specified table from src_user to dest_user.  
42  * Where conflicts exist, the conflicting "source" row is deleted.
43  */
44 $$;
45
46
47 CREATE OR REPLACE FUNCTION actor.usr_merge( src_usr INT, dest_usr INT, del_addrs BOOLEAN, del_cards BOOLEAN, deactivate_cards BOOLEAN ) RETURNS VOID AS $$
48 BEGIN
49
50     -- do some initial cleanup 
51     UPDATE actor.usr SET card = NULL WHERE id = src_usr;
52     UPDATE actor.usr SET mailing_address = NULL WHERE id = src_usr;
53     UPDATE actor.usr SET billing_address = NULL WHERE id = src_usr;
54
55     -- actor.*
56     IF del_cards THEN
57         DELETE FROM actor.card where usr = src_usr;
58     ELSE
59         IF deactivate_cards THEN
60             UPDATE actor.card SET active = 'f' WHERE usr = src_usr;
61         END IF;
62         UPDATE actor.card SET usr = dest_usr WHERE usr = src_usr;
63     END IF;
64
65
66     IF del_addrs THEN
67         DELETE FROM actor.usr_address WHERE usr = src_usr;
68     ELSE
69         UPDATE actor.usr_address SET usr = dest_usr WHERE usr = src_usr;
70     END IF;
71
72     UPDATE actor.usr_note SET usr = dest_usr WHERE usr = src_usr;
73     -- dupes are technically OK in actor.usr_standing_penalty, should manually delete them...
74     UPDATE actor.usr_standing_penalty SET usr = dest_usr WHERE usr = src_usr;
75     PERFORM actor.usr_merge_rows('actor.usr_org_unit_opt_in', 'usr', src_usr, dest_usr);
76     PERFORM actor.usr_merge_rows('actor.usr_setting', 'usr', src_usr, dest_usr);
77
78     -- permission.*
79     PERFORM actor.usr_merge_rows('permission.usr_perm_map', 'usr', src_usr, dest_usr);
80     PERFORM actor.usr_merge_rows('permission.usr_object_perm_map', 'usr', src_usr, dest_usr);
81     PERFORM actor.usr_merge_rows('permission.usr_grp_map', 'usr', src_usr, dest_usr);
82     PERFORM actor.usr_merge_rows('permission.usr_work_ou_map', 'usr', src_usr, dest_usr);
83
84
85     -- container.*
86     PERFORM actor.usr_merge_rows('container.biblio_record_entry_bucket', 'owner', src_usr, dest_usr);
87     PERFORM actor.usr_merge_rows('container.call_number_bucket', 'owner', src_usr, dest_usr);
88     PERFORM actor.usr_merge_rows('container.copy_bucket', 'owner', src_usr, dest_usr);
89     PERFORM actor.usr_merge_rows('container.user_bucket', 'owner', src_usr, dest_usr);
90     PERFORM actor.usr_merge_rows('container.user_bucket_item', 'target_user', src_usr, dest_usr);
91
92     -- vandelay.*
93     PERFORM actor.usr_merge_rows('vandelay.queue', 'owner', src_usr, dest_usr);
94
95     -- money.*
96     PERFORM actor.usr_merge_rows('money.collections_tracker', 'usr', src_usr, dest_usr);
97     PERFORM actor.usr_merge_rows('money.collections_tracker', 'collector', src_usr, dest_usr);
98     UPDATE money.billable_xact SET usr = dest_usr WHERE usr = src_usr;
99     UPDATE money.billing SET voider = dest_usr WHERE voider = src_usr;
100     UPDATE money.bnm_payment SET accepting_usr = dest_usr WHERE accepting_usr = src_usr;
101
102     -- action.*
103     UPDATE action.circulation SET usr = dest_usr WHERE usr = src_usr;
104     UPDATE action.circulation SET circ_staff = dest_usr WHERE circ_staff = src_usr;
105     UPDATE action.circulation SET checkin_staff = dest_usr WHERE checkin_staff = src_usr;
106
107     UPDATE action.hold_request SET usr = dest_usr WHERE usr = src_usr;
108     UPDATE action.hold_request SET fulfillment_staff = dest_usr WHERE fulfillment_staff = src_usr;
109     UPDATE action.hold_request SET requestor = dest_usr WHERE requestor = src_usr;
110     UPDATE action.hold_notification SET notify_staff = dest_usr WHERE notify_staff = src_usr;
111
112     UPDATE action.in_house_use SET staff = dest_usr WHERE staff = src_usr;
113     UPDATE action.non_cataloged_circulation SET staff = dest_usr WHERE staff = src_usr;
114     UPDATE action.non_cataloged_circulation SET patron = dest_usr WHERE patron = src_usr;
115     UPDATE action.non_cat_in_house_use SET staff = dest_usr WHERE staff = src_usr;
116     UPDATE action.survey_response SET usr = dest_usr WHERE usr = src_usr;
117
118     -- acq.*
119     UPDATE acq.fund_allocation SET allocator = dest_usr WHERE allocator = src_usr;
120     PERFORM actor.usr_merge_rows('acq.picklist', 'owner', src_usr, dest_usr);
121     UPDATE acq.purchase_order SET owner = dest_usr WHERE owner = src_usr;
122     UPDATE acq.po_note SET creator = dest_usr WHERE creator = src_usr;
123     UPDATE acq.po_note SET editor = dest_usr WHERE editor = src_usr;
124     UPDATE acq.lineitem_note SET creator = dest_usr WHERE creator = src_usr;
125     UPDATE acq.lineitem_note SET editor = dest_usr WHERE editor = src_usr;
126     UPDATE acq.lineitem_usr_attr_definition SET usr = dest_usr WHERE usr = src_usr;
127
128     -- asset.*
129     UPDATE asset.copy SET creator = dest_usr WHERE creator = src_usr;
130     UPDATE asset.copy SET editor = dest_usr WHERE editor = src_usr;
131     UPDATE asset.copy_note SET creator = dest_usr WHERE creator = src_usr;
132     UPDATE asset.call_number SET creator = dest_usr WHERE creator = src_usr;
133     UPDATE asset.call_number SET editor = dest_usr WHERE editor = src_usr;
134     UPDATE asset.call_number_note SET creator = dest_usr WHERE creator = src_usr;
135
136
137     -- reporter.*
138     -- It's not uncommon to define the reporter schema in a replica 
139     -- DB only, so don't assume these tables exist in the write DB.
140     BEGIN
141         PERFORM actor.usr_merge_rows('reporter.template', 'owner', src_usr, dest_usr);
142     EXCEPTION WHEN undefined_table THEN
143         -- do nothing
144     END;
145     BEGIN
146         PERFORM actor.usr_merge_rows('reporter.report', 'owner', src_usr, dest_usr);
147     EXCEPTION WHEN undefined_table THEN
148         -- do nothing
149     END;
150     BEGIN
151         PERFORM actor.usr_merge_rows('reporter.schedule', 'runner', src_usr, dest_usr);
152     EXCEPTION WHEN undefined_table THEN
153         -- do nothing
154     END;
155     BEGIN
156         PERFORM actor.usr_merge_rows('reporter.template_folder', 'owner', src_usr, dest_usr);
157     EXCEPTION WHEN undefined_table THEN
158         -- do nothing
159     END;
160     BEGIN
161         PERFORM actor.usr_merge_rows('reporter.report_folder', 'owner', src_usr, dest_usr);
162     EXCEPTION WHEN undefined_table THEN
163         -- do nothing
164     END;
165     BEGIN
166         PERFORM actor.usr_merge_rows('reporter.output_folder', 'owner', src_usr, dest_usr);
167     EXCEPTION WHEN undefined_table THEN
168         -- do nothing
169     END;
170
171     -- Finally, delete the source user
172     DELETE FROM actor.usr WHERE id = src_usr;
173
174 END;
175 $$ LANGUAGE plpgsql;
176
177 COMMENT ON FUNCTION actor.usr_merge(INT, INT, BOOLEAN, BOOLEAN, BOOLEAN) IS $$
178 /**
179  * Merges all user date from src_usr to dest_usr.  When collisions occur, 
180  * keep dest_usr's data and delete src_usr's data.
181  */
182 $$;
183
184
185
186 CREATE OR REPLACE FUNCTION actor.approve_pending_address(pending_id INT) RETURNS BIGINT AS $$
187 DECLARE
188     old_id INT;
189 BEGIN
190     SELECT INTO old_id replaces FROM actor.usr_address where id = pending_id;
191     IF old_id IS NULL THEN
192         RETURN NULL;
193     END IF;
194     DELETE FROM actor.usr_address WHERE id = -old_id;
195     UPDATE actor.usr_address SET id = -id WHERE id = old_id;
196     UPDATE actor.usr_address SET replaces = NULL, id = old_id WHERE id = pending_id;
197     RETURN old_id;
198 END
199 $$ LANGUAGE plpgsql;
200
201 COMMENT ON FUNCTION actor.approve_pending_address(INT) IS $$
202 /**
203  * Replaces an address with a pending address.  This is done by giving the pending 
204  * address the ID of the old address.  The replaced address is retained with -id.
205  */
206 $$;
207
208
209