]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/040.schema.asset.sql
update record merging stored proc to move holds and metarecords, and count all moved...
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 040.schema.asset.sql
1 /*
2  * Copyright (C) 2004-2008  Georgia Public Library Service
3  * Copyright (C) 2007-2008  Equinox Software, Inc.
4  * Mike Rylander <miker@esilibrary.com> 
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 DROP SCHEMA asset CASCADE;
19
20 BEGIN;
21
22 CREATE SCHEMA asset;
23
24 CREATE TABLE asset.copy_location (
25         id              SERIAL  PRIMARY KEY,
26         name            TEXT    NOT NULL,
27         owning_lib      INT     NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
28         holdable        BOOL    NOT NULL DEFAULT TRUE,
29         hold_verify     BOOL    NOT NULL DEFAULT FALSE,
30         opac_visible    BOOL    NOT NULL DEFAULT TRUE,
31         circulate       BOOL    NOT NULL DEFAULT TRUE
32 );
33
34 CREATE TABLE asset.copy (
35         id              BIGSERIAL                       PRIMARY KEY,
36         circ_lib        INT                             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
37         creator         BIGINT                          NOT NULL,
38         call_number     BIGINT                          NOT NULL,
39         editor          BIGINT                          NOT NULL,
40         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
41         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
42         copy_number     INT,
43         status          INT                             NOT NULL DEFAULT 0 REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
44         location        INT                             NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
45         loan_duration   INT                             NOT NULL CHECK ( loan_duration IN (1,2,3) ),
46         fine_level      INT                             NOT NULL CHECK ( fine_level IN (1,2,3) ),
47         age_protect     INT,
48         circulate       BOOL                            NOT NULL DEFAULT TRUE,
49         deposit         BOOL                            NOT NULL DEFAULT FALSE,
50         ref             BOOL                            NOT NULL DEFAULT FALSE,
51         holdable        BOOL                            NOT NULL DEFAULT TRUE,
52         deposit_amount  NUMERIC(6,2)                    NOT NULL DEFAULT 0.00,
53         price           NUMERIC(8,2),
54         barcode         TEXT                            NOT NULL,
55         circ_modifier   TEXT,
56         circ_as_type    TEXT,
57         dummy_title     TEXT,
58         dummy_author    TEXT,
59         alert_message   TEXT,
60         opac_visible    BOOL                            NOT NULL DEFAULT TRUE,
61         deleted         BOOL                            NOT NULL DEFAULT FALSE
62 );
63 CREATE UNIQUE INDEX copy_barcode_key ON asset.copy (barcode) WHERE deleted IS FALSE;
64 CREATE INDEX cp_cn_idx ON asset.copy (call_number);
65 CREATE INDEX cp_avail_cn_idx ON asset.copy (call_number);
66 CREATE RULE protect_copy_delete AS ON DELETE TO asset.copy DO INSTEAD UPDATE asset.copy SET deleted = TRUE WHERE OLD.id = asset.copy.id;
67
68 CREATE TABLE asset.copy_transparency (
69         id              SERIAL          PRIMARY KEY,
70         deposit_amount  NUMERIC(6,2),
71         owner           INT             NOT NULL REFERENCES actor.org_unit (id),
72         circ_lib        INT             REFERENCES actor.org_unit (id),
73         loan_duration   INT             CHECK ( loan_duration IN (1,2,3) ),
74         fine_level      INT             CHECK ( fine_level IN (1,2,3) ),
75         holdable        BOOL,
76         circulate       BOOL,
77         deposit         BOOL,
78         ref             BOOL,
79         opac_visible    BOOL,
80         circ_modifier   TEXT,
81         circ_as_type    TEXT,
82         name            TEXT            NOT NULL,
83         CONSTRAINT scte_name_once_per_lib UNIQUE (owner,name)
84 );
85
86 CREATE TABLE asset.copy_tranparency_map (
87         id              BIGSERIAL       PRIMARY KEY,
88         tansparency     INT     NOT NULL REFERENCES asset.copy_transparency (id),
89         target_copy     INT     NOT NULL UNIQUE REFERENCES asset.copy (id)
90 );
91 CREATE INDEX cp_tr_cp_idx ON asset.copy_tranparency_map (tansparency);
92
93 CREATE TABLE asset.stat_cat_entry_transparency_map (
94         id                      BIGSERIAL       PRIMARY KEY,
95         stat_cat                INT             NOT NULL, -- needs ON DELETE CASCADE
96         stat_cat_entry          INT             NOT NULL, -- needs ON DELETE CASCADE
97         owning_transparency     INT             NOT NULL, -- needs ON DELETE CASCADE
98         CONSTRAINT scte_once_per_trans UNIQUE (owning_transparency,stat_cat)
99 );
100
101 CREATE TABLE asset.stat_cat (
102         id              SERIAL  PRIMARY KEY,
103         owner           INT     NOT NULL,
104         opac_visible    BOOL    NOT NULL DEFAULT FALSE,
105         name            TEXT    NOT NULL,
106         CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
107 );
108
109 CREATE TABLE asset.stat_cat_entry (
110         id              SERIAL  PRIMARY KEY,
111         stat_cat        INT     NOT NULL,
112         owner           INT     NOT NULL,
113         value           TEXT    NOT NULL,
114         CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
115 );
116
117 CREATE TABLE asset.stat_cat_entry_copy_map (
118         id              BIGSERIAL       PRIMARY KEY,
119         stat_cat        INT             NOT NULL,
120         stat_cat_entry  INT             NOT NULL,
121         owning_copy     BIGINT          NOT NULL,
122         CONSTRAINT sce_once_per_copy UNIQUE (owning_copy,stat_cat)
123 );
124
125 CREATE TABLE asset.copy_note (
126         id              BIGSERIAL                       PRIMARY KEY,
127         owning_copy     BIGINT                          NOT NULL,
128         creator         BIGINT                          NOT NULL,
129         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
130         pub             BOOL                            NOT NULL DEFAULT FALSE,
131         title           TEXT                            NOT NULL,
132         value           TEXT                            NOT NULL
133 );
134
135 CREATE TABLE asset.call_number (
136         id              bigserial PRIMARY KEY,
137         creator         BIGINT                          NOT NULL,
138         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
139         editor          BIGINT                          NOT NULL,
140         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
141         record          bigint                          NOT NULL,
142         owning_lib      INT                             NOT NULL,
143         label           TEXT                            NOT NULL,
144         deleted         BOOL                            NOT NULL DEFAULT FALSE
145 );
146 CREATE INDEX asset_call_number_record_idx ON asset.call_number (record);
147 CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator);
148 CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor);
149 CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label));
150 CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (upper(label),id,owning_lib);
151 CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label) WHERE deleted IS FALSE;
152 CREATE RULE protect_cn_delete AS ON DELETE TO asset.call_number DO INSTEAD UPDATE asset.call_number SET deleted = TRUE WHERE OLD.id = asset.call_number.id;
153
154 CREATE TABLE asset.call_number_note (
155         id              BIGSERIAL                       PRIMARY KEY,
156         call_number     BIGINT                          NOT NULL,
157         creator         BIGINT                          NOT NULL,
158         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
159         pub             BOOL                            NOT NULL DEFAULT FALSE,
160         title           TEXT                            NOT NULL,
161         value           TEXT                            NOT NULL
162 );
163
164 CREATE VIEW stats.fleshed_copy AS 
165         SELECT  cp.*,
166                 CAST(cp.create_date AS DATE) AS create_date_day,
167                 CAST(cp.edit_date AS DATE) AS edit_date_day,
168                 DATE_TRUNC('hour', cp.create_date) AS create_date_hour,
169                 DATE_TRUNC('hour', cp.edit_date) AS edit_date_hour,
170                 cn.label AS call_number_label,
171                 cn.owning_lib,
172                 rd.item_lang,
173                 rd.item_type,
174                 rd.item_form
175         FROM    asset.copy cp
176                 JOIN asset.call_number cn ON (cp.call_number = cn.id)
177                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
178
179 CREATE VIEW stats.fleshed_call_number AS 
180         SELECT  cn.*,
181                 CAST(cn.create_date AS DATE) AS create_date_day,
182                 CAST(cn.edit_date AS DATE) AS edit_date_day,
183                 DATE_TRUNC('hour', cn.create_date) AS create_date_hour,
184                 DATE_TRUNC('hour', cn.edit_date) AS edit_date_hour,
185                 rd.item_lang,
186                 rd.item_type,
187                 rd.item_form
188         FROM    asset.call_number cn
189                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
190
191 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
192 DECLARE
193         moved_objects INT := 0;
194         source_cn     asset.call_number%ROWTYPE;
195         target_cn     asset.call_number%ROWTYPE;
196         metarec       metabib.metarecord%ROWTYPE;
197         hold          action.hold_request%ROWTYPE;
198 BEGIN
199         -- Find and move metarecords to the target record
200         SELECT  INTO metarec *
201           FROM  metabib.metarecord
202           WHERE master_record = source_record;
203
204         IF FOUND THEN
205                 UPDATE  metabib.metarecord
206                   SET   master_record = target_record,
207                         mods = NULL
208                   WHERE id = metarec.id;
209
210                 moved_objects := moved_objects + 1;
211         END IF;
212
213         -- Find call numbers attached to the source ...
214         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
215
216                 SELECT  INTO target_cn *
217                   FROM  asset.call_number
218                   WHERE label = source_cn.label
219                         AND owning_lib = source_cn.owning_lib
220                         AND record = target_record;
221
222                 -- ... and if there's a conflicting one on the target ...
223                 IF FOUND THEN
224
225                         -- ... move the copies to that, and ...
226                         UPDATE  asset.copy
227                           SET   call_number = target_cn.id
228                           WHERE call_number = source_cn.id;
229
230                         -- ... move V holds to the move-target call number
231                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
232                 
233                                 UPDATE  action.hold_request
234                                   SET   target = target_cn.id
235                                   WHERE id = hold.id;
236                 
237                                 moved_objects := moved_objects + 1;
238                         END LOOP;
239
240                 -- ... if not ...
241                 ELSE
242                         -- ... just move the call number to the target record
243                         UPDATE  asset.call_number
244                           SET   record = target_record
245                           WHERE id = source_cn.id;
246                 END IF;
247
248                 moved_objects := moved_objects + 1;
249         END LOOP;
250
251         -- Find T holds targeting the source record ...
252         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
253
254                 -- ... and move them to the target record
255                 UPDATE  action.hold_request
256                   SET   target = target_record
257                   WHERE id = hold.id;
258
259                 moved_objects := moved_objects + 1;
260         END LOOP;
261
262         -- That's all, folks!
263         RETURN moved_objects;
264 END;
265 $func$ LANGUAGE plpgsql;
266
267 COMMIT;
268