]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/040.schema.asset.sql
cache OPAC-visibility of copies and bibs
[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 IF EXISTS 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         CONSTRAINT acl_name_once_per_lib UNIQUE (name, owning_lib)
33 );
34
35 CREATE TABLE asset.copy_location_order
36 (
37         id              SERIAL           PRIMARY KEY,
38         location        INT              NOT NULL
39                                              REFERENCES asset.copy_location
40                                              ON DELETE CASCADE
41                                              DEFERRABLE INITIALLY DEFERRED,
42         org             INT              NOT NULL
43                                              REFERENCES actor.org_unit
44                                              ON DELETE CASCADE
45                                              DEFERRABLE INITIALLY DEFERRED,
46         position        INT              NOT NULL DEFAULT 0,
47         CONSTRAINT acplo_once_per_org UNIQUE ( location, org )
48 );
49
50 CREATE TABLE asset.copy (
51         id              BIGSERIAL                       PRIMARY KEY,
52         circ_lib        INT                             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
53         creator         BIGINT                          NOT NULL,
54         call_number     BIGINT                          NOT NULL,
55         editor          BIGINT                          NOT NULL,
56         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
57         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
58         copy_number     INT,
59         status          INT                             NOT NULL DEFAULT 0 REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
60         location        INT                             NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
61         loan_duration   INT                             NOT NULL CHECK ( loan_duration IN (1,2,3) ),
62         fine_level      INT                             NOT NULL CHECK ( fine_level IN (1,2,3) ),
63         age_protect     INT,
64         circulate       BOOL                            NOT NULL DEFAULT TRUE,
65         deposit         BOOL                            NOT NULL DEFAULT FALSE,
66         ref             BOOL                            NOT NULL DEFAULT FALSE,
67         holdable        BOOL                            NOT NULL DEFAULT TRUE,
68         deposit_amount  NUMERIC(6,2)                    NOT NULL DEFAULT 0.00,
69         price           NUMERIC(8,2),
70         barcode         TEXT                            NOT NULL,
71         circ_modifier   TEXT,
72         circ_as_type    TEXT,
73         dummy_title     TEXT,
74         dummy_author    TEXT,
75         alert_message   TEXT,
76         opac_visible    BOOL                            NOT NULL DEFAULT TRUE,
77         deleted         BOOL                            NOT NULL DEFAULT FALSE,
78         floating                BOOL                            NOT NULL DEFAULT FALSE,
79         dummy_isbn      TEXT,
80         status_changed_time TIMESTAMP WITH TIME ZONE,
81         mint_condition      BOOL        NOT NULL DEFAULT TRUE
82 );
83 CREATE UNIQUE INDEX copy_barcode_key ON asset.copy (barcode) WHERE deleted = FALSE OR deleted IS FALSE;
84 CREATE INDEX cp_cn_idx ON asset.copy (call_number);
85 CREATE INDEX cp_avail_cn_idx ON asset.copy (call_number);
86 CREATE INDEX cp_creator_idx  ON asset.copy ( creator );
87 CREATE INDEX cp_editor_idx   ON asset.copy ( editor );
88 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;
89
90 CREATE TABLE asset.opac_visible_copies (
91   id        BIGINT primary key, -- copy id
92   record    BIGINT,
93   circ_lib  INTEGER
94 );
95 COMMENT ON TABLE asset.opac_visible_copies IS $$
96 Materialized view of copies that are visible in the OPAC, used by
97 search.query_parser_fts() to speed up OPAC visibility checks on large
98 databases.  Contents are maintained by a set of triggers.
99 $$;
100 CREATE INDEX opac_visible_copies_idx1 on asset.opac_visible_copies (record, circ_lib);
101
102 CREATE OR REPLACE FUNCTION asset.acp_status_changed()
103 RETURNS TRIGGER AS $$
104 BEGIN
105     IF NEW.status <> OLD.status THEN
106         NEW.status_changed_time := now();
107     END IF;
108     RETURN NEW;
109 END;
110 $$ LANGUAGE plpgsql;
111
112 CREATE TRIGGER acp_status_changed_trig
113     BEFORE UPDATE ON asset.copy
114     FOR EACH ROW EXECUTE PROCEDURE asset.acp_status_changed();
115
116 CREATE TABLE asset.stat_cat_entry_transparency_map (
117         id                      BIGSERIAL       PRIMARY KEY,
118         stat_cat                INT             NOT NULL, -- needs ON DELETE CASCADE
119         stat_cat_entry          INT             NOT NULL, -- needs ON DELETE CASCADE
120         owning_transparency     INT             NOT NULL, -- needs ON DELETE CASCADE
121         CONSTRAINT scte_once_per_trans UNIQUE (owning_transparency,stat_cat)
122 );
123
124 CREATE TABLE asset.stat_cat (
125         id              SERIAL  PRIMARY KEY,
126         owner           INT     NOT NULL,
127         opac_visible    BOOL    NOT NULL DEFAULT FALSE,
128         name            TEXT    NOT NULL,
129         CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
130 );
131
132 CREATE TABLE asset.stat_cat_entry (
133         id              SERIAL  PRIMARY KEY,
134         stat_cat        INT     NOT NULL,
135         owner           INT     NOT NULL,
136         value           TEXT    NOT NULL,
137         CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
138 );
139
140 CREATE TABLE asset.stat_cat_entry_copy_map (
141         id              BIGSERIAL       PRIMARY KEY,
142         stat_cat        INT             NOT NULL,
143         stat_cat_entry  INT             NOT NULL,
144         owning_copy     BIGINT          NOT NULL,
145         CONSTRAINT sce_once_per_copy UNIQUE (owning_copy,stat_cat)
146 );
147 CREATE INDEX scecm_owning_copy_idx ON asset.stat_cat_entry_copy_map(owning_copy);
148
149 CREATE TABLE asset.copy_note (
150         id              BIGSERIAL                       PRIMARY KEY,
151         owning_copy     BIGINT                          NOT NULL,
152         creator         BIGINT                          NOT NULL,
153         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
154         pub             BOOL                            NOT NULL DEFAULT FALSE,
155         title           TEXT                            NOT NULL,
156         value           TEXT                            NOT NULL
157 );
158 CREATE INDEX asset_copy_note_creator_idx ON asset.copy_note ( creator );
159
160 CREATE TABLE asset.uri (
161     id  SERIAL  PRIMARY KEY,
162     href    TEXT    NOT NULL,
163     label   TEXT,
164     use_restriction TEXT,
165     active  BOOL    NOT NULL DEFAULT TRUE
166 );
167
168 CREATE TABLE asset.call_number (
169         id              bigserial PRIMARY KEY,
170         creator         BIGINT                          NOT NULL,
171         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
172         editor          BIGINT                          NOT NULL,
173         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
174         record          bigint                          NOT NULL,
175         owning_lib      INT                             NOT NULL,
176         label           TEXT                            NOT NULL,
177         deleted         BOOL                            NOT NULL DEFAULT FALSE
178 );
179 CREATE INDEX asset_call_number_record_idx ON asset.call_number (record);
180 CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator);
181 CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor);
182 CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label));
183 CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (upper(label),id,owning_lib);
184 CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label) WHERE deleted = FALSE OR deleted IS FALSE;
185 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;
186
187 CREATE TABLE asset.uri_call_number_map (
188     id          BIGSERIAL   PRIMARY KEY,
189     uri         INT         NOT NULL REFERENCES asset.uri (id),
190     call_number INT         NOT NULL REFERENCES asset.call_number (id),
191     CONSTRAINT uri_cn_once UNIQUE (uri,call_number)
192 );
193 CREATE INDEX asset_uri_call_number_map_cn_idx ON asset.uri_call_number_map (call_number);
194
195 CREATE TABLE asset.call_number_note (
196         id              BIGSERIAL                       PRIMARY KEY,
197         call_number     BIGINT                          NOT NULL,
198         creator         BIGINT                          NOT NULL,
199         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
200         pub             BOOL                            NOT NULL DEFAULT FALSE,
201         title           TEXT                            NOT NULL,
202         value           TEXT                            NOT NULL
203 );
204 CREATE INDEX asset_call_number_note_creator_idx ON asset.call_number_note ( creator );
205
206 CREATE TABLE asset.copy_template (
207         id             SERIAL   PRIMARY KEY,
208         owning_lib     INT      NOT NULL
209                                 REFERENCES actor.org_unit (id)
210                                 DEFERRABLE INITIALLY DEFERRED,
211         creator        BIGINT   NOT NULL
212                                 REFERENCES actor.usr (id)
213                                 DEFERRABLE INITIALLY DEFERRED,
214         editor         BIGINT   NOT NULL
215                                 REFERENCES actor.usr (id)
216                                 DEFERRABLE INITIALLY DEFERRED,
217         create_date    TIMESTAMP WITH TIME ZONE    DEFAULT NOW(),
218         edit_date      TIMESTAMP WITH TIME ZONE    DEFAULT NOW(),
219         name           TEXT     NOT NULL,
220         -- columns above this point are attributes of the template itself
221         -- columns after this point are attributes of the copy this template modifies/creates
222         circ_lib       INT      REFERENCES actor.org_unit (id)
223                                 DEFERRABLE INITIALLY DEFERRED,
224         status         INT      REFERENCES config.copy_status (id)
225                                 DEFERRABLE INITIALLY DEFERRED,
226         location       INT      REFERENCES asset.copy_location (id)
227                                 DEFERRABLE INITIALLY DEFERRED,
228         loan_duration  INT      CONSTRAINT valid_loan_duration CHECK (
229                                     loan_duration IS NULL OR loan_duration IN (1,2,3)),
230         fine_level     INT      CONSTRAINT valid_fine_level CHECK (
231                                     fine_level IS NULL OR loan_duration IN (1,2,3)),
232         age_protect    INT,
233         circulate      BOOL,
234         deposit        BOOL,
235         ref            BOOL,
236         holdable       BOOL,
237         deposit_amount NUMERIC(6,2),
238         price          NUMERIC(8,2),
239         circ_modifier  TEXT,
240         circ_as_type   TEXT,
241         alert_message  TEXT,
242         opac_visible   BOOL,
243         floating       BOOL,
244         mint_condition BOOL
245 );
246
247 CREATE VIEW stats.fleshed_copy AS 
248         SELECT  cp.*,
249                 CAST(cp.create_date AS DATE) AS create_date_day,
250                 CAST(cp.edit_date AS DATE) AS edit_date_day,
251                 DATE_TRUNC('hour', cp.create_date) AS create_date_hour,
252                 DATE_TRUNC('hour', cp.edit_date) AS edit_date_hour,
253                 cn.label AS call_number_label,
254                 cn.owning_lib,
255                 rd.item_lang,
256                 rd.item_type,
257                 rd.item_form
258         FROM    asset.copy cp
259                 JOIN asset.call_number cn ON (cp.call_number = cn.id)
260                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
261
262 CREATE VIEW stats.fleshed_call_number AS 
263         SELECT  cn.*,
264                 CAST(cn.create_date AS DATE) AS create_date_day,
265                 CAST(cn.edit_date AS DATE) AS edit_date_day,
266                 DATE_TRUNC('hour', cn.create_date) AS create_date_hour,
267                 DATE_TRUNC('hour', cn.edit_date) AS edit_date_hour,
268                 rd.item_lang,
269                 rd.item_type,
270                 rd.item_form
271         FROM    asset.call_number cn
272                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
273
274 COMMIT;
275