1 DROP SCHEMA asset CASCADE;
7 CREATE TABLE asset.copy_location (
10 owning_lib INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
11 holdable BOOL NOT NULL DEFAULT TRUE,
12 opac_visible BOOL NOT NULL DEFAULT TRUE,
13 circulate BOOL NOT NULL DEFAULT TRUE
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(),
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) ),
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,
42 opac_visible BOOL NOT NULL DEFAULT TRUE,
43 deleted BOOL NOT NULL DEFAULT FALSE
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;
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) ),
65 CONSTRAINT scte_name_once_per_lib UNIQUE (owner,name)
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)
73 CREATE INDEX cp_tr_cp_idx ON asset.copy_tranparency_map (tansparency);
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)
83 CREATE TABLE asset.stat_cat (
84 id SERIAL PRIMARY KEY,
86 opac_visible BOOL NOT NULL DEFAULT FALSE,
88 CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
91 CREATE TABLE asset.stat_cat_entry (
92 id SERIAL PRIMARY KEY,
93 stat_cat INT NOT NULL,
96 CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
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)
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,
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,
126 deleted BOOL NOT NULL DEFAULT FALSE
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;
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,
146 CREATE VIEW stats.fleshed_copy AS
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,
158 JOIN asset.call_number cn ON (cp.call_number = cn.id)
159 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
161 CREATE VIEW stats.fleshed_call_number AS
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,
170 FROM asset.call_number cn
171 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
173 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
176 source_cn asset.call_number%ROWTYPE;
177 target_cn asset.call_number%ROWTYPE;
179 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
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;
189 SET call_number = target_cn.id
190 WHERE call_number = source_cn.id;
192 UPDATE asset.call_number
193 SET record = target_record
194 WHERE id = source_cn.id;
197 moved_cns := moved_cns + 1;
202 $func$ LANGUAGE plpgsql;