~singpolyma/jmp-schemas

ef5cc7b337f0dda3a8882c166f0ccb547f09c231 — Stephen Paul Weber 2 months ago 69c1efe
Require settled_after to be NOT NULL
A deploy/require_settled_after.sql => deploy/require_settled_after.sql +36 -0
@@ 0,0 1,36 @@
-- Deploy jmp:require_settled_after to pg
-- requires: settled_after
-- requires: insert_charge_for_cdr

BEGIN;

CREATE OR REPLACE FUNCTION insert_charge_for_cdr() RETURNS TRIGGER AS
$insert_charge_for_cdr$
	DECLARE
		raw_rate NUMERIC;
		raw_charge NUMERIC;
		final_charge NUMERIC;
	BEGIN
		SELECT
			rate, charge INTO raw_rate, raw_charge
		FROM cdr_with_charge WHERE cdr_id=NEW.cdr_id;
		final_charge := cdr_charge(NEW.start, NEW.customer_id, raw_rate, raw_charge);
		IF final_charge > 0.0 THEN
			INSERT INTO transactions (
				customer_id, transaction_id, created_at, settled_after, amount, note
			) VALUES (
				NEW.customer_id,
				'cdr_charge_for_' || NEW.cdr_id,
				NEW.start + MAKE_INTERVAL(secs := NEW.billsec),
				NEW.start + MAKE_INTERVAL(secs := NEW.billsec),
				-final_charge,
				'Charge for ' || (CEIL(NEW.billsec / 60.0)) || ' minute call'
			);
		END IF;
		RETURN NEW;
	END;
$insert_charge_for_cdr$ LANGUAGE plpgsql;

ALTER TABLE transactions ALTER COLUMN settled_after SET NOT NULL;

COMMIT;

A revert/require_settled_after.sql => revert/require_settled_after.sql +33 -0
@@ 0,0 1,33 @@
-- Revert jmp:require_settled_after from pg

BEGIN;

ALTER TABLE transactions ALTER COLUMN settled_after DROP NOT NULL;

CREATE OR REPLACE FUNCTION insert_charge_for_cdr() RETURNS TRIGGER AS
$insert_charge_for_cdr$
	DECLARE
		raw_rate NUMERIC;
		raw_charge NUMERIC;
		final_charge NUMERIC;
	BEGIN
		SELECT
			rate, charge INTO raw_rate, raw_charge
		FROM cdr_with_charge WHERE cdr_id=NEW.cdr_id;
		final_charge := cdr_charge(NEW.start, NEW.customer_id, raw_rate, raw_charge);
		IF final_charge > 0.0 THEN
			INSERT INTO transactions (
				customer_id, transaction_id, created_at, amount, note
			) VALUES (
				NEW.customer_id,
				'cdr_charge_for_' || NEW.cdr_id,
				NEW.start + MAKE_INTERVAL(secs := NEW.billsec),
				-final_charge,
				'Charge for ' || (CEIL(NEW.billsec / 60.0)) || ' minute call'
			);
		END IF;
		RETURN NEW;
	END;
$insert_charge_for_cdr$ LANGUAGE plpgsql;

COMMIT;

M sqitch.plan => sqitch.plan +2 -0
@@ 35,3 35,5 @@ invites_rewarded_at [invites] 2022-02-22T16:54:33Z Stephen Paul Weber,,, <singpo

settled_after [transactions] 2022-04-11T17:34:18Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Consider transactions settled after some time
@2022102 2022-04-12T17:47:32Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Add settled_after to transactions

require_settled_after [settled_after insert_charge_for_cdr] 2022-04-12T18:45:50Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Make sure settled_after is NOT NULL

M verify/invite_per_15.sql => verify/invite_per_15.sql +46 -18
@@ 2,23 2,51 @@

BEGIN;

INSERT INTO transactions
	(customer_id, transaction_id, amount)
	VALUES ('test1', 'test1a', 5);
INSERT INTO transactions
	(customer_id, transaction_id, amount)
	VALUES ('test1', 'test1b', 15);
INSERT INTO transactions
	(customer_id, transaction_id, amount)
	VALUES ('test1', 'test1c', 30);
INSERT INTO transactions
	(customer_id, transaction_id, amount)
	VALUES ('test1', 'test1d', -45);

SELECT 1/COUNT(1)
FROM
	(SELECT COUNT(1) AS c FROM invites WHERE creator_id='test1') t
WHERE
	c=3;
DO $$
DECLARE
	settled_after_exists INTEGER;
	invite_count INTEGER;
BEGIN

SELECT count(1)
FROM information_schema.columns
WHERE table_name='transactions' AND column_name='settled_after'
INTO settled_after_exists;

IF settled_after_exists = 1 THEN
	INSERT INTO transactions
		(customer_id, transaction_id, settled_after, amount)
		VALUES ('test1', 'test1a', localtimestamp, 5);
	INSERT INTO transactions
		(customer_id, transaction_id, settled_after, amount)
		VALUES ('test1', 'test1b', localtimestamp, 15);
	INSERT INTO transactions
		(customer_id, transaction_id, settled_after, amount)
		VALUES ('test1', 'test1c', localtimestamp, 30);
	INSERT INTO transactions
		(customer_id, transaction_id, settled_after, amount)
		VALUES ('test1', 'test1d', localtimestamp, -45);
ELSE
	INSERT INTO transactions
		(customer_id, transaction_id, amount)
		VALUES ('test1', 'test1a', 5);
	INSERT INTO transactions
		(customer_id, transaction_id, amount)
		VALUES ('test1', 'test1b', 15);
	INSERT INTO transactions
		(customer_id, transaction_id, amount)
		VALUES ('test1', 'test1c', 30);
	INSERT INTO transactions
		(customer_id, transaction_id, amount)
		VALUES ('test1', 'test1d', -45);
END IF;

SELECT COUNT(1) AS c FROM invites WHERE creator_id='test1' INTO invite_count;

IF invite_count IS NULL OR invite_count <> 3 THEN
	RAISE EXCEPTION 'Expected 3 invites, got: %', invite_count;
END IF;

END $$;

ROLLBACK;

A verify/require_settled_after.sql => verify/require_settled_after.sql +16 -0
@@ 0,0 1,16 @@
-- Verify jmp:require_settled_after on pg

BEGIN;

DO $$
BEGIN
	INSERT INTO transactions
		(customer_id, transaction_id, amount)
	VALUES
		('test', 'test', 0);

	RAISE EXCEPTION 'INSERT should have failed, but did not';
EXCEPTION WHEN not_null_violation THEN
END $$;

ROLLBACK;