3 CREATE OR REPLACE FUNCTION config.update_hard_due_dates () RETURNS INT AS $func$
5 temp_value config.hard_due_date_values%ROWTYPE;
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
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
21 updated := updated + 1;
27 $func$ LANGUAGE plpgsql;