]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/040.schema.asset.sql
initial addition of Conifer-sponsored electronic serials support. tests and docs...
[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         CONSTRAINT acl_name_once_per_lib UNIQUE (name, owning_lib)
33 );
34
35 CREATE TABLE asset.copy (
36         id              BIGSERIAL                       PRIMARY KEY,
37         circ_lib        INT                             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
38         creator         BIGINT                          NOT NULL,
39         call_number     BIGINT                          NOT NULL,
40         editor          BIGINT                          NOT NULL,
41         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
42         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
43         copy_number     INT,
44         status          INT                             NOT NULL DEFAULT 0 REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
45         location        INT                             NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
46         loan_duration   INT                             NOT NULL CHECK ( loan_duration IN (1,2,3) ),
47         fine_level      INT                             NOT NULL CHECK ( fine_level IN (1,2,3) ),
48         age_protect     INT,
49         circulate       BOOL                            NOT NULL DEFAULT TRUE,
50         deposit         BOOL                            NOT NULL DEFAULT FALSE,
51         ref             BOOL                            NOT NULL DEFAULT FALSE,
52         holdable        BOOL                            NOT NULL DEFAULT TRUE,
53         deposit_amount  NUMERIC(6,2)                    NOT NULL DEFAULT 0.00,
54         price           NUMERIC(8,2),
55         barcode         TEXT                            NOT NULL,
56         circ_modifier   TEXT,
57         circ_as_type    TEXT,
58         dummy_title     TEXT,
59         dummy_author    TEXT,
60         alert_message   TEXT,
61         opac_visible    BOOL                            NOT NULL DEFAULT TRUE,
62         deleted         BOOL                            NOT NULL DEFAULT FALSE
63 );
64 CREATE UNIQUE INDEX copy_barcode_key ON asset.copy (barcode) WHERE deleted IS FALSE;
65 CREATE INDEX cp_cn_idx ON asset.copy (call_number);
66 CREATE INDEX cp_avail_cn_idx ON asset.copy (call_number);
67 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;
68
69 CREATE TABLE asset.copy_transparency (
70         id              SERIAL          PRIMARY KEY,
71         deposit_amount  NUMERIC(6,2),
72         owner           INT             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
73         circ_lib        INT             REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
74         loan_duration   INT             CHECK ( loan_duration IN (1,2,3) ),
75         fine_level      INT             CHECK ( fine_level IN (1,2,3) ),
76         holdable        BOOL,
77         circulate       BOOL,
78         deposit         BOOL,
79         ref             BOOL,
80         opac_visible    BOOL,
81         circ_modifier   TEXT,
82         circ_as_type    TEXT,
83         name            TEXT            NOT NULL,
84         CONSTRAINT scte_name_once_per_lib UNIQUE (owner,name)
85 );
86
87 CREATE TABLE asset.copy_tranparency_map (
88         id              BIGSERIAL       PRIMARY KEY,
89         tansparency     INT     NOT NULL REFERENCES asset.copy_transparency (id) DEFERRABLE INITIALLY DEFERRED,
90         target_copy     INT     NOT NULL UNIQUE REFERENCES asset.copy (id) DEFERRABLE INITIALLY DEFERRED
91 );
92 CREATE INDEX cp_tr_cp_idx ON asset.copy_tranparency_map (tansparency);
93
94 CREATE TABLE asset.stat_cat_entry_transparency_map (
95         id                      BIGSERIAL       PRIMARY KEY,
96         stat_cat                INT             NOT NULL, -- needs ON DELETE CASCADE
97         stat_cat_entry          INT             NOT NULL, -- needs ON DELETE CASCADE
98         owning_transparency     INT             NOT NULL, -- needs ON DELETE CASCADE
99         CONSTRAINT scte_once_per_trans UNIQUE (owning_transparency,stat_cat)
100 );
101
102 CREATE TABLE asset.stat_cat (
103         id              SERIAL  PRIMARY KEY,
104         owner           INT     NOT NULL,
105         opac_visible    BOOL    NOT NULL DEFAULT FALSE,
106         name            TEXT    NOT NULL,
107         CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
108 );
109
110 CREATE TABLE asset.stat_cat_entry (
111         id              SERIAL  PRIMARY KEY,
112         stat_cat        INT     NOT NULL,
113         owner           INT     NOT NULL,
114         value           TEXT    NOT NULL,
115         CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
116 );
117
118 CREATE TABLE asset.stat_cat_entry_copy_map (
119         id              BIGSERIAL       PRIMARY KEY,
120         stat_cat        INT             NOT NULL,
121         stat_cat_entry  INT             NOT NULL,
122         owning_copy     BIGINT          NOT NULL,
123         CONSTRAINT sce_once_per_copy UNIQUE (owning_copy,stat_cat)
124 );
125
126 CREATE TABLE asset.copy_note (
127         id              BIGSERIAL                       PRIMARY KEY,
128         owning_copy     BIGINT                          NOT NULL,
129         creator         BIGINT                          NOT NULL,
130         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
131         pub             BOOL                            NOT NULL DEFAULT FALSE,
132         title           TEXT                            NOT NULL,
133         value           TEXT                            NOT NULL
134 );
135
136 CREATE TABLE asset.uri (
137     id  SERIAL  PRIMARY KEY,
138     href    TEXT    NOT NULL,
139     label   TEXT,
140     use TEXT,
141     active  BOOL    NOT NULL DEFAULT TRUE
142 );
143
144 CREATE TABLE asset.call_number (
145         id              bigserial PRIMARY KEY,
146         creator         BIGINT                          NOT NULL,
147         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
148         editor          BIGINT                          NOT NULL,
149         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
150         record          bigint                          NOT NULL,
151         owning_lib      INT                             NOT NULL,
152         label           TEXT                            NOT NULL,
153         deleted         BOOL                            NOT NULL DEFAULT FALSE
154 );
155 CREATE INDEX asset_call_number_record_idx ON asset.call_number (record);
156 CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator);
157 CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor);
158 CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label));
159 CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (upper(label),id,owning_lib);
160 CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label) WHERE deleted IS FALSE;
161 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;
162
163 CREATE TABLE asset.uri_call_number_map (
164     id          BIGSERIAL   PRIMARY KEY,
165     uri         INT         NOT NULL REFERENCES asset.uri (id),
166     call_number INT         NOT NULL REFERENCES asset.call_number (id),
167     CONSTRAINT uri_cn_once UNIQUE (uri,call_number)
168 );
169
170 CREATE TABLE asset.call_number_note (
171         id              BIGSERIAL                       PRIMARY KEY,
172         call_number     BIGINT                          NOT NULL,
173         creator         BIGINT                          NOT NULL,
174         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
175         pub             BOOL                            NOT NULL DEFAULT FALSE,
176         title           TEXT                            NOT NULL,
177         value           TEXT                            NOT NULL
178 );
179
180 CREATE VIEW stats.fleshed_copy AS 
181         SELECT  cp.*,
182                 CAST(cp.create_date AS DATE) AS create_date_day,
183                 CAST(cp.edit_date AS DATE) AS edit_date_day,
184                 DATE_TRUNC('hour', cp.create_date) AS create_date_hour,
185                 DATE_TRUNC('hour', cp.edit_date) AS edit_date_hour,
186                 cn.label AS call_number_label,
187                 cn.owning_lib,
188                 rd.item_lang,
189                 rd.item_type,
190                 rd.item_form
191         FROM    asset.copy cp
192                 JOIN asset.call_number cn ON (cp.call_number = cn.id)
193                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
194
195 CREATE VIEW stats.fleshed_call_number AS 
196         SELECT  cn.*,
197                 CAST(cn.create_date AS DATE) AS create_date_day,
198                 CAST(cn.edit_date AS DATE) AS edit_date_day,
199                 DATE_TRUNC('hour', cn.create_date) AS create_date_hour,
200                 DATE_TRUNC('hour', cn.edit_date) AS edit_date_hour,
201                 rd.item_lang,
202                 rd.item_type,
203                 rd.item_form
204         FROM    asset.call_number cn
205                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
206
207 COMMIT;
208