From 2ffb26dc4432bfc1219159e52a934173b374c51d Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 10 Aug 2021 14:57:15 -0500 Subject: [PATCH 1/3] Make days correct --- bin/months_to_balance | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/months_to_balance b/bin/months_to_balance index 31e928649ef063906fd5faa0705230657166fe26..281f40e1bf0dd846cd11f287e6652c5a627bde1a 100755 --- a/bin/months_to_balance +++ b/bin/months_to_balance @@ -30,7 +30,7 @@ rows.each do |r| 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)) + UPDATE plan_log SET date_range = date_range - tsrange(LOCALTIMESTAMP + '1 month' + make_interval(days := (DATE_PART('day', UPPER(date_range)) - DATE_PART('day', LOCALTIMESTAMP))::int), UPPER(date_range)) WHERE customer_id=$1 AND UPPER(date_range)=$2 SQL end From a07d5bb0007d71eccf28ad0ee92513b0ccdabe77 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 10 Aug 2021 15:02:51 -0500 Subject: [PATCH 2/3] Give everyone $5.0001 to prevent low balance and eat one month This way they keep most of their activation time, and the side effect of interest is mitigated. --- bin/months_to_balance | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/months_to_balance b/bin/months_to_balance index 281f40e1bf0dd846cd11f287e6652c5a627bde1a..ae338b89bcafeb77dec5c067f4804ba21535e4f7 100755 --- a/bin/months_to_balance +++ b/bin/months_to_balance @@ -25,12 +25,12 @@ SQL rows.each do |r| cid = r["customer_id"] DB.transaction do - DB.exec(<<~SQL, [cid, "#{cid}-months_to_balance", r["amount"]]) + DB.exec(<<~SQL, [cid, "#{cid}-prevent_low_balance"]) INSERT INTO transactions (customer_id, transaction_id, amount, note) - VALUES ($1, $2, $3, 'months_to_balance') + VALUES ($1, $2, 5.0001, 'Prevent low balance') SQL DB.exec(<<~SQL, [cid, r["expires_at"]]) - UPDATE plan_log SET date_range = date_range - tsrange(LOCALTIMESTAMP + '1 month' + make_interval(days := (DATE_PART('day', UPPER(date_range)) - DATE_PART('day', LOCALTIMESTAMP))::int), UPPER(date_range)) + UPDATE plan_log SET date_range = date_range - tsrange(UPPER(date_range) - '1 month', UPPER(date_range)) WHERE customer_id=$1 AND UPPER(date_range)=$2 SQL end From bf64ce80f14ad90ef50994a5b5c6d93a2c1397b7 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 10 Aug 2021 15:40:26 -0500 Subject: [PATCH 3/3] Not going to run this, new plan --- bin/months_to_balance | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100755 bin/months_to_balance diff --git a/bin/months_to_balance b/bin/months_to_balance deleted file mode 100755 index ae338b89bcafeb77dec5c067f4804ba21535e4f7..0000000000000000000000000000000000000000 --- a/bin/months_to_balance +++ /dev/null @@ -1,37 +0,0 @@ -#!/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}-prevent_low_balance"]) - INSERT INTO transactions (customer_id, transaction_id, amount, note) - VALUES ($1, $2, 5.0001, 'Prevent low balance') - SQL - DB.exec(<<~SQL, [cid, r["expires_at"]]) - UPDATE plan_log SET date_range = date_range - tsrange(UPPER(date_range) - '1 month', UPPER(date_range)) - WHERE customer_id=$1 AND UPPER(date_range)=$2 - SQL - end -end