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

Commit

Permalink
Oops, forgot to add changes to schema.sql; #141
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Apr 3, 2013
1 parent 0f1af36 commit 4db1d3e
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,52 @@ CREATE VIEW goal_summary AS
JOIN participants p3 ON p3.id = tips_agg.tippee
WHERE goal > 0
GROUP BY tippee, goal, percentage, statement;


-------------------------------------------------------------------------------
-- https://github.com/gittip/www.gittip.com/issues/141

-- Create a goals table to track all goals a participant has stated over time.
CREATE TABLE goals
( id serial PRIMARY KEY
, ctime timestamp with time zone NOT NULL
, mtime timestamp with time zone NOT NULL
DEFAULT CURRENT_TIMESTAMP
, participant text NOT NULL
REFERENCES participants
ON UPDATE CASCADE
ON DELETE RESTRICT
, goal numeric(35,2) DEFAULT NULL
);


BEGIN;

-- Migrate data from goal column of participants over to new goals table.
INSERT INTO goals (ctime, mtime, participant, goal)
SELECT CURRENT_TIMESTAMP
, CURRENT_TIMESTAMP
, id
, goal
FROM participants
WHERE goal IS NOT NULL;

-- Create a rule to log changes to participant.goal into goals.
CREATE RULE log_goal_changes
AS ON UPDATE TO participants
WHERE (OLD.goal IS NULL AND NOT NEW.goal IS NULL)
OR (NEW.goal IS NULL AND NOT OLD.goal IS NULL)
OR NEW.goal <> OLD.goal
DO
INSERT INTO goals
(ctime, participant, goal)
VALUES ( COALESCE (( SELECT ctime
FROM goals
WHERE participant=OLD.id
LIMIT 1
), CURRENT_TIMESTAMP)
, OLD.id
, NEW.goal
);

END;

0 comments on commit 4db1d3e

Please sign in to comment.