Commit 26a9370d authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Eliminate recreation of database from testing.

Instead of dropping and recreating the database we now drop all the
tables and then recreate them.  This is because in our staging server I
currently have just one database available and I can't drop and recreate
it to run tests in it.

Additionally eliminated the update_votes.sql script - the triggers
themselves take care of votes being calculated.
parent 513a0a63
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -18,11 +18,8 @@ desc "Run Jasmine specs for comments backend"
task :jasmine do
  # Initialize database with test data
  test_db = "comments_test"
  system("echo 'DROP DATABASE IF EXISTS #{test_db};' | mysql")
  system("echo 'CREATE DATABASE #{test_db};' | mysql")
  system("mysql #{test_db} < comments/sql/schema.sql")
  system("mysql #{test_db} < comments/sql/test_data.sql")
  system("mysql #{test_db} < comments/sql/update_votes.sql")

  # run jasmine tests against that database
  system("node comments/node_modules/jasmine-node/lib/jasmine-node/cli.js comments/spec/")
+25 −0
Original line number Diff line number Diff line
DROP TABLE IF EXISTS readings;
DROP TABLE IF EXISTS subscriptions;
DROP TABLE IF EXISTS updates;
DROP TABLE IF EXISTS votes;
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS targets;
DROP TABLE IF EXISTS users;

CREATE TABLE users (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
@@ -104,3 +112,20 @@ CREATE OR REPLACE VIEW full_comments AS SELECT
FROM comments AS c
    LEFT JOIN users ON c.user_id = users.id
    LEFT JOIN targets ON c.target_id = targets.id;


-- set up triggers to recalculate the votes column automatically

DROP TRIGGER IF EXISTS on_vote_added;
CREATE TRIGGER on_vote_added AFTER INSERT ON votes
FOR EACH ROW
    UPDATE comments
    SET vote = (SELECT SUM(value) FROM votes WHERE votes.comment_id = comments.id)
    WHERE id = NEW.comment_id;

DROP TRIGGER IF EXISTS on_vote_deleted;
CREATE TRIGGER on_vote_deleted AFTER DELETE ON votes
FOR EACH ROW
    UPDATE comments
    SET vote = (SELECT SUM(value) FROM votes WHERE votes.comment_id = comments.id)
    WHERE id = OLD.comment_id;

comments/sql/update_votes.sql

deleted100644 → 0
+0 −19
Original line number Diff line number Diff line
-- calculates the votes column for all comments

UPDATE comments
SET vote = (SELECT SUM(value) FROM votes WHERE votes.comment_id = comments.id);


-- set up a trigger to recalculate the votes column automatically

CREATE TRIGGER on_vote_added AFTER INSERT ON votes
FOR EACH ROW
    UPDATE comments
    SET vote = (SELECT SUM(value) FROM votes WHERE votes.comment_id = comments.id)
    WHERE id = NEW.comment_id;

CREATE TRIGGER on_vote_deleted AFTER DELETE ON votes
FOR EACH ROW
    UPDATE comments
    SET vote = (SELECT SUM(value) FROM votes WHERE votes.comment_id = comments.id)
    WHERE id = OLD.comment_id;