]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1054.data.tz_org_setting.sql
LP#1759238: stamping upgrade script
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1054.data.tz_org_setting.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1054', :eg_version);
4
5 INSERT into config.org_unit_setting_type
6 ( name, grp, label, description, datatype ) VALUES
7
8 ( 'lib.timezone', 'lib',
9     oils_i18n_gettext('lib.timezone',
10         'Library time zone',
11         'coust', 'label'),
12     oils_i18n_gettext('lib.timezone',
13         'Define the time zone in which a library physically resides',
14         'coust', 'description'),
15     'string');
16
17 ALTER TABLE actor.org_unit_closed ADD COLUMN full_day BOOLEAN DEFAULT FALSE;
18 ALTER TABLE actor.org_unit_closed ADD COLUMN multi_day BOOLEAN DEFAULT FALSE;
19
20 UPDATE actor.org_unit_closed SET multi_day = TRUE
21   WHERE close_start::DATE <> close_end::DATE;
22
23 UPDATE actor.org_unit_closed SET full_day = TRUE
24   WHERE close_start::DATE = close_end::DATE
25         AND SUBSTRING(close_start::time::text FROM 1 FOR 8) = '00:00:00'
26         AND SUBSTRING(close_end::time::text FROM 1 FOR 8) = '23:59:59';
27
28 CREATE OR REPLACE FUNCTION action.push_circ_due_time () RETURNS TRIGGER AS $$
29 DECLARE
30     proper_tz TEXT := COALESCE(
31         oils_json_to_text((
32             SELECT value
33               FROM  actor.org_unit_ancestor_setting('lib.timezone',NEW.circ_lib)
34               LIMIT 1
35         )),
36         CURRENT_SETTING('timezone')
37     );
38 BEGIN
39
40     IF (EXTRACT(EPOCH FROM NEW.duration)::INT % EXTRACT(EPOCH FROM '1 day'::INTERVAL)::INT) = 0 -- day-granular duration
41         AND SUBSTRING((NEW.due_date AT TIME ZONE proper_tz)::TIME::TEXT FROM 1 FOR 8) <> '23:59:59' THEN -- has not yet been pushed
42         NEW.due_date = ((NEW.due_date AT TIME ZONE proper_tz)::DATE + '1 day'::INTERVAL - '1 second'::INTERVAL) || ' ' || proper_tz;
43     END IF;
44
45     RETURN NEW;
46 END;
47 $$ LANGUAGE PLPGSQL;
48
49 COMMIT;
50
51 \qecho The following query will adjust all historical, unaged circulations so
52 \qecho that if their due date field is pushed to the end of the day, it is done
53 \qecho in the circulating library''''s time zone, and not the server time zone.
54 \qecho 
55 \qecho It is safe to run this after any change to library time zones.
56 \qecho 
57 \qecho Running this is not required, as no code before this change has
58 \qecho depended on the time string of '''23:59:59'''.  It is also not necessary
59 \qecho if all of your libraries are in the same time zone, and that time zone
60 \qecho is the same as the database''''s configured time zone.
61 \qecho 
62 \qecho 'DO $$'
63 \qecho 'declare'
64 \qecho '    new_tz  text;'
65 \qecho '    ou_id   int;'
66 \qecho 'begin'
67 \qecho '    for ou_id in select id from actor.org_unit loop'
68 \qecho '        for new_tz in select oils_json_to_text(value) from actor.org_unit_ancestor_setting('''lib.timezone''',ou_id) loop'
69 \qecho '            if new_tz is not null then'
70 \qecho '                update  action.circulation'
71 \qecho '                  set   due_date = (due_date::timestamp || ''' ''' || new_tz)::timestamptz'
72 \qecho '                  where circ_lib = ou_id'
73 \qecho '                        and substring((due_date at time zone new_tz)::time::text from 1 for 8) <> '''23:59:59''';'
74 \qecho '            end if;'
75 \qecho '        end loop;'
76 \qecho '    end loop;'
77 \qecho 'end;'
78 \qecho '$$;'
79 \qecho