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