Add database logic to clear out requests that have 0 votes
Also, clean up some db constraints. Fixes #29
This commit is contained in:
parent
8483705ae4
commit
632dc73ee9
4 changed files with 35 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
||||||
CREATE TABLE requests (
|
CREATE TABLE requests (
|
||||||
url varchar NOT NULL UNIQUE,
|
url varchar NOT NULL,
|
||||||
requester int NOT NULL,
|
requester int NOT NULL,
|
||||||
state varchar NOT NULL DEFAULT 'Requested',
|
state varchar NOT NULL DEFAULT 'Requested',
|
||||||
reqTimestamp timestamptz NOT NULL DEFAULT NOW(),
|
reqTimestamp timestamptz NOT NULL DEFAULT NOW(),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
CREATE TABLE votes (
|
CREATE TABLE votes (
|
||||||
requestUrl varchar,
|
requestUrl varchar NOT NULL,
|
||||||
userId int,
|
userId int NOT NULL,
|
||||||
PRIMARY KEY (requestUrl,userId),
|
PRIMARY KEY (requestUrl,userId),
|
||||||
FOREIGN KEY (requestUrl) REFERENCES requests(url) ON DELETE CASCADE,
|
FOREIGN KEY (requestUrl) REFERENCES requests(url) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (userId) REFERENCES users(userId) ON DELETE CASCADE
|
FOREIGN KEY (userId) REFERENCES users(userId) ON DELETE CASCADE
|
||||||
|
|
|
@ -16,6 +16,13 @@ CREATE OR REPLACE PROCEDURE add_request(url varchar,requester int)
|
||||||
CALL update_scores();
|
CALL update_scores();
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
CREATE OR REPLACE PROCEDURE clear_zero_votes()
|
||||||
|
LANGUAGE SQL
|
||||||
|
AS $$
|
||||||
|
DELETE FROM requests WHERE NOT EXISTS
|
||||||
|
(SELECT FROM votes WHERE requests.url = votes.requesturl);
|
||||||
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE PROCEDURE add_vote(url varchar,voteuser int)
|
CREATE OR REPLACE PROCEDURE add_vote(url varchar,voteuser int)
|
||||||
LANGUAGE SQL
|
LANGUAGE SQL
|
||||||
AS $$
|
AS $$
|
||||||
|
@ -28,6 +35,7 @@ CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
|
||||||
AS $$
|
AS $$
|
||||||
DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
|
DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
|
||||||
CALL update_scores();
|
CALL update_scores();
|
||||||
|
CALL clear_zero_votes();
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
CREATE OR REPLACE PROCEDURE update_request_score_modifier(updateurl varchar, scoreDiff int)
|
CREATE OR REPLACE PROCEDURE update_request_score_modifier(updateurl varchar, scoreDiff int)
|
||||||
|
|
24
db/upgrade/v0.6-v0.7.sql
Normal file
24
db/upgrade/v0.6-v0.7.sql
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
UPDATE version SET minor = 7;
|
||||||
|
|
||||||
|
ALTER TABLE votes
|
||||||
|
ALTER COLUMN requestUrl SET NOT NULL
|
||||||
|
ALTER COLUMN userId SET NOT NULL;
|
||||||
|
|
||||||
|
CREATE OR REPLACE PROCEDURE clear_zero_votes()
|
||||||
|
LANGUAGE SQL
|
||||||
|
AS $$
|
||||||
|
DELETE FROM requests WHERE NOT EXISTS
|
||||||
|
(SELECT FROM votes WHERE requests.url = votes.requesturl);
|
||||||
|
$$;
|
||||||
|
|
||||||
|
CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
|
||||||
|
LANGUAGE SQL
|
||||||
|
AS $$
|
||||||
|
DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
|
||||||
|
CALL update_scores();
|
||||||
|
CALL clear_zero_votes();
|
||||||
|
$$;
|
||||||
|
|
||||||
|
COMMIT;
|
Loading…
Reference in a new issue