-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
10 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
db/migrations/20241230123042_improve-gavebrev-checkins-to-create.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- migrate:up | ||
drop view gavebrev_checkins_to_create; | ||
|
||
create view gavebrev_checkins_to_create as | ||
select | ||
* | ||
from ( select distinct on (g.donor_id) | ||
g.donor_id as donor_id, | ||
coalesce(c.year + 1, date_part('year', g.created_at)::numeric) as year, | ||
coalesce(c.income_verified, coalesce(c.income_preliminary, coalesce(c.income_inferred, 0))) as income_inferred | ||
from | ||
gavebrev g | ||
left join gavebrev_checkin c on g.donor_id = c.donor_id | ||
where | ||
g.status = 'signed' | ||
and g.stopped_at >= now() | ||
order by | ||
g.donor_id, | ||
c.year desc) s | ||
where | ||
year <= date_part('year', now()); | ||
|
||
grant select on gavebrev_checkins_to_create to reader_sensitive; | ||
|
||
select | ||
cron.schedule('create-gavebrev-checkins', '0 4 * * *', 'insert into gavebrev_checkin(donor_id, year, income_inferred) select * from gavebrev_checkins_to_create'); | ||
|
||
-- migrate:down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters