~singpolyma/jmp-schemas

589f2e6bed96c7d358b9daabb8ccddd57afb8b64 — Stephen Paul Weber 10 months ago 1bef640
For every $15 deposited, add one invite code
4 files changed, 56 insertions(+), 0 deletions(-)

A deploy/invite_per_15.sql
A revert/invite_per_15.sql
M sqitch.plan
A verify/invite_per_15.sql
A deploy/invite_per_15.sql => deploy/invite_per_15.sql +23 -0
@@ 0,0 1,23 @@
-- Deploy jmp:invite_per_15 to pg
-- requires: transactions
-- requires: invites

BEGIN;

CREATE OR REPLACE FUNCTION insert_invites_per() RETURNS TRIGGER AS
$insert_invites_per$
	BEGIN
		FOR i IN 1..(NEW.amount::int / TG_ARGV[0]::int) LOOP
			INSERT INTO invites (creator_id) VALUES (NEW.customer_id);
		END LOOP;
		RETURN NEW;
	END;
$insert_invites_per$ LANGUAGE plpgsql;

CREATE TRIGGER invite_per_15
	AFTER INSERT ON transactions
	FOR EACH ROW
	WHEN (NEW.amount >= 15)
	EXECUTE PROCEDURE insert_invites_per(15);

COMMIT;

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

BEGIN;

DROP TRIGGER invite_per_15 ON transactions;
DROP FUNCTION insert_invites_per;

COMMIT;

M sqitch.plan => sqitch.plan +1 -0
@@ 14,3 14,4 @@ plan_log_with_range [plan_log customer_plans] 2021-05-03T19:30:36Z Stephen Paul 

invites 2021-05-17T20:31:00Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Table to store invite codes and their state
unused_invites 2021-05-17T20:40:20Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # View for invites not yet used
invite_per_15 [transactions invites] 2021-08-18T17:46:38Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # $15 gives you an invite code

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

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;

ROLLBACK;