Add database logic to clear out requests that have 0 votes

Also, clean up some db constraints.

Fixes #29
master
Dessa Simpson 2021-04-25 20:42:20 -07:00
parent 8483705ae4
commit 632dc73ee9
4 changed files with 35 additions and 3 deletions

View File

@ -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(),

View File

@ -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

View File

@ -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)

24
db/upgrade/v0.6-v0.7.sql Normal file
View 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;