Restructure DB in preparation for follower/subscriber implementation

- Remove user.is{follower,subscriber}, replacing with tables follows and 
subscriptions
- Update vote_score_vw to reflect new structure
- Add db upgrade script
This commit is contained in:
Dessa Simpson 2021-02-22 23:54:47 -07:00
parent 60defb7ea6
commit dbe7e4c52d
6 changed files with 75 additions and 10 deletions

49
db/upgrade/v0.4-0.5.sql Normal file
View file

@ -0,0 +1,49 @@
BEGIN;
--UPDATE version SET minor = 5;
CREATE OR REPLACE PROCEDURE update_vote_points(normaluser int, follower int, subscriber int)
LANGUAGE SQL
AS $$
UPDATE config SET normaluservotepoints = normaluser,
followervotepoints = follower, subscribervotepoints = subscriber;
CALL update_scores();
$$;
CREATE TABLE follows (
userid integer,
PRIMARY KEY (userid)
);
CREATE TABLE subscriptions (
userid integer,
PRIMARY KEY (userid)
);
CREATE OR REPLACE VIEW vote_score_vw AS
WITH votepoints AS (SELECT normaluservotepoints, followervotepoints, subscribervotepoints FROM config)
SELECT requests.url AS url,
COUNT(votes.requesturl) AS count,
COALESCE(
SUM(CASE
WHEN follows.userid IS NULL AND subscriptions.userid IS NULL
THEN votepoints.normaluservotepoints
WHEN follows.userid IS NOT NULL AND subscriptions.userid IS NULL
THEN votepoints.followervotepoints
WHEN subscriptions.userid IS NOT NULL
THEN votepoints.subscribervotepoints
END), 0
) AS votescore
FROM requests
LEFT JOIN votes ON votes.requesturl = requests.url
LEFT JOIN users on votes.userid = users.userid
LEFT JOIN bans ON users.userid = bans.userid
LEFT JOIN follows ON users.userid = follows.userid
LEFT JOIN subscriptions ON users.userid = subscriptions.userid
CROSS JOIN votepoints
WHERE bans.userid IS NULL
GROUP BY url;
ALTER TABLE users DROP COLUMN isfollower, DROP COLUMN issubscriber;
COMMIT;