]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/tests/datasets/sql/env_create.sql
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / tests / datasets / sql / env_create.sql
1 /*
2  * 'id' gives us insert sort order
3  * 'tag' is mapped to the last_xact_id at import
4  */
5 CREATE TABLE marcxml_import (id SERIAL PRIMARY KEY, marc TEXT, tag TEXT);
6
7 /**
8  * Create an address for a given actor.org_unit
9  *
10  * The "address_type" parameter accepts a TEXT value that contains the
11  * strings 'mailing', 'interlibrary', 'billing', and 'holds' to enable
12  * you to provide granular control over which address is associated for
13  * each function; if given NULL, then all functions are associated with
14  * the incoming address.
15  *
16  * This will happily create duplicate addresses if given duplicate info.
17  */
18 CREATE FUNCTION evergreen.create_aou_address
19     (owning_lib INTEGER, street1 TEXT, street2 TEXT, city TEXT, state TEXT, country TEXT,
20      post_code TEXT, address_type TEXT)
21 RETURNS void AS $$
22 BEGIN
23     INSERT INTO actor.org_address (org_unit, street1, street2, city, state, country, post_code)
24         VALUES ($1, $2, $3, $4, $5, $6, $7);
25     
26     IF $8 IS NULL THEN
27         UPDATE actor.org_unit SET holds_address = currval('actor.org_address_id_seq'), ill_address = currval('actor.org_address_id_seq'), billing_address = currval('actor.org_address_id_seq'), mailing_address = currval('actor.org_address_id_seq') WHERE id = $1;
28     END IF;
29     IF $8 ~ 'holds' THEN
30         UPDATE actor.org_unit SET holds_address = currval('actor.org_address_id_seq') WHERE id = $1;
31     END IF;
32     IF $8 ~ 'interlibrary' THEN
33         UPDATE actor.org_unit SET ill_address = currval('actor.org_address_id_seq') WHERE id = $1;
34     END IF;
35     IF $8 ~ 'billing' THEN
36         UPDATE actor.org_unit SET billing_address = currval('actor.org_address_id_seq') WHERE id = $1;
37     END IF;
38     IF $8 ~ 'mailing' THEN
39         UPDATE actor.org_unit SET mailing_address = currval('actor.org_address_id_seq') WHERE id = $1;
40     END IF;
41 END
42 $$ LANGUAGE PLPGSQL;
43
44 /**
45  * create a callnumber for every bib record in the database,
46  * appending the bib ID to the callnumber label to differentiate.
47  * If set, 'bib_tag' will limit the inserted callnumbers to bibs
48  * whose last_xact_id matches bib_tag
49  */
50 CREATE FUNCTION evergreen.populate_call_number 
51     (ownlib INTEGER, label TEXT, bib_tag TEXT, class INTEGER DEFAULT 1)
52 RETURNS void AS $$
53     INSERT INTO asset.call_number (record, creator, editor, owning_lib, label, label_class)
54         SELECT id, 1, 1, $1, $2 || id::text, $4
55         FROM biblio.record_entry
56         WHERE id > 0 AND 
57             CASE WHEN $3 IS NULL THEN TRUE 
58                 ELSE last_xact_id = $3 END
59         ORDER BY id;
60 $$ LANGUAGE SQL;
61
62 CREATE FUNCTION evergreen.populate_call_number 
63     (ownlib INTEGER, label TEXT, bib_tag TEXT)
64 RETURNS void AS $$
65     SELECT evergreen.populate_call_number($1, $2, $3, NULL);
66 $$ LANGUAGE SQL;
67
68 /*
69  * Each copy needs a price to be able to test lost/longoverdue and other
70  * real-life situations. Randomly generate a price between 1.99 and 25.99.
71  */
72 CREATE OR REPLACE FUNCTION evergreen.generate_price() RETURNS FLOAT AS $$
73 BEGIN
74         RETURN trunc(random() * 25 + 1) + .99;
75 END;
76 $$ LANGUAGE 'plpgsql';
77
78 /*
79  * create a copy for every callnumber in the database whose label and owning_lib 
80  * matches, appending the callnumber ID to the copy barcode to differentate.
81  */
82 CREATE FUNCTION evergreen.populate_copy 
83     (circlib INTEGER, ownlib INTEGER, barcode TEXT, label TEXT)
84 RETURNS void AS $$
85     INSERT INTO asset.copy (call_number, circ_lib, creator, editor, loan_duration, fine_level, price, barcode)
86         SELECT id, $1, 1, 1, 1, 1, (SELECT evergreen.generate_price()), $3 || id::text
87         FROM asset.call_number
88         WHERE record > 0 AND label LIKE $4 || '%' AND owning_lib = $2
89         ORDER BY id;
90 $$ LANGUAGE SQL;
91
92 /** Returns the next (by ID) non-deleted asset.copy */
93 CREATE FUNCTION evergreen.next_copy (copy_id BIGINT) RETURNS asset.copy AS $$
94     SELECT * FROM asset.copy 
95     WHERE id > $1 AND NOT deleted
96     ORDER BY id LIMIT 1;
97 $$ LANGUAGE SQL;
98
99 /** Returns the next (by ID) non-deleted biblio.record_entry */
100 CREATE FUNCTION evergreen.next_bib (bib_id BIGINT) RETURNS biblio.record_entry AS $$
101     SELECT * FROM biblio.record_entry
102     WHERE id > $1 AND NOT deleted
103     ORDER BY id LIMIT 1;
104 $$ LANGUAGE SQL;
105
106
107 /** Create one circulation */
108 CREATE FUNCTION evergreen.populate_circ (
109     patron_id INTEGER,
110     staff_id INTEGER,
111     copy_id BIGINT,
112     circ_lib INTEGER,
113     duration_rule TEXT,
114     recurring_fine_rule TEXT,
115     max_fine_rule TEXT,
116     overdue BOOLEAN
117 )
118
119 RETURNS void AS $$
120     DECLARE duration config.rule_circ_duration%ROWTYPE;
121     DECLARE recurring config.rule_recurring_fine%ROWTYPE;
122     DECLARE max_fine config.rule_max_fine%ROWTYPE;
123     DECLARE patron actor.usr%ROWTYPE;
124     DECLARE xact_base_date TIMESTAMP;
125     DECLARE due_date TIMESTAMP;
126     DECLARE xact_start TIMESTAMP;
127 BEGIN
128
129     SELECT INTO duration * FROM config.rule_circ_duration WHERE name = duration_rule;
130     SELECT INTO recurring * FROM config.rule_recurring_fine WHERE name = recurring_fine_rule;
131     SELECT INTO max_fine * FROM config.rule_max_fine WHERE name = max_fine_rule;
132     SELECT INTO patron * FROM actor.usr WHERE id = patron_id;
133
134     IF patron.expire_date < NOW() THEN
135         xact_base_date = patron.expire_date;
136     ELSE
137         xact_base_date = NOW();
138     END IF;
139
140     IF overdue THEN
141         -- if duration is '7 days', the overdue item was due 7 days ago
142         due_date := xact_base_date - duration.normal;
143         -- make overdue circs appear as if they were created two durations ago
144         xact_start := xact_base_date - duration.normal - duration.normal;
145     ELSE
146         due_date := xact_base_date + duration.normal;
147         xact_start := xact_base_date;
148     END IF;
149
150     IF duration.normal >= '1 day'::INTERVAL THEN
151         due_date := (DATE(due_date) || ' 23:59:59')::TIMESTAMP;
152     END IF;
153
154     INSERT INTO action.circulation (
155         xact_start, usr, target_copy, circ_lib, circ_staff, renewal_remaining,
156         grace_period, duration, recurring_fine, max_fine, duration_rule,
157         recurring_fine_rule, max_fine_rule, due_date )
158     VALUES (
159         xact_start,
160         patron_id,
161         copy_id,
162         circ_lib, 
163         staff_id,
164         duration.max_renewals,
165         recurring.grace_period,
166         duration.normal,
167         recurring.normal,
168         max_fine.amount,
169         duration.name,
170         recurring.name,
171         max_fine.name,
172         due_date
173     );
174
175     -- mark copy as checked out
176     UPDATE asset.copy SET status = 1 WHERE id = copy_id;
177
178 END;
179 $$ LANGUAGE PLPGSQL;
180
181
182
183 /** Create one hold */
184 CREATE FUNCTION evergreen.populate_hold (
185     hold_type TEXT,
186     target BIGINT,
187     patron_id INTEGER,
188     requestor INTEGER,
189     pickup_lib INTEGER,
190     frozen BOOLEAN,
191     thawdate TIMESTAMP WITH TIME ZONE,
192     holdable_formats TEXT DEFAULT NULL
193 ) RETURNS void AS $$
194 BEGIN
195     INSERT INTO action.hold_request (
196         requestor, hold_type, target, usr, pickup_lib, 
197             request_lib, selection_ou, frozen, thaw_date, holdable_formats)
198     VALUES (
199         requestor,
200         hold_type,
201         target,
202         patron_id,
203         pickup_lib,
204         pickup_lib,
205         pickup_lib,
206         frozen,
207         thawdate,
208         holdable_formats
209     );
210
211     -- Create hold notes for staff-placed holds: 1 public, 1 private
212     IF requestor != patron_id THEN
213         INSERT INTO action.hold_request_note (hold, title, body, pub, staff)
214             VALUES (
215                currval('action.hold_request_id_seq'),
216                'Public: Title of hold# ' || currval('action.hold_request_id_seq'),
217                'Public: Hold note body for ' || currval('action.hold_request_id_seq'),
218                TRUE, TRUE
219             ), (
220                currval('action.hold_request_id_seq'),
221                'Private: title of hold# ' || currval('action.hold_request_id_seq'),
222                'Private: Hold note body for ' || currval('action.hold_request_id_seq'),
223                FALSE, TRUE 
224             );
225     END IF;
226 END;
227 $$ LANGUAGE PLPGSQL;
228
229
230
231 /**
232  * Create a booking resource type for all 
233  * bib records with a specific last_xact_id
234 */
235 CREATE FUNCTION evergreen.populate_booking_resource_type        
236     (ownlib INTEGER, bib_tag TEXT)
237 RETURNS void AS $$
238     INSERT INTO booking.resource_type(name, owner, catalog_item, transferable, record)
239         SELECT TRIM (' ./' FROM (XPATH(
240                 '//marc:datafield[@tag="245"]/marc:subfield[@code="a"]/text()',
241                 marc::XML,
242                 ARRAY[ARRAY['marc', 'http://www.loc.gov/MARC21/slim']]
243         ))[1]::TEXT),
244         $1, True, True, id
245         FROM biblio.record_entry
246         WHERE id > 0 AND last_xact_id = $2;
247 $$ LANGUAGE SQL;
248
249 /**
250  * Make all items with barcodes that start 
251  * with a certain substring bookable
252 */
253
254 CREATE FUNCTION evergreen.populate_booking_resource
255     (barcode_start TEXT)
256 RETURNS void AS $$
257     INSERT INTO booking.resource(owner, type, barcode)
258         SELECT circ_lib, resource_type.id, barcode FROM asset.copy
259         INNER JOIN asset.call_number on copy.call_number=call_number.id
260         INNER JOIN booking.resource_type on call_number.record=resource_type.record
261         WHERE barcode LIKE $1 || '%';
262 $$ LANGUAGE SQL;
263