]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/090.schema.action.sql
indexes supporting collections queries
[Evergreen.git] / Open-ILS / src / sql / Pg / 090.schema.action.sql
1 DROP SCHEMA action CASCADE;
2
3 BEGIN;
4
5 CREATE SCHEMA action;
6
7 CREATE TABLE action.in_house_use (
8         id              SERIAL                          PRIMARY KEY,
9         item            BIGINT                          NOT NULL REFERENCES asset.copy (id),
10         staff           INT                             NOT NULL REFERENCES actor.usr (id),
11         org_unit        INT                             NOT NULL REFERENCES actor.org_unit (id),
12         use_time        TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW()
13 );
14
15 CREATE TABLE action.non_cataloged_circulation (
16         id              SERIAL                          PRIMARY KEY,
17         patron          INT                             NOT NULL REFERENCES actor.usr (id),
18         staff           INT                             NOT NULL REFERENCES actor.usr (id),
19         circ_lib        INT                             NOT NULL REFERENCES actor.org_unit (id),
20         item_type       INT                             NOT NULL REFERENCES config.non_cataloged_type (id),
21         circ_time       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW()
22 );
23
24 CREATE TABLE action.non_cat_in_house_use (
25         id              SERIAL                          PRIMARY KEY,
26         item_type       BIGINT                          NOT NULL REFERENCES config.non_cataloged_type(id),
27         staff           INT                             NOT NULL REFERENCES actor.usr (id),
28         org_unit        INT                             NOT NULL REFERENCES actor.org_unit (id),
29         use_time        TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW()
30 );
31
32 CREATE TABLE action.survey (
33         id              SERIAL                          PRIMARY KEY,
34         owner           INT                             NOT NULL REFERENCES actor.org_unit (id),
35         start_date      TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW(),
36         end_date        TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW() + '10 years'::INTERVAL,
37         usr_summary     BOOL                            NOT NULL DEFAULT FALSE,
38         opac            BOOL                            NOT NULL DEFAULT FALSE,
39         poll            BOOL                            NOT NULL DEFAULT FALSE,
40         required        BOOL                            NOT NULL DEFAULT FALSE,
41         name            TEXT                            NOT NULL,
42         description     TEXT                            NOT NULL
43 );
44 CREATE UNIQUE INDEX asv_once_per_owner_idx ON action.survey (owner,name);
45
46 CREATE TABLE action.survey_question (
47         id              SERIAL  PRIMARY KEY,
48         survey          INT     NOT NULL REFERENCES action.survey,
49         question        TEXT    NOT NULL
50 );
51
52 CREATE TABLE action.survey_answer (
53         id              SERIAL  PRIMARY KEY,
54         question        INT     NOT NULL REFERENCES action.survey_question,
55         answer          TEXT    NOT NULL
56 );
57
58 CREATE SEQUENCE action.survey_response_group_id_seq;
59
60 CREATE TABLE action.survey_response (
61         id                      BIGSERIAL                       PRIMARY KEY,
62         response_group_id       INT,
63         usr                     INT, -- REFERENCES actor.usr
64         survey                  INT                             NOT NULL REFERENCES action.survey,
65         question                INT                             NOT NULL REFERENCES action.survey_question,
66         answer                  INT                             NOT NULL REFERENCES action.survey_answer,
67         answer_date             TIMESTAMP WITH TIME ZONE,
68         effective_date          TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW()
69 );
70 CREATE OR REPLACE FUNCTION action.survey_response_answer_date_fixup () RETURNS TRIGGER AS '
71 BEGIN
72         NEW.answer_date := NOW();
73         RETURN NEW;
74 END;
75 ' LANGUAGE 'plpgsql';
76 CREATE TRIGGER action_survey_response_answer_date_fixup_tgr
77         BEFORE INSERT ON action.survey_response
78         FOR EACH ROW
79         EXECUTE PROCEDURE action.survey_response_answer_date_fixup ();
80
81
82 CREATE TABLE action.circulation (
83         target_copy             BIGINT                          NOT NULL, -- asset.copy.id
84         circ_lib                INT                             NOT NULL, -- actor.org_unit.id
85         circ_staff              INT                             NOT NULL, -- actor.usr.id
86         checkin_staff           INT,                                      -- actor.usr.id
87         checkin_lib             INT,                                      -- actor.org_unit.id
88         renewal_remaining       INT                             NOT NULL, -- derived from "circ duration" rule
89         due_date                TIMESTAMP WITH TIME ZONE,
90         stop_fines_time         TIMESTAMP WITH TIME ZONE,
91         checkin_time            TIMESTAMP WITH TIME ZONE,
92         duration                INTERVAL,                                 -- derived from "circ duration" rule
93         fine_interval           INTERVAL                        NOT NULL DEFAULT '1 day'::INTERVAL, -- derived from "circ fine" rule
94         recuring_fine           NUMERIC(6,2),                             -- derived from "circ fine" rule
95         max_fine                NUMERIC(6,2),                             -- derived from "max fine" rule
96         phone_renewal           BOOL                            NOT NULL DEFAULT FALSE,
97         desk_renewal            BOOL                            NOT NULL DEFAULT FALSE,
98         opac_renewal            BOOL                            NOT NULL DEFAULT FALSE,
99         duration_rule           TEXT                            NOT NULL, -- name of "circ duration" rule
100         recuring_fine_rule      TEXT                            NOT NULL, -- name of "circ fine" rule
101         max_fine_rule           TEXT                            NOT NULL, -- name of "max fine" rule
102         stop_fines              TEXT                            CHECK (stop_fines IN ('CHECKIN','CLAIMSRETURNED','LOST','MAXFINES','RENEW','LONGOVERDUE'))
103 ) INHERITS (money.billable_xact);
104 ALTER TABLE action.circulation ADD PRIMARY KEY (id);
105 CREATE INDEX circ_open_xacts_idx ON action.circulation (usr) WHERE xact_finish IS NULL;
106 CREATE INDEX circ_outstanding_idx ON action.circulation (usr) WHERE checkin_time IS NULL;
107 CREATE INDEX circ_checkin_time ON "action".circulation (checkin_time) WHERE checkin_time IS NOT NULL;
108 CREATE INDEX circ_circ_lib_idx ON "action".circulation (circ_lib);
109 CREATE INDEX circ_open_date_idx ON "action".circulation (xact_start) WHERE xact_finish IS NULL;
110
111
112 CREATE OR REPLACE VIEW action.open_circulation AS
113         SELECT  *
114           FROM  action.circulation
115           WHERE checkin_time IS NULL
116           ORDER BY due_date;
117                 
118
119 CREATE OR REPLACE VIEW action.billable_cirulations AS
120         SELECT  *
121           FROM  action.circulation
122           WHERE xact_finish IS NULL;
123
124 CREATE VIEW stats.fleshed_circulation AS
125         SELECT  c.*,
126                 CAST(c.xact_start AS DATE) AS start_date_day,
127                 CAST(c.xact_finish AS DATE) AS finish_date_day,
128                 DATE_TRUNC('hour', c.xact_start) AS start_date_hour,
129                 DATE_TRUNC('hour', c.xact_finish) AS finish_date_hour,
130                 cp.call_number_label,
131                 cp.owning_lib,
132                 cp.item_lang,
133                 cp.item_type,
134                 cp.item_form
135         FROM    "action".circulation c
136                 JOIN stats.fleshed_copy cp ON (cp.id = c.target_copy);
137
138
139 CREATE OR REPLACE FUNCTION action.circulation_claims_returned () RETURNS TRIGGER AS $$
140 BEGIN
141         IF OLD.stop_fines IS NULL OR OLD.stop_fines <> NEW.stop_fines THEN
142                 IF NEW.stop_fines = 'CLAIMSRETURNED' THEN
143                         UPDATE actor.usr SET claims_returned_count = claims_returned_count + 1 WHERE id = NEW.usr;
144                 END IF;
145                 IF NEW.stop_fines = 'LOST' THEN
146                         UPDATE asset.copy SET status = 3 WHERE id = NEW.target_copy;
147                 END IF;
148         END IF;
149         RETURN NEW;
150 END;
151 $$ LANGUAGE 'plpgsql';
152 CREATE TRIGGER action_circulation_stop_fines_tgr
153         BEFORE UPDATE ON action.circulation
154         FOR EACH ROW
155         EXECUTE PROCEDURE action.circulation_claims_returned ();
156
157
158 CREATE TABLE action.hold_request (
159         id                      SERIAL                          PRIMARY KEY,
160         request_time            TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW(),
161         capture_time            TIMESTAMP WITH TIME ZONE,
162         fulfillment_time        TIMESTAMP WITH TIME ZONE,
163         checkin_time            TIMESTAMP WITH TIME ZONE,
164         return_time             TIMESTAMP WITH TIME ZONE,
165         prev_check_time         TIMESTAMP WITH TIME ZONE,
166         expire_time             TIMESTAMP WITH TIME ZONE,
167         cancel_time             TIMESTAMP WITH TIME ZONE,
168         target                  BIGINT                          NOT NULL, -- see hold_type
169         current_copy            BIGINT                          REFERENCES asset.copy (id) ON DELETE SET NULL,
170         fulfillment_staff       INT                             REFERENCES actor.usr (id),
171         fulfillment_lib         INT                             REFERENCES actor.org_unit (id),
172         request_lib             INT                             NOT NULL REFERENCES actor.org_unit (id),
173         requestor               INT                             NOT NULL REFERENCES actor.usr (id),
174         usr                     INT                             NOT NULL REFERENCES actor.usr (id),
175         selection_ou            INT                             NOT NULL,
176         selection_depth         INT                             NOT NULL DEFAULT 0,
177         pickup_lib              INT                             NOT NULL REFERENCES actor.org_unit,
178         hold_type               TEXT                            NOT NULL CHECK (hold_type IN ('M','T','V','C')),
179         holdable_formats        TEXT,
180         phone_notify            TEXT,
181         email_notify            BOOL                            NOT NULL DEFAULT TRUE
182 );
183
184 CREATE INDEX hold_request_target_idx ON action.hold_request (target);
185 CREATE INDEX hold_request_usr_idx ON action.hold_request (usr);
186 CREATE INDEX hold_request_pickup_lib_idx ON action.hold_request (pickup_lib);
187 CREATE INDEX hold_request_current_copy_idx ON action.hold_request (current_copy);
188 CREATE INDEX hold_request_prev_check_time_idx ON action.hold_request (prev_check_time);
189
190
191 CREATE TABLE action.hold_notification (
192         id              SERIAL                          PRIMARY KEY,
193         hold            INT                             NOT NULL REFERENCES action.hold_request (id),
194         notify_staff    INT                             REFERENCES actor.usr (id),
195         notify_time     TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW(),
196         method          TEXT                            NOT NULL, -- email address or phone number
197         note            TEXT
198 );
199 CREATE INDEX ahn_hold_idx ON action.hold_notification (hold);
200
201 CREATE TABLE action.hold_copy_map (
202         id              SERIAL  PRIMARY KEY,
203         hold            INT     NOT NULL REFERENCES action.hold_request (id) ON DELETE CASCADE,
204         target_copy     BIGINT  NOT NULL REFERENCES asset.copy (id) ON DELETE CASCADE,
205         CONSTRAINT copy_once_per_hold UNIQUE (hold,target_copy)
206 );
207 -- CREATE INDEX acm_hold_idx ON action.hold_copy_map (hold);
208 CREATE INDEX acm_copy_idx ON action.hold_copy_map (target_copy);
209
210 CREATE TABLE action.transit_copy (
211         id                      SERIAL                          PRIMARY KEY,
212         source_send_time        TIMESTAMP WITH TIME ZONE,
213         dest_recv_time          TIMESTAMP WITH TIME ZONE,
214         target_copy             BIGINT                          NOT NULL REFERENCES asset.copy (id) ON DELETE CASCADE,
215         source                  INT                             NOT NULL REFERENCES actor.org_unit (id),
216         dest                    INT                             NOT NULL REFERENCES actor.org_unit (id),
217         prev_hop                INT                             REFERENCES action.transit_copy (id),
218         copy_status             INT                             NOT NULL REFERENCES config.copy_status (id),
219         persistant_transfer     BOOL                            NOT NULL DEFAULT FALSE
220 );
221 CREATE INDEX active_transit_dest_idx ON "action".transit_copy (dest); 
222 CREATE INDEX active_transit_source_idx ON "action".transit_copy (source);
223 CREATE INDEX active_transit_cp_idx ON "action".transit_copy (target_copy);
224
225
226 CREATE TABLE action.hold_transit_copy (
227         hold    INT     REFERENCES action.hold_request (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED
228 ) INHERITS (action.transit_copy);
229 ALTER TABLE action.hold_transit_copy ADD PRIMARY KEY (id);
230 ALTER TABLE action.hold_transit_copy ADD CONSTRAINT ahtc_tc_fkey FOREIGN KEY (target_copy) REFERENCES asset.copy (id) ON DELETE CASCADE;
231 CREATE INDEX active_hold_transit_dest_idx ON "action".hold_transit_copy (dest);
232 CREATE INDEX active_hold_transit_source_idx ON "action".hold_transit_copy (source);
233 CREATE INDEX active_hold_transit_cp_idx ON "action".hold_transit_copy (target_copy);
234
235
236 CREATE TABLE action.unfulfilled_hold_list (
237         id              BIGSERIAL                       PRIMARY KEY,
238         current_copy    BIGINT                          NOT NULL,
239         hold            INT                             NOT NULL,
240         circ_lib        INT                             NOT NULL,
241         fail_time       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW()
242 );
243
244 COMMIT;
245