]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/005.schema.actors.sql
adding config.standing_penalty, and links to that from actor.usr_standing_penalty
[Evergreen.git] / Open-ILS / src / sql / Pg / 005.schema.actors.sql
1 DROP SCHEMA actor CASCADE;
2
3 BEGIN;
4 CREATE SCHEMA actor;
5 COMMENT ON SCHEMA actor IS $$
6 /*
7  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
8  * Mike Rylander <mrylander@gmail.com>
9  *
10  * Schema: actor
11  *
12  * Holds all tables pertaining to users and libraries (org units).
13  *
14  * ****
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  */
26 $$;
27
28 CREATE TABLE actor.usr (
29         id                      SERIAL                          PRIMARY KEY,
30         card                    INT                             UNIQUE, -- active card
31         profile                 INT                             NOT NULL, -- patron profile
32         usrname                 TEXT                            NOT NULL UNIQUE,
33         email                   TEXT,
34         passwd                  TEXT                            NOT NULL,
35         standing                INT                             NOT NULL DEFAULT 1 REFERENCES config.standing (id) DEFERRABLE INITIALLY DEFERRED,
36         ident_type              INT                             NOT NULL REFERENCES config.identification_type (id) DEFERRABLE INITIALLY DEFERRED,
37         ident_value             TEXT,
38         ident_type2             INT                             REFERENCES config.identification_type (id) DEFERRABLE INITIALLY DEFERRED,
39         ident_value2            TEXT,
40         net_access_level        INT                             NOT NULL DEFAULT 1 REFERENCES config.net_access_level (id) DEFERRABLE INITIALLY DEFERRED,
41         photo_url               TEXT,
42         prefix                  TEXT,
43         first_given_name        TEXT                            NOT NULL,
44         second_given_name       TEXT,
45         family_name             TEXT                            NOT NULL,
46         suffix                  TEXT,
47         alias                   TEXT,
48         day_phone               TEXT,
49         evening_phone           TEXT,
50         other_phone             TEXT,
51         mailing_address         INT,
52         billing_address         INT,
53         home_ou                 INT                             NOT NULL,
54         dob                     TIMESTAMP WITH TIME ZONE,
55         active                  BOOL                            NOT NULL DEFAULT TRUE,
56         master_account          BOOL                            NOT NULL DEFAULT FALSE,
57         super_user              BOOL                            NOT NULL DEFAULT FALSE,
58         barred                  BOOL                            NOT NULL DEFAULT FALSE,
59         deleted                 BOOL                            NOT NULL DEFAULT FALSE,
60         usrgroup                SERIAL                          NOT NULL,
61         claims_returned_count   INT                             NOT NULL DEFAULT 0,
62         credit_forward_balance  NUMERIC(6,2)                    NOT NULL DEFAULT 0.00,
63         last_xact_id            TEXT                            NOT NULL DEFAULT 'none',
64         alert_message           TEXT,
65         create_date             TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
66         expire_date             TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT (now() + '3 years'::INTERVAL)
67 );
68 COMMENT ON TABLE actor.usr IS $$
69 /*
70  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
71  * Mike Rylander <mrylander@gmail.com>
72  *
73  * User objects
74  *
75  * This table contains the core User objects that describe both
76  * staff members and patrons.  The difference between the two
77  * types of users is based on the user's permissions.
78  *
79  * ****
80  *
81  * This program is free software; you can redistribute it and/or
82  * modify it under the terms of the GNU General Public License
83  * as published by the Free Software Foundation; either version 2
84  * of the License, or (at your option) any later version.
85  *
86  * This program is distributed in the hope that it will be useful,
87  * but WITHOUT ANY WARRANTY; without even the implied warranty of
88  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
89  * GNU General Public License for more details.
90  */
91 $$;
92
93 CREATE INDEX actor_usr_home_ou_idx ON actor.usr (home_ou);
94 CREATE INDEX actor_usr_mailing_address_idx ON actor.usr (mailing_address);
95 CREATE INDEX actor_usr_billing_address_idx ON actor.usr (billing_address);
96
97 CREATE INDEX actor_usr_first_given_name_idx ON actor.usr (lower(first_given_name));
98 CREATE INDEX actor_usr_second_given_name_idx ON actor.usr (lower(second_given_name));
99 CREATE INDEX actor_usr_family_name_idx ON actor.usr (lower(family_name));
100
101 CREATE INDEX actor_usr_email_idx ON actor.usr (lower(email));
102
103 CREATE INDEX actor_usr_day_phone_idx ON actor.usr (lower(day_phone));
104 CREATE INDEX actor_usr_evening_phone_idx ON actor.usr (lower(evening_phone));
105 CREATE INDEX actor_usr_other_phone_idx ON actor.usr (lower(other_phone));
106
107 CREATE INDEX actor_usr_ident_value_idx ON actor.usr (lower(ident_value));
108 CREATE INDEX actor_usr_ident_value2_idx ON actor.usr (lower(ident_value2));
109
110 CREATE FUNCTION actor.crypt_pw_insert () RETURNS TRIGGER AS $$
111         BEGIN
112                 NEW.passwd = MD5( NEW.passwd );
113                 RETURN NEW;
114         END;
115 $$ LANGUAGE PLPGSQL;
116
117 CREATE FUNCTION actor.crypt_pw_update () RETURNS TRIGGER AS $$
118         BEGIN
119                 IF NEW.passwd <> OLD.passwd THEN
120                         NEW.passwd = MD5( NEW.passwd );
121                 END IF;
122                 RETURN NEW;
123         END;
124 $$ LANGUAGE PLPGSQL;
125
126 CREATE TRIGGER actor_crypt_pw_update_trigger
127         BEFORE UPDATE ON actor.usr FOR EACH ROW
128         EXECUTE PROCEDURE actor.crypt_pw_update ();
129
130 CREATE TRIGGER actor_crypt_pw_insert_trigger
131         BEFORE INSERT ON actor.usr FOR EACH ROW
132         EXECUTE PROCEDURE actor.crypt_pw_insert ();
133
134 CREATE RULE protect_user_delete AS ON DELETE TO actor.usr DO INSTEAD UPDATE actor.usr SET deleted = TRUE WHERE OLD.id = actor.usr.id;
135
136 CREATE TABLE actor.usr_note (
137         id              BIGSERIAL                       PRIMARY KEY,
138         usr             BIGINT                          NOT NULL REFERENCES actor.usr ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
139         creator         BIGINT                          NOT NULL REFERENCES actor.usr ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
140         create_date     TIMESTAMP WITH TIME ZONE        DEFAULT NOW(),
141         pub             BOOL                            NOT NULL DEFAULT FALSE,
142         title           TEXT                            NOT NULL,
143         value           TEXT                            NOT NULL
144 );
145 CREATE INDEX actor_usr_note_usr_idx ON actor.usr_note (usr);
146
147 CREATE TABLE actor.usr_standing_penalty (
148         id                      SERIAL  PRIMARY KEY,
149         usr                     INT     NOT NULL REFERENCES actor.usr (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
150         standing_penalty        INT     NOT NULL REFERENCES config.standing_penalty (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
151         staff                   INT     REFERENCES actor.usr (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
152         set_date                TIMESTAMP WITH TIME ZONE        DEFAULT NOW()
153 );
154 COMMENT ON TABLE actor.usr_standing_penalty IS $$
155 /*
156  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
157  * Mike Rylander <mrylander@gmail.com>
158  *
159  * User standing penalties
160  *
161  * ****
162  *
163  * This program is free software; you can redistribute it and/or
164  * modify it under the terms of the GNU General Public License
165  * as published by the Free Software Foundation; either version 2
166  * of the License, or (at your option) any later version.
167  *
168  * This program is distributed in the hope that it will be useful,
169  * but WITHOUT ANY WARRANTY; without even the implied warranty of
170  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171  * GNU General Public License for more details.
172  */
173 $$;
174
175 CREATE INDEX actor_usr_standing_penalty_usr_idx ON actor.usr_standing_penalty (usr);
176
177 CREATE TABLE actor.usr_setting (
178         id      BIGSERIAL       PRIMARY KEY,
179         usr     INT             NOT NULL REFERENCES actor.usr ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
180         name    TEXT            NOT NULL,
181         value   TEXT            NOT NULL,
182         CONSTRAINT usr_once_per_key UNIQUE (usr,name)
183 );
184 COMMENT ON TABLE actor.usr_setting IS $$
185 /*
186  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
187  * Mike Rylander <mrylander@gmail.com>
188  *
189  * User settings
190  *
191  * This table contains any arbitrary settings that a client
192  * program would like to save for a user.
193  *
194  * ****
195  *
196  * This program is free software; you can redistribute it and/or
197  * modify it under the terms of the GNU General Public License
198  * as published by the Free Software Foundation; either version 2
199  * of the License, or (at your option) any later version.
200  *
201  * This program is distributed in the hope that it will be useful,
202  * but WITHOUT ANY WARRANTY; without even the implied warranty of
203  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
204  * GNU General Public License for more details.
205  */
206 $$;
207
208 CREATE INDEX actor_usr_setting_usr_idx ON actor.usr_setting (usr);
209
210
211 CREATE TABLE actor.stat_cat (
212         id              SERIAL  PRIMARY KEY,
213         owner           INT     NOT NULL,
214         name            TEXT    NOT NULL,
215         opac_visible    BOOL NOT NULL DEFAULT FALSE,
216         CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
217 );
218 COMMENT ON TABLE actor.stat_cat IS $$
219 /*
220  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
221  * Mike Rylander <mrylander@gmail.com>
222  *
223  * User Statistical Catagories
224  *
225  * Local data collected about Users is placed into a Statistical
226  * Catagory.  Here's where those catagories are defined.
227  *
228  * ****
229  *
230  * This program is free software; you can redistribute it and/or
231  * modify it under the terms of the GNU General Public License
232  * as published by the Free Software Foundation; either version 2
233  * of the License, or (at your option) any later version.
234  *
235  * This program is distributed in the hope that it will be useful,
236  * but WITHOUT ANY WARRANTY; without even the implied warranty of
237  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
238  * GNU General Public License for more details.
239  */
240 $$;
241
242
243 CREATE TABLE actor.stat_cat_entry (
244         id              SERIAL  PRIMARY KEY,
245         stat_cat        INT     NOT NULL,
246         owner           INT     NOT NULL,
247         value           TEXT    NOT NULL,
248         CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
249 );
250 COMMENT ON TABLE actor.stat_cat_entry IS $$
251 /*
252  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
253  * Mike Rylander <mrylander@gmail.com>
254  *
255  * User Statistical Catagory Entries
256  *
257  * Local data collected about Users is placed into a Statistical
258  * Catagory.  Each library can create entries into any of its own
259  * stat_cats, its ancestors' stat_cats, or its descendants' stat_cats.
260  *
261  *
262  * ****
263  *
264  * This program is free software; you can redistribute it and/or
265  * modify it under the terms of the GNU General Public License
266  * as published by the Free Software Foundation; either version 2
267  * of the License, or (at your option) any later version.
268  *
269  * This program is distributed in the hope that it will be useful,
270  * but WITHOUT ANY WARRANTY; without even the implied warranty of
271  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
272  * GNU General Public License for more details.
273  */
274 $$;
275
276
277 CREATE TABLE actor.stat_cat_entry_usr_map (
278         id              BIGSERIAL       PRIMARY KEY,
279         stat_cat_entry  TEXT            NOT NULL,
280         stat_cat        INT             NOT NULL,
281         target_usr      INT             NOT NULL,
282         CONSTRAINT sc_once_per_usr UNIQUE (target_usr,stat_cat)
283 );
284 COMMENT ON TABLE actor.stat_cat_entry_usr_map IS $$
285 /*
286  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
287  * Mike Rylander <mrylander@gmail.com>
288  *
289  * Statistical Catagory Entry to User map
290  *
291  * Records the stat_cat entries for each user.
292  *
293  *
294  * ****
295  *
296  * This program is free software; you can redistribute it and/or
297  * modify it under the terms of the GNU General Public License
298  * as published by the Free Software Foundation; either version 2
299  * of the License, or (at your option) any later version.
300  *
301  * This program is distributed in the hope that it will be useful,
302  * but WITHOUT ANY WARRANTY; without even the implied warranty of
303  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304  * GNU General Public License for more details.
305  */
306 $$;
307
308 CREATE INDEX actor_stat_cat_entry_usr_idx ON actor.stat_cat_entry_usr_map (target_usr);
309
310 CREATE TABLE actor.card (
311         id      SERIAL  PRIMARY KEY,
312         usr     INT     NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
313         barcode TEXT    NOT NULL UNIQUE,
314         active  BOOL    NOT NULL DEFAULT TRUE
315 );
316 COMMENT ON TABLE actor.card IS $$
317 /*
318  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
319  * Mike Rylander <mrylander@gmail.com>
320  *
321  * Library Cards
322  *
323  * Each User has one or more library cards.  The current "main"
324  * card is linked to here from the actor.usr table, and it is up
325  * to the consortium policy whether more than one card can be
326  * active for any one user at a given time.
327  *
328  *
329  * ****
330  *
331  * This program is free software; you can redistribute it and/or
332  * modify it under the terms of the GNU General Public License
333  * as published by the Free Software Foundation; either version 2
334  * of the License, or (at your option) any later version.
335  *
336  * This program is distributed in the hope that it will be useful,
337  * but WITHOUT ANY WARRANTY; without even the implied warranty of
338  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
339  * GNU General Public License for more details.
340  */
341 $$;
342
343 CREATE INDEX actor_card_usr_idx ON actor.card (usr);
344
345 CREATE TABLE actor.org_unit_type (
346         id              SERIAL  PRIMARY KEY,
347         name            TEXT    NOT NULL,
348         opac_label      TEXT    NOT NULL,
349         depth           INT     NOT NULL,
350         parent          INT     REFERENCES actor.org_unit_type (id) DEFERRABLE INITIALLY DEFERRED,
351         can_have_vols   BOOL    NOT NULL DEFAULT TRUE,
352         can_have_users  BOOL    NOT NULL DEFAULT TRUE
353 );
354 CREATE INDEX actor_org_unit_type_parent_idx ON actor.org_unit_type (parent);
355
356 CREATE TABLE actor.org_unit (
357         id              SERIAL  PRIMARY KEY,
358         parent_ou       INT     REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
359         ou_type         INT     NOT NULL REFERENCES actor.org_unit_type (id) DEFERRABLE INITIALLY DEFERRED,
360         ill_address     INT,
361         holds_address   INT,
362         mailing_address INT,
363         billing_address INT,
364         shortname       TEXT    NOT NULL,
365         name            TEXT    NOT NULL,
366         email           TEXT,
367         phone           TEXT,
368         opac_visible    BOOL    NOT NULL DEFAULT TRUE
369 );
370 CREATE INDEX actor_org_unit_parent_ou_idx ON actor.org_unit (parent_ou);
371 CREATE INDEX actor_org_unit_ou_type_idx ON actor.org_unit (ou_type);
372 CREATE INDEX actor_org_unit_ill_address_idx ON actor.org_unit (ill_address);
373 CREATE INDEX actor_org_unit_billing_address_idx ON actor.org_unit (billing_address);
374 CREATE INDEX actor_org_unit_mailing_address_idx ON actor.org_unit (mailing_address);
375 CREATE INDEX actor_org_unit_holds_address_idx ON actor.org_unit (holds_address);
376
377 CREATE TABLE actor.org_lasso (
378     id      SERIAL  PRIMARY KEY,
379     name        TEXT    UNIQUE
380 );
381
382 CREATE TABLE actor.org_lasso_map (
383     id          SERIAL  PRIMARY KEY,
384     lasso       INT     NOT NULL REFERENCES actor.org_lasso (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
385     org_unit    INT     NOT NULL REFERENCES actor.org_unit (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
386 );
387 CREATE UNIQUE INDEX ou_lasso_lasso_ou_idx ON actor.org_lasso_map (lasso, org_unit);
388 CREATE INDEX ou_lasso_org_unit_idx ON actor.org_lasso_map (org_unit);
389
390 CREATE TABLE actor.org_unit_proximity (
391         id              BIGSERIAL       PRIMARY KEY,
392         from_org        INT,
393         to_org          INT,
394         prox            INT
395 );
396 CREATE INDEX from_prox_idx ON actor.org_unit_proximity (from_org);
397
398 CREATE TABLE actor.hours_of_operation (
399         id              INT     PRIMARY KEY REFERENCES actor.org_unit (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
400         dow_0_open      TIME    NOT NULL DEFAULT '09:00',
401         dow_0_close     TIME    NOT NULL DEFAULT '17:00',
402         dow_1_open      TIME    NOT NULL DEFAULT '09:00',
403         dow_1_close     TIME    NOT NULL DEFAULT '17:00',
404         dow_2_open      TIME    NOT NULL DEFAULT '09:00',
405         dow_2_close     TIME    NOT NULL DEFAULT '17:00',
406         dow_3_open      TIME    NOT NULL DEFAULT '09:00',
407         dow_3_close     TIME    NOT NULL DEFAULT '17:00',
408         dow_4_open      TIME    NOT NULL DEFAULT '09:00',
409         dow_4_close     TIME    NOT NULL DEFAULT '17:00',
410         dow_5_open      TIME    NOT NULL DEFAULT '09:00',
411         dow_5_close     TIME    NOT NULL DEFAULT '17:00',
412         dow_6_open      TIME    NOT NULL DEFAULT '09:00',
413         dow_6_close     TIME    NOT NULL DEFAULT '17:00'
414 );
415
416 CREATE TABLE actor.org_unit_closed (
417         id              SERIAL                          PRIMARY KEY,
418         org_unit        INT                             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
419         close_start     TIMESTAMP WITH TIME ZONE        NOT NULL,
420         close_end       TIMESTAMP WITH TIME ZONE        NOT NULL,
421         reason          TEXT
422 );
423
424 -- Workstation registration...
425 CREATE TABLE actor.workstation (
426         id              SERIAL  PRIMARY KEY,
427         name            TEXT    NOT NULL UNIQUE,
428         owning_lib      INT     NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED
429 );
430
431 CREATE TABLE actor.usr_org_unit_opt_in (
432         id              SERIAL                          PRIMARY KEY,
433         org_unit        INT                             NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
434         usr             INT                             NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
435         staff           INT                             NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
436         opt_in_ts       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT NOW(),
437         opt_in_ws       INT                             NOT NULL REFERENCES actor.workstation (id) DEFERRABLE INITIALLY DEFERRED,
438         CONSTRAINT usr_opt_in_once_per_org_unit UNIQUE (usr,org_unit)
439 );
440
441 CREATE TABLE actor.org_unit_setting (
442         id              BIGSERIAL       PRIMARY KEY,
443         org_unit        INT             NOT NULL REFERENCES actor.org_unit ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
444         name            TEXT            NOT NULL,
445         value           TEXT            NOT NULL,
446         CONSTRAINT ou_once_per_key UNIQUE (org_unit,name)
447 );
448 COMMENT ON TABLE actor.org_unit_setting IS $$
449 /*
450  * Copyright (C) 2005-2008  Equinox Software, Inc. / Georgia Public Library Service 
451  * Mike Rylander <mrylander@gmail.com>
452  *
453  * Org Unit settings
454  *
455  * This table contains any arbitrary settings that a client
456  * program would like to save for an org unit.
457  *
458  * ****
459  *
460  * This program is free software; you can redistribute it and/or
461  * modify it under the terms of the GNU General Public License
462  * as published by the Free Software Foundation; either version 2
463  * of the License, or (at your option) any later version.
464  *
465  * This program is distributed in the hope that it will be useful,
466  * but WITHOUT ANY WARRANTY; without even the implied warranty of
467  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
468  * GNU General Public License for more details.
469  */
470 $$;
471
472 CREATE INDEX actor_org_unit_setting_usr_idx ON actor.org_unit_setting (org_unit);
473
474
475 CREATE TABLE actor.usr_address (
476         id                      SERIAL  PRIMARY KEY,
477         valid                   BOOL    NOT NULL DEFAULT TRUE,
478         within_city_limits      BOOL    NOT NULL DEFAULT TRUE,
479         address_type            TEXT    NOT NULL DEFAULT 'MAILING',
480         usr                     INT     NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
481         street1                 TEXT    NOT NULL,
482         street2                 TEXT,
483         city                    TEXT    NOT NULL,
484         county                  TEXT,
485         state                   TEXT    NOT NULL,
486         country                 TEXT    NOT NULL,
487         post_code               TEXT    NOT NULL
488 );
489
490 CREATE INDEX actor_usr_addr_usr_idx ON actor.usr_address (usr);
491
492 CREATE INDEX actor_usr_addr_street1_idx ON actor.usr_address (lower(street1));
493 CREATE INDEX actor_usr_addr_street2_idx ON actor.usr_address (lower(street2));
494
495 CREATE INDEX actor_usr_addr_city_idx ON actor.usr_address (lower(city));
496 CREATE INDEX actor_usr_addr_state_idx ON actor.usr_address (lower(state));
497 CREATE INDEX actor_usr_addr_post_code_idx ON actor.usr_address (lower(post_code));
498
499
500 CREATE TABLE actor.org_address (
501         id              SERIAL  PRIMARY KEY,
502         valid           BOOL    NOT NULL DEFAULT TRUE,
503         address_type    TEXT    NOT NULL DEFAULT 'MAILING',
504         org_unit        INT     NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
505         street1         TEXT    NOT NULL,
506         street2         TEXT,
507         city            TEXT    NOT NULL,
508         county          TEXT,
509         state           TEXT    NOT NULL,
510         country         TEXT    NOT NULL,
511         post_code       TEXT    NOT NULL
512 );
513
514 CREATE INDEX actor_org_address_org_unit_idx ON actor.org_address (org_unit);
515
516 CREATE OR REPLACE FUNCTION public.first5 ( TEXT ) RETURNS TEXT AS $$
517         SELECT SUBSTRING( $1, 1, 5);
518 $$ LANGUAGE SQL;
519
520 COMMIT;