]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/010.schema.biblio.sql
Lp 1730726: Fix a number of PgTap tests for PostgreSQL 10.
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 010.schema.biblio.sql
1 /*
2  * Copyright (C) 2004-2008  Georgia Public Library Service
3  * Copyright (C) 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 IF EXISTS biblio CASCADE;
19
20 BEGIN;
21 CREATE SCHEMA biblio;
22
23 CREATE SEQUENCE biblio.autogen_tcn_value_seq;
24 CREATE OR REPLACE FUNCTION biblio.next_autogen_tcn_value () RETURNS TEXT AS $$
25         BEGIN RETURN 'AUTOGENERATED-' || nextval('biblio.autogen_tcn_value_seq'::TEXT); END;
26 $$ LANGUAGE PLPGSQL;
27
28 CREATE OR REPLACE FUNCTION biblio.check_marcxml_well_formed () RETURNS TRIGGER AS $func$
29 BEGIN
30
31     IF xml_is_well_formed(NEW.marc) THEN
32         RETURN NEW;
33     ELSE
34         RAISE EXCEPTION 'Attempted to % MARCXML that is not well formed', TG_OP;
35     END IF;
36     
37 END;
38 $func$ LANGUAGE PLPGSQL;
39
40 CREATE TABLE biblio.record_entry (
41         id              BIGSERIAL       PRIMARY KEY,
42         creator         INT             NOT NULL DEFAULT 1,
43         editor          INT             NOT NULL DEFAULT 1,
44         source          INT,
45         quality         INT,
46         create_date     TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
47         edit_date       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
48         active          BOOL            NOT NULL DEFAULT TRUE,
49         deleted         BOOL            NOT NULL DEFAULT FALSE,
50         fingerprint     TEXT,
51         tcn_source      TEXT            NOT NULL DEFAULT 'AUTOGEN',
52         tcn_value       TEXT            NOT NULL DEFAULT biblio.next_autogen_tcn_value(),
53         marc            TEXT            NOT NULL,
54         last_xact_id    TEXT            NOT NULL,
55     vis_attr_vector INT[],
56     owner       INT,
57     share_depth INT,
58     merge_date  TIMESTAMP WITH TIME ZONE,
59     merged_to   BIGINT REFERENCES biblio.record_entry(id)
60 );
61 CREATE INDEX biblio_record_entry_creator_idx ON biblio.record_entry ( creator );
62 CREATE INDEX biblio_record_entry_create_date_idx ON biblio.record_entry ( create_date );
63 CREATE INDEX biblio_record_entry_editor_idx ON biblio.record_entry ( editor );
64 CREATE INDEX biblio_record_entry_edit_date_idx ON biblio.record_entry ( edit_date );
65 CREATE INDEX biblio_record_entry_fp_idx ON biblio.record_entry ( fingerprint );
66 CREATE UNIQUE INDEX biblio_record_unique_tcn ON biblio.record_entry (tcn_value) WHERE deleted = FALSE OR deleted IS FALSE;
67 CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed();
68 CREATE TRIGGER b_maintain_901 BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE evergreen.maintain_901();
69 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE evergreen.maintain_control_numbers();
70
71 CREATE TABLE biblio.record_note (
72         id              BIGSERIAL       PRIMARY KEY,
73         record          BIGINT          NOT NULL,
74         value           TEXT            NOT NULL,
75         creator         INT             NOT NULL DEFAULT 1,
76         editor          INT             NOT NULL DEFAULT 1,
77         pub             BOOL            NOT NULL DEFAULT FALSE,
78         create_date     TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
79         edit_date       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now()
80 );
81 CREATE INDEX biblio_record_note_record_idx ON biblio.record_note ( record );
82 CREATE INDEX biblio_record_note_creator_idx ON biblio.record_note ( creator );
83 CREATE INDEX biblio_record_note_editor_idx ON biblio.record_note ( editor );
84
85 CREATE TABLE biblio.peer_type (
86     id      SERIAL  PRIMARY KEY,
87     name        TEXT        NOT NULL UNIQUE -- i18n
88 );
89
90 CREATE TABLE biblio.peer_bib_copy_map (
91     id      SERIAL  PRIMARY KEY,
92     peer_type   INT     NOT NULL REFERENCES biblio.peer_type (id),
93     peer_record BIGINT      NOT NULL REFERENCES biblio.record_entry (id),
94     target_copy BIGINT      NOT NULL -- can't use fkey because of acp subtables
95 );
96 CREATE INDEX peer_bib_copy_map_record_idx ON biblio.peer_bib_copy_map (peer_record);
97 CREATE INDEX peer_bib_copy_map_copy_idx ON biblio.peer_bib_copy_map (target_copy);
98
99 CREATE TABLE biblio.monograph_part (
100     id              SERIAL  PRIMARY KEY,
101     record          BIGINT  NOT NULL REFERENCES biblio.record_entry (id),
102     label           TEXT    NOT NULL,
103     label_sortkey   TEXT    NOT NULL,
104     deleted         BOOL    NOT NULL DEFAULT FALSE
105 );
106 CREATE UNIQUE INDEX record_label_unique_idx ON biblio.monograph_part (record, label) WHERE deleted = FALSE;
107
108 CREATE OR REPLACE FUNCTION biblio.normalize_biblio_monograph_part_sortkey () RETURNS TRIGGER AS $$
109 BEGIN
110     NEW.label_sortkey := REGEXP_REPLACE(
111         evergreen.lpad_number_substrings(
112             naco_normalize(NEW.label),
113             '0',
114             10
115         ),
116         E'\\s+',
117         '',
118         'g'
119     );
120     RETURN NEW;
121 END;
122 $$ LANGUAGE PLPGSQL;
123
124 CREATE TRIGGER norm_sort_label BEFORE INSERT OR UPDATE ON biblio.monograph_part FOR EACH ROW EXECUTE PROCEDURE biblio.normalize_biblio_monograph_part_sortkey();
125
126 COMMIT;