]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/040.schema.asset.sql
sped up page_down by about 20%
[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 INSERT INTO asset.copy_location (name,owning_lib) VALUES ('Stacks',1);
16
17 CREATE TABLE asset.copy (
18         id              BIGSERIAL                       PRIMARY KEY,
19         circ_lib        INT                             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
20         creator         BIGINT                          NOT NULL,
21         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
22         editor          BIGINT                          NOT NULL,
23         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
24         barcode         TEXT                            UNIQUE NOT NULL,
25         call_number     BIGINT                          NOT NULL,
26         copy_number     INT,
27         holdable        BOOL                            NOT NULL DEFAULT TRUE,
28         status          INT                             NOT NULL DEFAULT 0 REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
29         location        INT                             NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
30         loan_duration   INT                             NOT NULL CHECK ( loan_duration IN (1,2,3) ),
31         fine_level      INT                             NOT NULL CHECK ( fine_level IN (1,2,3) ),
32         circulate       BOOL                            NOT NULL DEFAULT TRUE,
33         deposit         BOOL                            NOT NULL DEFAULT FALSE,
34         deposit_amount  NUMERIC(6,2)                    NOT NULL DEFAULT 0.00,
35         price           NUMERIC(8,2)                    NOT NULL DEFAULT 0.00,
36         ref             BOOL                            NOT NULL DEFAULT FALSE,
37         circ_modifier   TEXT,
38         circ_as_type    TEXT,
39         opac_visible    BOOL                            NOT NULL DEFAULT TRUE
40 );
41 CREATE INDEX cp_cn_idx ON asset.copy (call_number);
42 CREATE INDEX cp_avail_cn_idx ON asset.copy (call_number) WHERE status = 0;
43
44 CREATE TABLE asset.copy_transparency (
45         id              SERIAL          PRIMARY KEY,
46         deposit_amount  NUMERIC(6,2),
47         owner           INT             NOT NULL REFERENCES actor.org_unit (id),
48         circ_lib        INT             REFERENCES actor.org_unit (id),
49         loan_duration   INT             CHECK ( loan_duration IN (1,2,3) ),
50         fine_level      INT             CHECK ( fine_level IN (1,2,3) ),
51         holdable        BOOL,
52         circulate       BOOL,
53         deposit         BOOL,
54         ref             BOOL,
55         opac_visible    BOOL,
56         circ_modifier   TEXT,
57         circ_as_type    TEXT,
58         name            TEXT            NOT NULL,
59         CONSTRAINT scte_name_once_per_lib UNIQUE (owner,name)
60 );
61
62 CREATE TABLE asset.copy_tranparency_map (
63         id              BIGSERIAL       PRIMARY KEY,
64         tansparency     INT     NOT NULL REFERENCES asset.copy_transparency (id),
65         target_copy     INT     NOT NULL UNIQUE REFERENCES asset.copy (id)
66 );
67 CREATE INDEX cp_tr_cp_idx ON asset.copy_tranparency_map (tansparency);
68
69 CREATE TABLE asset.stat_cat_entry_transparency_map (
70         id                      BIGSERIAL       PRIMARY KEY,
71         stat_cat                INT             NOT NULL, -- needs ON DELETE CASCADE
72         stat_cat_entry          INT             NOT NULL, -- needs ON DELETE CASCADE
73         owning_transparency     INT             NOT NULL, -- needs ON DELETE CASCADE
74         CONSTRAINT scte_once_per_trans UNIQUE (owning_transparency,stat_cat)
75 );
76
77 CREATE TABLE asset.stat_cat (
78         id              SERIAL  PRIMARY KEY,
79         owner           INT     NOT NULL,
80         opac_visible    BOOL    NOT NULL DEFAULT FALSE,
81         name            TEXT    NOT NULL,
82         CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
83 );
84
85 CREATE TABLE asset.stat_cat_entry (
86         id              SERIAL  PRIMARY KEY,
87         stat_cat        INT     NOT NULL,
88         owner           INT     NOT NULL,
89         value           TEXT    NOT NULL,
90         CONSTRAINT sce_once_per_owner UNIQUE (owner,value)
91 );
92
93 CREATE TABLE asset.stat_cat_entry_copy_map (
94         id              BIGSERIAL       PRIMARY KEY,
95         stat_cat        INT             NOT NULL,
96         stat_cat_entry  INT             NOT NULL,
97         owning_copy     BIGINT          NOT NULL,
98         CONSTRAINT sce_once_per_copy UNIQUE (owning_copy,stat_cat)
99 );
100
101 CREATE TABLE asset.copy_note (
102         id              BIGSERIAL                       PRIMARY KEY,
103         owning_copy     BIGINT                          NOT NULL,
104         creator         BIGINT                          NOT NULL,
105         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
106         title           TEXT                            NOT NULL,
107         value           TEXT                            NOT NULL
108 );
109
110 CREATE TABLE asset.call_number (
111         id              bigserial PRIMARY KEY,
112         creator         BIGINT                          NOT NULL,
113         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
114         editor          BIGINT                          NOT NULL,
115         edit_date       TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
116         record          bigint                          NOT NULL,
117         owning_lib      INT                             NOT NULL,
118         label           TEXT                            NOT NULL,
119         CONSTRAINT asset_call_number_label_once_per_lib UNIQUE (record, owning_lib, label)
120 );
121 CREATE INDEX asset_call_number_record_idx ON asset.call_number (record);
122 CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator);
123 CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor);
124 CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label));
125 CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (upper(label),id,owning_lib);
126
127 CREATE TABLE asset.call_number_note (
128         id              BIGSERIAL                       PRIMARY KEY,
129         call_number     BIGINT                          NOT NULL,
130         creator         BIGINT                          NOT NULL,
131         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
132         title           TEXT                            NOT NULL,
133         value           TEXT                            NOT NULL
134 );
135
136
137 CREATE VIEW stats.fleshed_copy AS 
138         SELECT  cp.*,
139                 CAST(cp.create_date AS DATE) AS create_date_day,
140                 CAST(cp.edit_date AS DATE) AS edit_date_day,
141                 DATE_TRUNC('hour', cp.create_date) AS create_date_hour,
142                 DATE_TRUNC('hour', cp.edit_date) AS edit_date_hour,
143                 cn.label AS call_number_label,
144                 cn.owning_lib,
145                 rd.item_lang,
146                 rd.item_type,
147                 rd.item_form
148         FROM    asset.copy cp
149                 JOIN asset.call_number cn ON (cp.call_number = cn.id)
150                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
151
152 CREATE VIEW stats.fleshed_call_number AS 
153         SELECT  cn.*,
154                 CAST(cn.create_date AS DATE) AS create_date_day,
155                 CAST(cn.edit_date AS DATE) AS edit_date_day,
156                 DATE_TRUNC('hour', cn.create_date) AS create_date_hour,
157                 DATE_TRUNC('hour', cn.edit_date) AS edit_date_hour,
158                 rd.item_lang,
159                 rd.item_type,
160                 rd.item_form
161         FROM    asset.call_number cn
162                 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
163
164
165 COMMIT;