]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/002.schema.config.sql
Patch from Joe Atzberger, with some IDL additions, to add a new generic remote_accoun...
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 002.schema.config.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
19
20 DROP SCHEMA stats CASCADE;
21 DROP SCHEMA config CASCADE;
22
23 BEGIN;
24 CREATE SCHEMA stats;
25
26 CREATE SCHEMA config;
27 COMMENT ON SCHEMA config IS $$
28 /*
29  * Copyright (C) 2005  Georgia Public Library Service 
30  * Mike Rylander <mrylander@gmail.com>
31  *
32  * The config schema holds static configuration data for the
33  * Open-ILS installation.
34  *
35  * ****
36  *
37  * This program is free software; you can redistribute it and/or
38  * modify it under the terms of the GNU General Public License
39  * as published by the Free Software Foundation; either version 2
40  * of the License, or (at your option) any later version.
41  *
42  * This program is distributed in the hope that it will be useful,
43  * but WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  * GNU General Public License for more details.
46  */
47 $$;
48
49 CREATE TABLE config.upgrade_log (
50     version         TEXT    PRIMARY KEY,
51     install_date    TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
52 );
53
54 INSERT INTO config.upgrade_log (version) VALUES ('0134'); -- phasefx
55
56 CREATE TABLE config.bib_source (
57         id              SERIAL  PRIMARY KEY,
58         quality         INT     CHECK ( quality BETWEEN 0 AND 100 ),
59         source          TEXT    NOT NULL UNIQUE,
60         transcendant    BOOL    NOT NULL DEFAULT FALSE
61 );
62 COMMENT ON TABLE config.bib_source IS $$
63 /*
64  * Copyright (C) 2005  Georgia Public Library Service 
65  * Mike Rylander <mrylander@gmail.com>
66  *
67  * Valid sources of MARC records
68  *
69  * This is table is used to set up the relative "quality" of each
70  * MARC source, such as OCLC.
71  *
72  * ****
73  *
74  * This program is free software; you can redistribute it and/or
75  * modify it under the terms of the GNU General Public License
76  * as published by the Free Software Foundation; either version 2
77  * of the License, or (at your option) any later version.
78  *
79  * This program is distributed in the hope that it will be useful,
80  * but WITHOUT ANY WARRANTY; without even the implied warranty of
81  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
82  * GNU General Public License for more details.
83  */
84 $$;
85
86 CREATE TABLE config.standing (
87         id              SERIAL  PRIMARY KEY,
88         value           TEXT    NOT NULL UNIQUE
89 );
90 COMMENT ON TABLE config.standing IS $$
91 /*
92  * Copyright (C) 2005  Georgia Public Library Service 
93  * Mike Rylander <mrylander@gmail.com>
94  *
95  * Patron Standings
96  *
97  * This table contains the values that can be applied to a patron
98  * by a staff member.  These values should not be changed, other
99  * than for translation, as the ID column is currently a "magic
100  * number" in the source. :(
101  *
102  * ****
103  *
104  * This program is free software; you can redistribute it and/or
105  * modify it under the terms of the GNU General Public License
106  * as published by the Free Software Foundation; either version 2
107  * of the License, or (at your option) any later version.
108  *
109  * This program is distributed in the hope that it will be useful,
110  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
112  * GNU General Public License for more details.
113  */
114 $$;
115
116 CREATE TABLE config.standing_penalty (
117         id                      SERIAL  PRIMARY KEY,
118         name            TEXT    NOT NULL UNIQUE,
119         label           TEXT    NOT NULL,
120         block_list      TEXT,
121         org_depth       INTEGER
122 );
123 INSERT INTO config.standing_penalty (id,name,label,block_list)
124         VALUES (1,'PATRON_EXCEEDS_FINES','Patron exceeds fine threshold','CIRC|HOLD|RENEW');
125 INSERT INTO config.standing_penalty (id,name,label,block_list)
126         VALUES (2,'PATRON_EXCEEDS_OVERDUE_COUNT','Patron exceeds max overdue item threshold','CIRC|HOLD|RENEW');
127 INSERT INTO config.standing_penalty (id,name,label,block_list)
128         VALUES (3,'PATRON_EXCEEDS_CHECKOUT_COUNT','Patron exceeds max checked out item threshold','CIRC');
129 INSERT INTO config.standing_penalty (id,name,label,block_list)
130         VALUES (4,'PATRON_EXCEEDS_COLLECTIONS_WARNING','Patron exceeds pre-collections warning fine threshold','CIRC|HOLD|RENEW');
131
132 INSERT INTO config.standing_penalty (id,name,label) VALUES (20,'ALERT_NOTE','Alerting Note, no blocks');
133 INSERT INTO config.standing_penalty (id,name,label) VALUES (21,'SILENT_NOTE','Note, no blocks');
134 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (22,'STAFF_C','Alerting block on Circ','CIRC');
135 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (23,'STAFF_CH','Alerting block on Circ and Hold','CIRC|HOLD');
136 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (24,'STAFF_CR','Alerting block on Circ and Renew','CIRC|RENEW');
137 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (25,'STAFF_CHR','Alerting block on Circ, Hold and Renew','CIRC|HOLD|RENEW');
138 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (26,'STAFF_HR','Alerting block on Hold and Renew','HOLD|RENEW');
139 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (27,'STAFF_H','Alerting block on Hold','HOLD');
140 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (28,'STAFF_R','Alerting block on Renew','RENEW');
141 INSERT INTO config.standing_penalty (id,name,label) VALUES (29,'INVALID_PATRON_ADDRESS','Patron has an invalid address');
142 INSERT INTO config.standing_penalty (id,name,label) VALUES (30,'PATRON_IN_COLLECTIONS','Patron has been referred to a collections agency');
143
144 SELECT SETVAL('config.standing_penalty_id_seq', 100);
145
146 CREATE TABLE config.xml_transform (
147         name            TEXT    PRIMARY KEY,
148         namespace_uri   TEXT    NOT NULL,
149         prefix          TEXT    NOT NULL,
150         xslt            TEXT    NOT NULL
151 );
152
153 CREATE TABLE config.metabib_field (
154         id              SERIAL  PRIMARY KEY,
155         field_class     TEXT    NOT NULL CHECK (lower(field_class) IN ('title','author','subject','keyword','series')),
156         name            TEXT    NOT NULL,
157         xpath           TEXT    NOT NULL,
158         weight          INT     NOT NULL DEFAULT 1,
159         format          TEXT    NOT NULL DEFAULT 'mods33',
160         search_field    BOOL    NOT NULL DEFAULT TRUE,
161         facet_field     BOOL    NOT NULL DEFAULT FALSE
162 );
163 COMMENT ON TABLE config.metabib_field IS $$
164 /*
165  * Copyright (C) 2005  Georgia Public Library Service 
166  * Mike Rylander <mrylander@gmail.com>
167  *
168  * XPath used for record indexing ingest
169  *
170  * This table contains the XPath used to chop up MODS into its
171  * indexable parts.  Each XPath entry is named and assigned to
172  * a "class" of either title, subject, author, keyword or series.
173  * 
174  *
175  * ****
176  *
177  * This program is free software; you can redistribute it and/or
178  * modify it under the terms of the GNU General Public License
179  * as published by the Free Software Foundation; either version 2
180  * of the License, or (at your option) any later version.
181  *
182  * This program is distributed in the hope that it will be useful,
183  * but WITHOUT ANY WARRANTY; without even the implied warranty of
184  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185  * GNU General Public License for more details.
186  */
187 $$;
188
189 CREATE UNIQUE INDEX config_metabib_field_class_name_idx ON config.metabib_field (field_class, name);
190
191 CREATE TABLE config.non_cataloged_type (
192         id              SERIAL          PRIMARY KEY,
193         owning_lib      INT             NOT NULL, -- REFERENCES actor.org_unit (id),
194         name            TEXT            NOT NULL,
195         circ_duration   INTERVAL        NOT NULL DEFAULT '14 days'::INTERVAL,
196         in_house        BOOL            NOT NULL DEFAULT FALSE,
197         CONSTRAINT noncat_once_per_lib UNIQUE (owning_lib,name)
198 );
199 COMMENT ON TABLE config.non_cataloged_type IS $$
200 /*
201  * Copyright (C) 2005  Georgia Public Library Service 
202  * Mike Rylander <mrylander@gmail.com>
203  *
204  * Types of valid non-cataloged items.
205  *
206  *
207  * ****
208  *
209  * This program is free software; you can redistribute it and/or
210  * modify it under the terms of the GNU General Public License
211  * as published by the Free Software Foundation; either version 2
212  * of the License, or (at your option) any later version.
213  *
214  * This program is distributed in the hope that it will be useful,
215  * but WITHOUT ANY WARRANTY; without even the implied warranty of
216  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
217  * GNU General Public License for more details.
218  */
219 $$;
220
221 CREATE TABLE config.identification_type (
222         id              SERIAL  PRIMARY KEY,
223         name            TEXT    NOT NULL UNIQUE
224 );
225 COMMENT ON TABLE config.identification_type IS $$
226 /*
227  * Copyright (C) 2005  Georgia Public Library Service 
228  * Mike Rylander <mrylander@gmail.com>
229  *
230  * Types of valid patron identification.
231  *
232  * Each patron must display at least one valid form of identification
233  * in order to get a library card.  This table lists those forms.
234  * 
235  *
236  * ****
237  *
238  * This program is free software; you can redistribute it and/or
239  * modify it under the terms of the GNU General Public License
240  * as published by the Free Software Foundation; either version 2
241  * of the License, or (at your option) any later version.
242  *
243  * This program is distributed in the hope that it will be useful,
244  * but WITHOUT ANY WARRANTY; without even the implied warranty of
245  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
246  * GNU General Public License for more details.
247  */
248 $$;
249
250 CREATE TABLE config.rule_circ_duration (
251         id              SERIAL          PRIMARY KEY,
252         name            TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
253         extended        INTERVAL        NOT NULL,
254         normal          INTERVAL        NOT NULL,
255         shrt            INTERVAL        NOT NULL,
256         max_renewals    INT             NOT NULL
257 );
258 COMMENT ON TABLE config.rule_circ_duration IS $$
259 /*
260  * Copyright (C) 2005  Georgia Public Library Service 
261  * Mike Rylander <mrylander@gmail.com>
262  *
263  * Circulation Duration rules
264  *
265  * Each circulation is given a duration based on one of these rules.
266  * 
267  *
268  * ****
269  *
270  * This program is free software; you can redistribute it and/or
271  * modify it under the terms of the GNU General Public License
272  * as published by the Free Software Foundation; either version 2
273  * of the License, or (at your option) any later version.
274  *
275  * This program is distributed in the hope that it will be useful,
276  * but WITHOUT ANY WARRANTY; without even the implied warranty of
277  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
278  * GNU General Public License for more details.
279  */
280 $$;
281
282 CREATE TABLE config.rule_max_fine (
283     id          SERIAL          PRIMARY KEY,
284     name        TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
285     amount      NUMERIC(6,2)    NOT NULL,
286     is_percent  BOOL            NOT NULL DEFAULT FALSE
287 );
288 COMMENT ON TABLE config.rule_max_fine IS $$
289 /*
290  * Copyright (C) 2005  Georgia Public Library Service 
291  * Mike Rylander <mrylander@gmail.com>
292  *
293  * Circulation Max Fine rules
294  *
295  * Each circulation is given a maximum fine based on one of
296  * these rules.
297  * 
298  *
299  * ****
300  *
301  * This program is free software; you can redistribute it and/or
302  * modify it under the terms of the GNU General Public License
303  * as published by the Free Software Foundation; either version 2
304  * of the License, or (at your option) any later version.
305  *
306  * This program is distributed in the hope that it will be useful,
307  * but WITHOUT ANY WARRANTY; without even the implied warranty of
308  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
309  * GNU General Public License for more details.
310  */
311 $$;
312
313 CREATE TABLE config.rule_recurring_fine (
314         id                      SERIAL          PRIMARY KEY,
315         name                    TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
316         high                    NUMERIC(6,2)    NOT NULL,
317         normal                  NUMERIC(6,2)    NOT NULL,
318         low                     NUMERIC(6,2)    NOT NULL,
319         recurrence_interval     INTERVAL        NOT NULL DEFAULT '1 day'::INTERVAL
320 );
321 COMMENT ON TABLE config.rule_recurring_fine IS $$
322 /*
323  * Copyright (C) 2005  Georgia Public Library Service 
324  * Mike Rylander <mrylander@gmail.com>
325  *
326  * Circulation Recurring Fine rules
327  *
328  * Each circulation is given a recurring fine amount based on one of
329  * these rules.  The recurrence_interval should not be any shorter
330  * than the interval between runs of the fine_processor.pl script
331  * (which is run from CRON), or you could miss fines.
332  * 
333  *
334  * ****
335  *
336  * This program is free software; you can redistribute it and/or
337  * modify it under the terms of the GNU General Public License
338  * as published by the Free Software Foundation; either version 2
339  * of the License, or (at your option) any later version.
340  *
341  * This program is distributed in the hope that it will be useful,
342  * but WITHOUT ANY WARRANTY; without even the implied warranty of
343  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
344  * GNU General Public License for more details.
345  */
346 $$;
347
348
349 CREATE TABLE config.rule_age_hold_protect (
350         id      SERIAL          PRIMARY KEY,
351         name    TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
352         age     INTERVAL        NOT NULL,
353         prox    INT             NOT NULL
354 );
355 COMMENT ON TABLE config.rule_age_hold_protect IS $$
356 /*
357  * Copyright (C) 2005  Georgia Public Library Service 
358  * Mike Rylander <mrylander@gmail.com>
359  *
360  * Hold Item Age Protection rules
361  *
362  * A hold request can only capture new(ish) items when they are
363  * within a particular proximity of the home_ou of the requesting
364  * user.  The proximity ('prox' column) is calculated by counting
365  * the number of tree edges between the user's home_ou and the owning_lib
366  * of the copy that could fulfill the hold.
367  * 
368  *
369  * ****
370  *
371  * This program is free software; you can redistribute it and/or
372  * modify it under the terms of the GNU General Public License
373  * as published by the Free Software Foundation; either version 2
374  * of the License, or (at your option) any later version.
375  *
376  * This program is distributed in the hope that it will be useful,
377  * but WITHOUT ANY WARRANTY; without even the implied warranty of
378  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
379  * GNU General Public License for more details.
380  */
381 $$;
382
383 CREATE TABLE config.copy_status (
384         id              SERIAL  PRIMARY KEY,
385         name            TEXT    NOT NULL UNIQUE,
386         holdable        BOOL    NOT NULL DEFAULT FALSE,
387         opac_visible    BOOL    NOT NULL DEFAULT FALSE
388 );
389 COMMENT ON TABLE config.copy_status IS $$
390 /*
391  * Copyright (C) 2005  Georgia Public Library Service 
392  * Mike Rylander <mrylander@gmail.com>
393  *
394  * Copy Statuses
395  *
396  * The available copy statuses, and whether a copy in that
397  * status is available for hold request capture.  0 (zero) is
398  * the only special number in this set, meaning that the item
399  * is available for immediate checkout, and is counted as available
400  * in the OPAC.
401  *
402  * Statuses with an ID below 100 are not removable, and have special
403  * meaning in the code.  Do not change them except to translate the
404  * textual name.
405  *
406  * You may add and remove statuses above 100, and these can be used
407  * to remove items from normal circulation without affecting the rest
408  * of the copy's values or its location.
409  *
410  * ****
411  *
412  * This program is free software; you can redistribute it and/or
413  * modify it under the terms of the GNU General Public License
414  * as published by the Free Software Foundation; either version 2
415  * of the License, or (at your option) any later version.
416  *
417  * This program is distributed in the hope that it will be useful,
418  * but WITHOUT ANY WARRANTY; without even the implied warranty of
419  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
420  * GNU General Public License for more details.
421  */
422 $$;
423
424 CREATE TABLE config.net_access_level (
425         id      SERIAL          PRIMARY KEY,
426         name    TEXT            NOT NULL UNIQUE
427 );
428 COMMENT ON TABLE config.net_access_level IS $$
429 /*
430  * Copyright (C) 2005  Georgia Public Library Service 
431  * Mike Rylander <mrylander@gmail.com>
432  *
433  * Patron Network Access level
434  *
435  * This will be used to inform the in-library firewall of how much
436  * internet access the using patron should be allowed.
437  *
438  * ****
439  *
440  * This program is free software; you can redistribute it and/or
441  * modify it under the terms of the GNU General Public License
442  * as published by the Free Software Foundation; either version 2
443  * of the License, or (at your option) any later version.
444  *
445  * This program is distributed in the hope that it will be useful,
446  * but WITHOUT ANY WARRANTY; without even the implied warranty of
447  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
448  * GNU General Public License for more details.
449  */
450 $$;
451
452
453 CREATE TABLE config.remote_account (
454     id          SERIAL  PRIMARY KEY,
455     label       TEXT    NOT NULL,
456     host        TEXT    NOT NULL,   -- name or IP, :port optional
457     username    TEXT,               -- optional, since we could default to $USER
458     password    TEXT,               -- optional, since we could use SSH keys, or anonymous login.
459     account     TEXT,               -- aka profile or FTP "account" command
460     path        TEXT,               -- aka directory
461     owner       INT     NOT NULL,   -- REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
462     last_activity TIMESTAMP WITH TIME ZONE
463 );
464
465 CREATE TABLE config.audience_map (
466         code            TEXT    PRIMARY KEY,
467         value           TEXT    NOT NULL,
468         description     TEXT
469 );
470
471 CREATE TABLE config.lit_form_map (
472         code            TEXT    PRIMARY KEY,
473         value           TEXT    NOT NULL,
474         description     TEXT
475 );
476
477 CREATE TABLE config.language_map (
478         code    TEXT    PRIMARY KEY,
479         value   TEXT    NOT NULL
480 );
481
482 CREATE TABLE config.item_form_map (
483         code    TEXT    PRIMARY KEY,
484         value   TEXT    NOT NULL
485 );
486
487 CREATE TABLE config.item_type_map (
488         code    TEXT    PRIMARY KEY,
489         value   TEXT    NOT NULL
490 );
491
492 CREATE TABLE config.bib_level_map (
493         code    TEXT    PRIMARY KEY,
494         value   TEXT    NOT NULL
495 );
496
497 CREATE TABLE config.marc21_rec_type_map (
498     code        TEXT    PRIMARY KEY,
499     type_val    TEXT    NOT NULL,
500     blvl_val    TEXT    NOT NULL
501 );
502
503 CREATE TABLE config.marc21_ff_pos_map (
504     id          SERIAL  PRIMARY KEY,
505     fixed_field TEXT    NOT NULL,
506     tag         TEXT    NOT NULL,
507     rec_type    TEXT    NOT NULL,
508     start_pos   INT     NOT NULL,
509     length      INT     NOT NULL,
510     default_val TEXT    NOT NULL DEFAULT ' '
511 );
512
513 CREATE TABLE config.marc21_physical_characteristic_type_map (
514     ptype_key   TEXT    PRIMARY KEY,
515     label       TEXT    NOT NULL -- I18N
516 );
517
518 CREATE TABLE config.marc21_physical_characteristic_subfield_map (
519     id          SERIAL  PRIMARY KEY,
520     ptype_key   TEXT    NOT NULL REFERENCES config.marc21_physical_characteristic_type_map (ptype_key) ON DELETE CASCADE ON UPDATE CASCADE,
521     subfield    TEXT    NOT NULL,
522     start_pos   INT     NOT NULL,
523     length      INT     NOT NULL,
524     label       TEXT    NOT NULL -- I18N
525 );
526
527 CREATE TABLE config.marc21_physical_characteristic_value_map (
528     id              SERIAL  PRIMARY KEY,
529     value           TEXT    NOT NULL,
530     ptype_subfield  INT     NOT NULL REFERENCES config.marc21_physical_characteristic_subfield_map (id),
531     label           TEXT    NOT NULL -- I18N
532 );
533
534
535 CREATE TABLE config.z3950_source (
536     name                TEXT    PRIMARY KEY,
537     label               TEXT    NOT NULL UNIQUE,
538     host                TEXT    NOT NULL,
539     port                INT     NOT NULL,
540     db                  TEXT    NOT NULL,
541     record_format       TEXT    NOT NULL DEFAULT 'FI',
542     transmission_format TEXT    NOT NULL DEFAULT 'usmarc',
543     auth                BOOL    NOT NULL DEFAULT TRUE
544 );
545
546 COMMENT ON TABLE config.z3950_source IS $$
547 Z39.50 Sources
548
549 Each row in this table represents a database searchable via Z39.50.
550 $$;
551
552 COMMENT ON COLUMN config.z3950_source.record_format IS $$
553 Z39.50 element set.
554 $$;
555
556 COMMENT ON COLUMN config.z3950_source.transmission_format IS $$
557 Z39.50 preferred record syntax..
558 $$;
559
560
561 CREATE TABLE config.z3950_attr (
562     id          SERIAL  PRIMARY KEY,
563     source      TEXT    NOT NULL REFERENCES config.z3950_source (name) DEFERRABLE INITIALLY DEFERRED,
564     name        TEXT    NOT NULL,
565     label       TEXT    NOT NULL,
566     code        INT     NOT NULL,
567     format      INT     NOT NULL,
568     truncation  INT     NOT NULL DEFAULT 0,
569     CONSTRAINT z_code_format_once_per_source UNIQUE (code,format,source)
570 );
571
572 CREATE TABLE config.i18n_locale (
573     code        TEXT    PRIMARY KEY,
574     marc_code   TEXT    NOT NULL REFERENCES config.language_map (code) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
575     name        TEXT    UNIQUE NOT NULL,
576     description TEXT
577 );
578
579 CREATE TABLE config.i18n_core (
580     id              BIGSERIAL   PRIMARY KEY,
581     fq_field        TEXT        NOT NULL,
582     identity_value  TEXT        NOT NULL,
583     translation     TEXT        NOT NULL    REFERENCES config.i18n_locale (code) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
584     string          TEXT        NOT NULL
585 );
586
587 CREATE UNIQUE INDEX i18n_identity ON config.i18n_core (fq_field,identity_value,translation);
588
589 CREATE TABLE config.billing_type (
590     id              SERIAL  PRIMARY KEY,
591     name            TEXT    NOT NULL,
592     owner           INT     NOT NULL, -- REFERENCES actor.org_unit (id)
593     default_price   NUMERIC(6,2),
594     CONSTRAINT billing_type_once_per_lib UNIQUE (name, owner)
595 );
596
597 CREATE TABLE config.org_unit_setting_type (
598     name            TEXT    PRIMARY KEY,
599     label           TEXT    UNIQUE NOT NULL,
600     description     TEXT,
601     datatype        TEXT    NOT NULL DEFAULT 'string',
602     fm_class        TEXT,
603     view_perm       INT,
604     update_perm     INT,
605     --
606     -- define valid datatypes
607     --
608     CONSTRAINT coust_valid_datatype CHECK ( datatype IN
609     ( 'bool', 'integer', 'float', 'currency', 'interval',
610       'date', 'string', 'object', 'array', 'link' ) ),
611     --
612     -- fm_class is meaningful only for 'link' datatype
613     --
614     CONSTRAINT coust_no_empty_link CHECK
615     ( ( datatype =  'link' AND fm_class IS NOT NULL ) OR
616       ( datatype <> 'link' AND fm_class IS NULL ) )
617 );
618
619 CREATE TABLE config.usr_setting_type (
620
621     name TEXT PRIMARY KEY,
622     opac_visible BOOL NOT NULL DEFAULT FALSE,
623     label TEXT UNIQUE NOT NULL,
624     description TEXT,
625     datatype TEXT NOT NULL DEFAULT 'string',
626     fm_class TEXT,
627
628     --
629     -- define valid datatypes
630     --
631     CONSTRAINT coust_valid_datatype CHECK ( datatype IN
632     ( 'bool', 'integer', 'float', 'currency', 'interval',
633         'date', 'string', 'object', 'array', 'link' ) ),
634
635     --
636     -- fm_class is meaningful only for 'link' datatype
637     --
638     CONSTRAINT coust_no_empty_link CHECK
639     ( ( datatype = 'link' AND fm_class IS NOT NULL ) OR
640         ( datatype <> 'link' AND fm_class IS NULL ) )
641
642 );
643
644 -- Some handy functions, based on existing ones, to provide optional ingest normalization
645
646 CREATE OR REPLACE FUNCTION public.left_trunc( TEXT, INT ) RETURNS TEXT AS $func$
647         SELECT SUBSTRING($1,$2);
648 $func$ LANGUAGE SQL STRICT IMMUTABLE;
649
650 CREATE OR REPLACE FUNCTION public.right_trunc( TEXT, INT ) RETURNS TEXT AS $func$
651         SELECT SUBSTRING($1,1,$2);
652 $func$ LANGUAGE SQL STRICT IMMUTABLE;
653
654 CREATE OR REPLACE FUNCTION public.split_date_range( TEXT ) RETURNS TEXT AS $func$
655         SELECT REGEXP_REPLACE( $1, E'(\\d{4})-(\\d{4})', E'\\1 \\2', 'g' );
656 $func$ LANGUAGE SQL STRICT IMMUTABLE;
657
658 -- And ... a table in which to register them
659
660 CREATE TABLE config.index_normalizer (
661         id              SERIAL  PRIMARY KEY,
662         name            TEXT    UNIQUE NOT NULL,
663         description     TEXT,
664         func            TEXT    NOT NULL,
665         param_count     INT     NOT NULL DEFAULT 0
666 );
667
668 CREATE TABLE config.metabib_field_index_norm_map (
669         id      SERIAL  PRIMARY KEY,
670         field   INT     NOT NULL REFERENCES config.metabib_field (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
671         norm    INT     NOT NULL REFERENCES config.index_normalizer (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
672         params  TEXT,
673         pos     INT     NOT NULL DEFAULT 0
674 );
675
676 CREATE OR REPLACE FUNCTION oils_tsearch2 () RETURNS TRIGGER AS $$
677 DECLARE
678         normalizer      RECORD;
679         value           TEXT := '';
680 BEGIN
681         value := NEW.value;
682
683         IF TG_TABLE_NAME::TEXT ~ 'field_entry$' THEN
684                 FOR normalizer IN
685                         SELECT  n.func AS func,
686                                 n.param_count AS param_count,
687                                 m.params AS params
688                           FROM  config.index_normalizer n
689                                 JOIN config.metabib_field_index_norm_map m ON (m.norm = n.id)
690                           WHERE field = NEW.field
691                           ORDER BY m.pos
692                 LOOP
693                         EXECUTE 'SELECT ' || normalizer.func || '(' ||
694                                         quote_literal( value ) ||
695                                         CASE
696                                                 WHEN normalizer.param_count > 0 THEN ',' || BTRIM(normalizer.params,'[]')
697                                                 ELSE ''
698                                         END ||
699                                 ')' INTO value;
700
701                 END LOOP;
702         END IF;
703
704         IF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT > 8.2 THEN
705                 NEW.index_vector = to_tsvector((TG_ARGV[0])::regconfig, value);
706         ELSE
707                 NEW.index_vector = to_tsvector(TG_ARGV[0], value);
708         END IF;
709
710         RETURN NEW;
711 END;
712 $$ LANGUAGE PLPGSQL;
713
714 COMMIT;