]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/xxxx.function.config_update_hard_due_dates_ceiling_date_fix.sql
LP#1427392 - Allow deletion of Hard Due Date Values.
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / xxxx.function.config_update_hard_due_dates_ceiling_date_fix.sql
1 BEGIN;
2
3 CREATE OR REPLACE FUNCTION config.update_hard_due_dates () RETURNS INT AS $func$
4 DECLARE
5     temp_value  config.hard_due_date_values%ROWTYPE;
6     updated     INT := 0;
7 BEGIN
8     FOR temp_value IN
9       SELECT  DISTINCT ON (hard_due_date) *
10         FROM  config.hard_due_date_values
11         WHERE active_date <= NOW() -- We've passed (or are at) the rollover time
12         ORDER BY hard_due_date, active_date DESC -- Latest (nearest to us) active time
13    LOOP
14         UPDATE  config.hard_due_date
15           SET   ceiling_date = temp_value.ceiling_date
16           WHERE id = temp_value.hard_due_date
17                 AND ceiling_date <> temp_value.ceiling_date -- Time is equal if we've already updated the chdd
18                 AND temp_value.ceiling_date >= NOW(); -- Don't update ceiling dates to the past
19
20         IF FOUND THEN
21             updated := updated + 1;
22         END IF;
23     END LOOP;
24
25     RETURN updated;
26 END;
27 $func$ LANGUAGE plpgsql;
28
29 COMMIT;