Add database logic to clear out requests that have 0 votes
Also, clean up some db constraints. Fixes #29master
parent
8483705ae4
commit
632dc73ee9
|
@ -1,5 +1,5 @@
|
|||
CREATE TABLE requests (
|
||||
url varchar NOT NULL UNIQUE,
|
||||
url varchar NOT NULL,
|
||||
requester int NOT NULL,
|
||||
state varchar NOT NULL DEFAULT 'Requested',
|
||||
reqTimestamp timestamptz NOT NULL DEFAULT NOW(),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
CREATE TABLE votes (
|
||||
requestUrl varchar,
|
||||
userId int,
|
||||
requestUrl varchar NOT NULL,
|
||||
userId int NOT NULL,
|
||||
PRIMARY KEY (requestUrl,userId),
|
||||
FOREIGN KEY (requestUrl) REFERENCES requests(url) 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();
|
||||
$$;
|
||||
|
||||
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)
|
||||
LANGUAGE SQL
|
||||
AS $$
|
||||
|
@ -28,6 +35,7 @@ CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
|
|||
AS $$
|
||||
DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
|
||||
CALL update_scores();
|
||||
CALL clear_zero_votes();
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE PROCEDURE update_request_score_modifier(updateurl varchar, scoreDiff int)
|
||||
|
|
|
@ -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 New Issue