Skip to content

Instantly share code, notes, and snippets.

@mbutsko
Last active June 4, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbutsko/939f4490560da85035c3335c02c07ca9 to your computer and use it in GitHub Desktop.
Save mbutsko/939f4490560da85035c3335c02c07ca9 to your computer and use it in GitHub Desktop.
Reactivate improperly deactivated scheduled closure endorsements
sql = "
with latest_scheduled_closure_endorsements as ( SELECT * FROM ( select ptr.*, DENSE_RANK() OVER newest_first FROM scheduled_closure_endorsements ptr WINDOW newest_first as (PARTITION BY ptr.policy_term_id ORDER BY ptr.created_at DESC)) _ WHERE DENSE_RANK = 1)
, latest_policy_term_revisions as ( SELECT * FROM ( select ptr.*, DENSE_RANK() OVER newest_first FROM policy_term_revisions ptr WINDOW newest_first as (PARTITION BY ptr.policy_term_id ORDER BY ptr.created_at DESC)) _ WHERE DENSE_RANK = 1)
select sce.id from latest_scheduled_closure_endorsements sce
join policy_term_revisions ptr on ptr.quote_id=sce.quote_id
and ptr.revision_type='endorsement' -- Because you could have reinstated directly on to your target
join scheduled_closure_endorsement_policy_payment_amounts sceppa on sceppa.scheduled_closure_endorsement_id=sce.id
join policy_payment_amounts ppa on ppa.id=sceppa.policy_payment_amount_id
join latest_policy_term_revisions lptr on lptr.policy_term_id=sce.policy_term_id
left outer join repayment_plans rp on rp.policy_term_id=sce.policy_term_id and rp.missed_scheduled_closure_endorsement_id is not null
join scheduled_payments sp on sp.id=sce.scheduled_payment_id
where sce.deactivated_at is not null
and sce.transaction_date is null
and rp.id is null
and lptr.revision_type != 'cancellation'
group by sce.id
having sum(premium_in_cents) > 0;
"
sces = ScheduledClosureEndorsement.find_by_sql(sql)
raise "Bad count" unless sces.count == 132
sces.each {|sce| ScheduledClosureEndorsement.find(sce.id).update(deactivated_at: nil) }
raise "Did not update all records" unless ScheduledClosureEndorsement.find_by_sql(sql).count == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment