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

Commit

Permalink
Move new SQL to branch.sql; #141
Browse files Browse the repository at this point in the history
Let's try out this pattern: feature branches should collect new SQL
statements in a branch.sql file, and if this exists we'll apply it
during makedb.sh. That should avoid some conflicts when merging from
master out to feature branches, as well as making it clearer that
branch.sql is a work in progress.
  • Loading branch information
chadwhitacre committed Apr 3, 2013
1 parent ebb1c83 commit 90bbdd8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
47 changes: 47 additions & 0 deletions branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-------------------------------------------------------------------------------
-- 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;
9 changes: 9 additions & 0 deletions makedb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ echo
psql -U $OWNER $DBNAME < enforce-utc.sql
psql -U $OWNER $DBNAME < schema.sql

echo "=============================================================================="
echo "Looking for branch.sql ..."
echo

if [ -f branch.sql ]
then psql -U $OWNER $DBNAME < branch.sql
else echo "None found."
fi

echo
echo "=============================================================================="
49 changes: 0 additions & 49 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -349,52 +349,3 @@ 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 90bbdd8

Please sign in to comment.