-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE participants ADD COLUMN takes numeric(35,2) NOT NULL DEFAULT 0; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Changaco
Author
Contributor
|
||
|
||
CREATE TEMPORARY TABLE temp_teams AS | ||
SELECT username, receiving | ||
FROM participants | ||
WHERE "number" = 'plural'; | ||
|
||
CREATE TEMPORARY TABLE temp_takes | ||
( team text | ||
, member text | ||
, amount numeric(35,2) | ||
); | ||
|
||
CREATE FUNCTION process_take() RETURNS trigger AS $$ | ||
DECLARE | ||
actual_amount numeric(35,2); | ||
team_balance numeric(35,2); | ||
BEGIN | ||
team_balance := ( | ||
SELECT receiving | ||
FROM temp_teams | ||
WHERE username = NEW.team | ||
); | ||
actual_amount := NEW.amount; | ||
IF (team_balance < NEW.amount) THEN | ||
actual_amount := team_balance; | ||
END IF; | ||
UPDATE participants | ||
SET takes = (takes + actual_amount) | ||
, receiving = (receiving + actual_amount) | ||
WHERE username = NEW.member; | ||
UPDATE temp_teams | ||
SET receiving = (receiving - actual_amount) | ||
This comment has been minimized.
Sorry, something went wrong.
chadwhitacre
Contributor
|
||
WHERE username = NEW.team; | ||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER process_take AFTER INSERT ON temp_takes | ||
FOR EACH ROW EXECUTE PROCEDURE process_take(); | ||
|
||
INSERT INTO temp_takes | ||
SELECT team, member, amount | ||
FROM current_takes t | ||
WHERE t.amount > 0 | ||
ORDER BY ctime DESC; | ||
|
||
END; |
1 comment
on commit b7aa3d1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good w/ this commit.
Everything after this line is a one-off, right? Is the
process_take
function something we need to keep around in the db? If not, do we need to delete it here?