Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Switch to a single instructions table
Browse files Browse the repository at this point in the history
To include both payments and payroll.
  • Loading branch information
chadwhitacre committed Jul 30, 2015
1 parent 1fd8f38 commit 9fa6a49
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
BEGIN;

-- https://github.com/gratipay/inside.gratipay.com/issues/117
-- payment_instructions - A user instructs Gratipay to make voluntary payments to a Team.
-- instructions - A user instructs Gratipay to make voluntary payments to a Team.

CREATE TYPE kind_of_instruction AS ENUM ('payment', 'payroll');

ALTER TABLE subscriptions RENAME COLUMN subscriber TO participant;
ALTER TABLE subscriptions RENAME CONSTRAINT subscriptions_subscriber_fkey
TO payment_instructions_participant_fkey;
TO instructions_participant_fkey;
ALTER TABLE subscriptions RENAME CONSTRAINT subscriptions_team_fkey
TO payment_instructions_team_fkey;
ALTER TABLE subscriptions RENAME TO payment_instructions;
ALTER INDEX subscriptions_pkey RENAME TO payment_instructions_pkey;
ALTER INDEX subscriptions_all RENAME TO payment_instructions_all;
ALTER SEQUENCE subscriptions_id_seq RENAME TO payment_instructions_id_seq;
TO instructions_team_fkey;
ALTER TABLE subscriptions ADD COLUMN kind kind_of_instruction NOT NULL DEFAULT 'payment';
ALTER TABLE subscriptions RENAME TO instructions;
ALTER INDEX subscriptions_pkey RENAME TO instructions_pkey;
ALTER INDEX subscriptions_all RENAME TO instructions_all;
ALTER SEQUENCE subscriptions_id_seq RENAME TO instructions_id_seq;

DROP TRIGGER update_current_subscription ON current_subscriptions;
DROP VIEW current_subscriptions;
CREATE VIEW current_payment_instructions AS
CREATE VIEW current_instructions AS
SELECT DISTINCT ON (participant, team) *
FROM payment_instructions
FROM instructions
ORDER BY participant, team, mtime DESC;

-- Allow updating is_funded via the current_payment_instructions view for convenience
-- Allow updating is_funded via the current_instructions view for convenience
DROP FUNCTION update_subscription();
CREATE FUNCTION update_payment_instruction() RETURNS trigger AS $$
CREATE FUNCTION update_instruction() RETURNS trigger AS $$
BEGIN
UPDATE payment_instructions
UPDATE instructions
SET is_funded = NEW.is_funded
WHERE id = NEW.id;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_current_payment_instruction
INSTEAD OF UPDATE ON current_payment_instructions
FOR EACH ROW EXECUTE PROCEDURE update_payment_instruction();
CREATE TRIGGER update_current_instruction
INSTEAD OF UPDATE ON current_instructions
FOR EACH ROW EXECUTE PROCEDURE update_instruction();

END;

0 comments on commit 9fa6a49

Please sign in to comment.