From 3df6bdbe362b3c76da69089616f518ac1c59121c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 2 Aug 2021 14:26:39 -0500 Subject: [PATCH] Add one-off script to convert long activation ranges to balance Fix-up for the web register previously doing 5 month activations with 0 balance instead of adding balance + 1 month activation. --- bin/months_to_balance | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 bin/months_to_balance diff --git a/bin/months_to_balance b/bin/months_to_balance new file mode 100755 index 0000000000000000000000000000000000000000..31e928649ef063906fd5faa0705230657166fe26 --- /dev/null +++ b/bin/months_to_balance @@ -0,0 +1,37 @@ +#!/usr/bin/ruby +# frozen_string_literal: true + +# This is meant to be a one-time script that takes any "extra" +# months an account is activated for and rolls them back into balance instead +# Mostly as a fix for all the time that web activation spent not setting balance + +require "pg" + +DB = PG.connect(dbname: "jmp") +DB.type_map_for_results = PG::BasicTypeMapForResults.new(DB) +DB.type_map_for_queries = PG::BasicTypeMapForQueries.new(DB) + +rows = DB.query(<<~SQL) + SELECT customer_id, expires_at, + ((((DATE_PART('year', expires_at) - DATE_PART('year', LOCALTIMESTAMP)) * 12 + + (DATE_PART('month', expires_at) - DATE_PART('month', LOCALTIMESTAMP))) - 1) * + case plan_name + when 'usd_beta_unlimited-v20210223' then 2.99 + when 'cad_beta_unlimited-v20210223' then 3.59 + end)::NUMERIC(12,4) AS amount + FROM customer_plans WHERE expires_at > (DATE_TRUNC('month', LOCALTIMESTAMP) + '2 months') ORDER BY expires_at; +SQL + +rows.each do |r| + cid = r["customer_id"] + DB.transaction do + DB.exec(<<~SQL, [cid, "#{cid}-months_to_balance", r["amount"]]) + INSERT INTO transactions (customer_id, transaction_id, amount, note) + VALUES ($1, $2, $3, 'months_to_balance') + SQL + DB.exec(<<~SQL, [cid, r["expires_at"]]) + UPDATE plan_log SET date_range = date_range - tsrange(LOCALTIMESTAMP + '1 month', UPPER(date_range)) + WHERE customer_id=$1 AND UPPER(date_range)=$2 + SQL + end +end