This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to a single instructions table
To include both payments and payroll.
- Loading branch information
1 parent
1fd8f38
commit 9fa6a49
Showing
1 changed file
with
19 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |