mirror of https://github.com/LemmyNet/lemmy
Adding SQL format checking via `pg_format` / pgFormatter (#3740)
* SQL format checking, 1. * SQL format checking, 2. * SQL format checking, 3. * SQL format checking, 4. * SQL format checking, 5. * Running pg_format * Getting rid of comment. * Upping pg_format version. * Using git ls-files for sql format check. * Fixing sql lints. * Addressing PR comments.pull/3799/head^2
parent
b4380cb548
commit
be1389420b
@ -1,2 +1,4 @@
|
||||
drop table user_ban;
|
||||
drop table user_;
|
||||
DROP TABLE user_ban;
|
||||
|
||||
DROP TABLE user_;
|
||||
|
||||
|
@ -1,23 +1,25 @@
|
||||
create table user_ (
|
||||
id serial primary key,
|
||||
name varchar(20) not null,
|
||||
fedi_name varchar(40) not null,
|
||||
preferred_username varchar(20),
|
||||
password_encrypted text not null,
|
||||
email text unique,
|
||||
icon bytea,
|
||||
admin boolean default false not null,
|
||||
banned boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp,
|
||||
unique(name, fedi_name)
|
||||
CREATE TABLE user_ (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(20) NOT NULL,
|
||||
fedi_name varchar(40) NOT NULL,
|
||||
preferred_username varchar(20),
|
||||
password_encrypted text NOT NULL,
|
||||
email text UNIQUE,
|
||||
icon bytea,
|
||||
admin boolean DEFAULT FALSE NOT NULL,
|
||||
banned boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp,
|
||||
UNIQUE (name, fedi_name)
|
||||
);
|
||||
|
||||
create table user_ban (
|
||||
id serial primary key,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique (user_id)
|
||||
CREATE TABLE user_ban (
|
||||
id serial PRIMARY KEY,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (user_id)
|
||||
);
|
||||
|
||||
insert into user_ (name, fedi_name, password_encrypted) values ('admin', 'TBD', 'TBD');
|
||||
INSERT INTO user_ (name, fedi_name, password_encrypted)
|
||||
VALUES ('admin', 'TBD', 'TBD');
|
||||
|
||||
|
@ -1,6 +1,14 @@
|
||||
drop table site;
|
||||
drop table community_user_ban;;
|
||||
drop table community_moderator;
|
||||
drop table community_follower;
|
||||
drop table community;
|
||||
drop table category;
|
||||
DROP TABLE site;
|
||||
|
||||
DROP TABLE community_user_ban;
|
||||
|
||||
;
|
||||
|
||||
DROP TABLE community_moderator;
|
||||
|
||||
DROP TABLE community_follower;
|
||||
|
||||
DROP TABLE community;
|
||||
|
||||
DROP TABLE category;
|
||||
|
||||
|
@ -1,79 +1,81 @@
|
||||
create table category (
|
||||
id serial primary key,
|
||||
name varchar(100) not null unique
|
||||
CREATE TABLE category (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(100) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
insert into category (name) values
|
||||
('Discussion'),
|
||||
('Humor/Memes'),
|
||||
('Gaming'),
|
||||
('Movies'),
|
||||
('TV'),
|
||||
('Music'),
|
||||
('Literature'),
|
||||
('Comics'),
|
||||
('Photography'),
|
||||
('Art'),
|
||||
('Learning'),
|
||||
('DIY'),
|
||||
('Lifestyle'),
|
||||
('News'),
|
||||
('Politics'),
|
||||
('Society'),
|
||||
('Gender/Identity/Sexuality'),
|
||||
('Race/Colonisation'),
|
||||
('Religion'),
|
||||
('Science/Technology'),
|
||||
('Programming/Software'),
|
||||
('Health/Sports/Fitness'),
|
||||
('Porn'),
|
||||
('Places'),
|
||||
('Meta'),
|
||||
('Other');
|
||||
INSERT INTO category (name)
|
||||
VALUES ('Discussion'),
|
||||
('Humor/Memes'),
|
||||
('Gaming'),
|
||||
('Movies'),
|
||||
('TV'),
|
||||
('Music'),
|
||||
('Literature'),
|
||||
('Comics'),
|
||||
('Photography'),
|
||||
('Art'),
|
||||
('Learning'),
|
||||
('DIY'),
|
||||
('Lifestyle'),
|
||||
('News'),
|
||||
('Politics'),
|
||||
('Society'),
|
||||
('Gender/Identity/Sexuality'),
|
||||
('Race/Colonisation'),
|
||||
('Religion'),
|
||||
('Science/Technology'),
|
||||
('Programming/Software'),
|
||||
('Health/Sports/Fitness'),
|
||||
('Porn'),
|
||||
('Places'),
|
||||
('Meta'),
|
||||
('Other');
|
||||
|
||||
create table community (
|
||||
id serial primary key,
|
||||
name varchar(20) not null unique,
|
||||
title varchar(100) not null,
|
||||
description text,
|
||||
category_id int references category on update cascade on delete cascade not null,
|
||||
creator_id int references user_ on update cascade on delete cascade not null,
|
||||
removed boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE community (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(20) NOT NULL UNIQUE,
|
||||
title varchar(100) NOT NULL,
|
||||
description text,
|
||||
category_id int REFERENCES category ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
removed boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table community_moderator (
|
||||
id serial primary key,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique (community_id, user_id)
|
||||
CREATE TABLE community_moderator (
|
||||
id serial PRIMARY KEY,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (community_id, user_id)
|
||||
);
|
||||
|
||||
create table community_follower (
|
||||
id serial primary key,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique (community_id, user_id)
|
||||
CREATE TABLE community_follower (
|
||||
id serial PRIMARY KEY,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (community_id, user_id)
|
||||
);
|
||||
|
||||
create table community_user_ban (
|
||||
id serial primary key,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique (community_id, user_id)
|
||||
CREATE TABLE community_user_ban (
|
||||
id serial PRIMARY KEY,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (community_id, user_id)
|
||||
);
|
||||
|
||||
insert into community (name, title, category_id, creator_id) values ('main', 'The Default Community', 1, 1);
|
||||
INSERT INTO community (name, title, category_id, creator_id)
|
||||
VALUES ('main', 'The Default Community', 1, 1);
|
||||
|
||||
create table site (
|
||||
id serial primary key,
|
||||
name varchar(20) not null unique,
|
||||
description text,
|
||||
creator_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE site (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(20) NOT NULL UNIQUE,
|
||||
description text,
|
||||
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
|
@ -1,4 +1,8 @@
|
||||
drop table post_read;
|
||||
drop table post_saved;
|
||||
drop table post_like;
|
||||
drop table post;
|
||||
DROP TABLE post_read;
|
||||
|
||||
DROP TABLE post_saved;
|
||||
|
||||
DROP TABLE post_like;
|
||||
|
||||
DROP TABLE post;
|
||||
|
||||
|
@ -1,37 +1,38 @@
|
||||
create table post (
|
||||
id serial primary key,
|
||||
name varchar(100) not null,
|
||||
url text, -- These are both optional, a post can just have a title
|
||||
body text,
|
||||
creator_id int references user_ on update cascade on delete cascade not null,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
removed boolean default false not null,
|
||||
locked boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE post (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(100) NOT NULL,
|
||||
url text, -- These are both optional, a post can just have a title
|
||||
body text,
|
||||
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
removed boolean DEFAULT FALSE NOT NULL,
|
||||
locked boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table post_like (
|
||||
id serial primary key,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
|
||||
published timestamp not null default now(),
|
||||
unique(post_id, user_id)
|
||||
CREATE TABLE post_like (
|
||||
id serial PRIMARY KEY,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (post_id, user_id)
|
||||
);
|
||||
|
||||
create table post_saved (
|
||||
id serial primary key,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique(post_id, user_id)
|
||||
CREATE TABLE post_saved (
|
||||
id serial PRIMARY KEY,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (post_id, user_id)
|
||||
);
|
||||
|
||||
create table post_read (
|
||||
id serial primary key,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique(post_id, user_id)
|
||||
CREATE TABLE post_read (
|
||||
id serial PRIMARY KEY,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (post_id, user_id)
|
||||
);
|
||||
|
||||
|
@ -1,3 +1,6 @@
|
||||
drop table comment_saved;
|
||||
drop table comment_like;
|
||||
drop table comment;
|
||||
DROP TABLE comment_saved;
|
||||
|
||||
DROP TABLE comment_like;
|
||||
|
||||
DROP TABLE comment;
|
||||
|
||||
|
@ -1,29 +1,30 @@
|
||||
create table comment (
|
||||
id serial primary key,
|
||||
creator_id int references user_ on update cascade on delete cascade not null,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
parent_id int references comment on update cascade on delete cascade,
|
||||
content text not null,
|
||||
removed boolean default false not null,
|
||||
read boolean default false not null,
|
||||
published timestamp not null default now(),
|
||||
updated timestamp
|
||||
CREATE TABLE comment (
|
||||
id serial PRIMARY KEY,
|
||||
creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
parent_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
content text NOT NULL,
|
||||
removed boolean DEFAULT FALSE NOT NULL,
|
||||
read boolean DEFAULT FALSE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table comment_like (
|
||||
id serial primary key,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
comment_id int references comment on update cascade on delete cascade not null,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
|
||||
published timestamp not null default now(),
|
||||
unique(comment_id, user_id)
|
||||
CREATE TABLE comment_like (
|
||||
id serial PRIMARY KEY,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (comment_id, user_id)
|
||||
);
|
||||
|
||||
create table comment_saved (
|
||||
id serial primary key,
|
||||
comment_id int references comment on update cascade on delete cascade not null,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
published timestamp not null default now(),
|
||||
unique(comment_id, user_id)
|
||||
CREATE TABLE comment_saved (
|
||||
id serial PRIMARY KEY,
|
||||
comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now(),
|
||||
UNIQUE (comment_id, user_id)
|
||||
);
|
||||
|
||||
|
@ -1,2 +1,4 @@
|
||||
drop view post_view;
|
||||
drop function hot_rank;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP FUNCTION hot_rank;
|
||||
|
||||
|
@ -1,5 +1,10 @@
|
||||
drop view community_view;
|
||||
drop view community_moderator_view;
|
||||
drop view community_follower_view;
|
||||
drop view community_user_ban_view;
|
||||
drop view site_view;
|
||||
DROP VIEW community_view;
|
||||
|
||||
DROP VIEW community_moderator_view;
|
||||
|
||||
DROP VIEW community_follower_view;
|
||||
|
||||
DROP VIEW community_user_ban_view;
|
||||
|
||||
DROP VIEW site_view;
|
||||
|
||||
|
@ -1,2 +1,4 @@
|
||||
drop view reply_view;
|
||||
drop view comment_view;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
DROP VIEW comment_view;
|
||||
|
||||
|
@ -1,60 +1,114 @@
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
c.*,
|
||||
(select community_id from post p where p.id = c.post_id),
|
||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
||||
coalesce(sum(cl.score), 0) as score,
|
||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
||||
from comment c
|
||||
left join comment_like cl on c.id = cl.comment_id
|
||||
group by c.id
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
c.*,
|
||||
(
|
||||
SELECT
|
||||
community_id
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.id = c.post_id),
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb,
|
||||
post p
|
||||
WHERE
|
||||
c.creator_id = cb.user_id
|
||||
AND p.id = c.post_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
c.creator_id = user_.id) AS creator_name,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
@ -1,8 +1,16 @@
|
||||
drop table mod_remove_post;
|
||||
drop table mod_lock_post;
|
||||
drop table mod_remove_comment;
|
||||
drop table mod_remove_community;
|
||||
drop table mod_ban;
|
||||
drop table mod_ban_from_community;
|
||||
drop table mod_add;
|
||||
drop table mod_add_community;
|
||||
DROP TABLE mod_remove_post;
|
||||
|
||||
DROP TABLE mod_lock_post;
|
||||
|
||||
DROP TABLE mod_remove_comment;
|
||||
|
||||
DROP TABLE mod_remove_community;
|
||||
|
||||
DROP TABLE mod_ban;
|
||||
|
||||
DROP TABLE mod_ban_from_community;
|
||||
|
||||
DROP TABLE mod_add;
|
||||
|
||||
DROP TABLE mod_add_community;
|
||||
|
||||
|
@ -1,76 +1,76 @@
|
||||
create table mod_remove_post (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
reason text,
|
||||
removed boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_remove_post (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
reason text,
|
||||
removed boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_lock_post (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
post_id int references post on update cascade on delete cascade not null,
|
||||
locked boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_lock_post (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
locked boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_remove_comment (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
comment_id int references comment on update cascade on delete cascade not null,
|
||||
reason text,
|
||||
removed boolean default true,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_remove_comment (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
reason text,
|
||||
removed boolean DEFAULT TRUE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_remove_community (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
reason text,
|
||||
removed boolean default true,
|
||||
expires timestamp,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_remove_community (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
reason text,
|
||||
removed boolean DEFAULT TRUE,
|
||||
expires timestamp,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- TODO make sure you can't ban other mods
|
||||
create table mod_ban_from_community (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
reason text,
|
||||
banned boolean default true,
|
||||
expires timestamp,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_ban_from_community (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
reason text,
|
||||
banned boolean DEFAULT TRUE,
|
||||
expires timestamp,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_ban (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
||||
reason text,
|
||||
banned boolean default true,
|
||||
expires timestamp,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_ban (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
reason text,
|
||||
banned boolean DEFAULT TRUE,
|
||||
expires timestamp,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
create table mod_add_community (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
||||
community_id int references community on update cascade on delete cascade not null,
|
||||
removed boolean default false,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_add_community (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
removed boolean DEFAULT FALSE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- When removed is false that means kicked
|
||||
create table mod_add (
|
||||
id serial primary key,
|
||||
mod_user_id int references user_ on update cascade on delete cascade not null,
|
||||
other_user_id int references user_ on update cascade on delete cascade not null,
|
||||
removed boolean default false,
|
||||
when_ timestamp not null default now()
|
||||
CREATE TABLE mod_add (
|
||||
id serial PRIMARY KEY,
|
||||
mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
removed boolean DEFAULT FALSE,
|
||||
when_ timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
|
@ -1 +1,2 @@
|
||||
drop view user_view;
|
||||
DROP VIEW user_view;
|
||||
|
||||
|
@ -1,12 +1,43 @@
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
@ -1,8 +1,16 @@
|
||||
drop view mod_remove_post_view;
|
||||
drop view mod_lock_post_view;
|
||||
drop view mod_remove_comment_view;
|
||||
drop view mod_remove_community_view;
|
||||
drop view mod_ban_from_community_view;
|
||||
drop view mod_ban_view;
|
||||
drop view mod_add_community_view;
|
||||
drop view mod_add_view;
|
||||
DROP VIEW mod_remove_post_view;
|
||||
|
||||
DROP VIEW mod_lock_post_view;
|
||||
|
||||
DROP VIEW mod_remove_comment_view;
|
||||
|
||||
DROP VIEW mod_remove_community_view;
|
||||
|
||||
DROP VIEW mod_ban_from_community_view;
|
||||
|
||||
DROP VIEW mod_ban_view;
|
||||
|
||||
DROP VIEW mod_add_community_view;
|
||||
|
||||
DROP VIEW mod_add_view;
|
||||
|
||||
|
@ -1,59 +1,266 @@
|
||||
create view mod_remove_post_view as
|
||||
select mrp.*,
|
||||
(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from post p where mrp.post_id = p.id) as post_name,
|
||||
(select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
|
||||
(select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
|
||||
from mod_remove_post mrp;
|
||||
|
||||
create view mod_lock_post_view as
|
||||
select mlp.*,
|
||||
(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from post p where mlp.post_id = p.id) as post_name,
|
||||
(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
|
||||
(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
|
||||
from mod_lock_post mlp;
|
||||
|
||||
create view mod_remove_comment_view as
|
||||
select mrc.*,
|
||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
||||
(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
|
||||
(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
|
||||
(select content from comment c where mrc.comment_id = c.id) as comment_content,
|
||||
(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
|
||||
(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
|
||||
(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id,
|
||||
(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
|
||||
from mod_remove_comment mrc;
|
||||
|
||||
create view mod_remove_community_view as
|
||||
select mrc.*,
|
||||
(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
|
||||
(select c.name from community c where mrc.community_id = c.id) as community_name
|
||||
from mod_remove_community mrc;
|
||||
|
||||
create view mod_ban_from_community_view as
|
||||
select mb.*,
|
||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name,
|
||||
(select name from community c where mb.community_id = c.id) as community_name
|
||||
from mod_ban_from_community mb;
|
||||
|
||||
create view mod_ban_view as
|
||||
select mb.*,
|
||||
(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where mb.other_user_id = u.id) as other_user_name
|
||||
from mod_ban mb;
|
||||
|
||||
create view mod_add_community_view as
|
||||
select ma.*,
|
||||
(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where ma.other_user_id = u.id) as other_user_name,
|
||||
(select name from community c where ma.community_id = c.id) as community_name
|
||||
from mod_add_community ma;
|
||||
|
||||
create view mod_add_view as
|
||||
select ma.*,
|
||||
(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
|
||||
(select name from user_ u where ma.other_user_id = u.id) as other_user_name
|
||||
from mod_add ma;
|
||||
CREATE VIEW mod_remove_post_view AS
|
||||
SELECT
|
||||
mrp.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrp.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
mrp.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
post p,
|
||||
community c
|
||||
WHERE
|
||||
mrp.post_id = p.id
|
||||
AND p.community_id = c.id) AS community_id,
|
||||
(
|
||||
SELECT
|
||||
c.name
|
||||
FROM
|
||||
post p,
|
||||
community c
|
||||
WHERE
|
||||
mrp.post_id = p.id
|
||||
AND p.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_remove_post mrp;
|
||||
|
||||
CREATE VIEW mod_lock_post_view AS
|
||||
SELECT
|
||||
mlp.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mlp.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
mlp.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
post p,
|
||||
community c
|
||||
WHERE
|
||||
mlp.post_id = p.id
|
||||
AND p.community_id = c.id) AS community_id,
|
||||
(
|
||||
SELECT
|
||||
c.name
|
||||
FROM
|
||||
post p,
|
||||
community c
|
||||
WHERE
|
||||
mlp.post_id = p.id
|
||||
AND p.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_lock_post mlp;
|
||||
|
||||
CREATE VIEW mod_remove_comment_view AS
|
||||
SELECT
|
||||
mrc.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
c.id
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_user_id,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND u.id = c.creator_id) AS comment_user_name,
|
||||
(
|
||||
SELECT
|
||||
content
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id) AS comment_content,
|
||||
(
|
||||
SELECT
|
||||
p.id
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_id,
|
||||
(
|
||||
SELECT
|
||||
p.name
|
||||
FROM
|
||||
post p,
|
||||
comment c
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id) AS post_name,
|
||||
(
|
||||
SELECT
|
||||
co.id
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_id,
|
||||
(
|
||||
SELECT
|
||||
co.name
|
||||
FROM
|
||||
comment c,
|
||||
post p,
|
||||
community co
|
||||
WHERE
|
||||
mrc.comment_id = c.id
|
||||
AND c.post_id = p.id
|
||||
AND p.community_id = co.id) AS community_name
|
||||
FROM
|
||||
mod_remove_comment mrc;
|
||||
|
||||
CREATE VIEW mod_remove_community_view AS
|
||||
SELECT
|
||||
mrc.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mrc.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
c.name
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
mrc.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_remove_community mrc;
|
||||
|
||||
CREATE VIEW mod_ban_from_community_view AS
|
||||
SELECT
|
||||
mb.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.other_user_id = u.id) AS other_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
mb.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_ban_from_community mb;
|
||||
|
||||
CREATE VIEW mod_ban_view AS
|
||||
SELECT
|
||||
mb.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
mb.other_user_id = u.id) AS other_user_name
|
||||
FROM
|
||||
mod_ban mb;
|
||||
|
||||
CREATE VIEW mod_add_community_view AS
|
||||
SELECT
|
||||
ma.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.other_user_id = u.id) AS other_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
ma.community_id = c.id) AS community_name
|
||||
FROM
|
||||
mod_add_community ma;
|
||||
|
||||
CREATE VIEW mod_add_view AS
|
||||
SELECT
|
||||
ma.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.mod_user_id = u.id) AS mod_user_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
ma.other_user_id = u.id) AS other_user_name
|
||||
FROM
|
||||
mod_add ma;
|
||||
|
||||
|
@ -1,137 +1,293 @@
|
||||
drop view reply_view;
|
||||
drop view comment_view;
|
||||
drop view community_view;
|
||||
drop view post_view;
|
||||
alter table community drop column deleted;
|
||||
alter table post drop column deleted;
|
||||
alter table comment drop column deleted;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
||||
from community c
|
||||
)
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
DROP VIEW comment_view;
|
||||
|
||||
union all
|
||||
DROP VIEW community_view;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
DROP VIEW post_view;
|
||||
|
||||
create or replace view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id
|
||||
)
|
||||
ALTER TABLE community
|
||||
DROP COLUMN deleted;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
ALTER TABLE post
|
||||
DROP COLUMN deleted;
|
||||
|
||||
union all
|
||||
ALTER TABLE comment
|
||||
DROP COLUMN deleted;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
c.*,
|
||||
(select community_id from post p where p.id = c.post_id),
|
||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
||||
coalesce(sum(cl.score), 0) as score,
|
||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
||||
from comment c
|
||||
left join comment_like cl on c.id = cl.comment_id
|
||||
group by c.id
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
|
||||
union all
|
||||
CREATE OR REPLACE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
c.*,
|
||||
(
|
||||
SELECT
|
||||
community_id
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.id = c.post_id),
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb,
|
||||
post p
|
||||
WHERE
|
||||
c.creator_id = cb.user_id
|
||||
AND p.id = c.post_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
c.creator_id = user_.id) AS creator_name,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
@ -1,141 +1,301 @@
|
||||
alter table community add column deleted boolean default false not null;
|
||||
alter table post add column deleted boolean default false not null;
|
||||
alter table comment add column deleted boolean default false not null;
|
||||
ALTER TABLE community
|
||||
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||
|
||||
-- The views
|
||||
drop view community_view;
|
||||
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
|
||||
from community c
|
||||
)
|
||||
ALTER TABLE post
|
||||
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
ALTER TABLE comment
|
||||
ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
|
||||
|
||||
-- The views
|
||||
DROP VIEW community_view;
|
||||
|
||||
drop view post_view;
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
DROP VIEW post_view;
|
||||
|
||||
drop view reply_view;
|
||||
drop view comment_view;
|
||||
create view comment_view as
|
||||
with all_comment as
|
||||
(
|
||||
select
|
||||
c.*,
|
||||
(select community_id from post p where p.id = c.post_id),
|
||||
(select u.banned from user_ u where c.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where c.creator_id = user_.id) as creator_name,
|
||||
coalesce(sum(cl.score), 0) as score,
|
||||
count (case when cl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when cl.score = -1 then 1 else null end) as downvotes
|
||||
from comment c
|
||||
left join comment_like cl on c.id = cl.comment_id
|
||||
group by c.id
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
coalesce(cl.score, 0) as my_vote,
|
||||
(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
|
||||
from user_ u
|
||||
cross join all_comment ac
|
||||
left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
|
||||
DROP VIEW reply_view;
|
||||
|
||||
union all
|
||||
DROP VIEW comment_view;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as saved
|
||||
from all_comment ac
|
||||
;
|
||||
CREATE VIEW comment_view AS
|
||||
with all_comment AS (
|
||||
SELECT
|
||||
c.*,
|
||||
(
|
||||
SELECT
|
||||
community_id
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.id = c.post_id),
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb,
|
||||
post p
|
||||
WHERE
|
||||
c.creator_id = cb.user_id
|
||||
AND p.id = c.post_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
c.creator_id = user_.id) AS creator_name,
|
||||
coalesce(sum(cl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN cl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN cl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes
|
||||
FROM
|
||||
comment c
|
||||
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
||||
GROUP BY
|
||||
c.id
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
coalesce(cl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cs.id::bool
|
||||
FROM
|
||||
comment_saved cs
|
||||
WHERE
|
||||
u.id = cs.user_id
|
||||
AND cs.comment_id = ac.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_comment ac
|
||||
LEFT JOIN comment_like cl ON u.id = cl.user_id
|
||||
AND ac.id = cl.comment_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_comment ac;
|
||||
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
@ -1 +1,3 @@
|
||||
insert into user_ (name, fedi_name, password_encrypted) values ('admin', 'TBD', 'TBD');
|
||||
INSERT INTO user_ (name, fedi_name, password_encrypted)
|
||||
VALUES ('admin', 'TBD', 'TBD');
|
||||
|
||||
|
@ -1 +1,3 @@
|
||||
delete from user_ where name like 'admin';
|
||||
DELETE FROM user_
|
||||
WHERE name LIKE 'admin';
|
||||
|
||||
|
@ -1,80 +1,190 @@
|
||||
drop view community_view;
|
||||
drop view post_view;
|
||||
alter table community drop column nsfw;
|
||||
alter table post drop column nsfw;
|
||||
alter table user_ drop column show_nsfw;
|
||||
DROP VIEW community_view;
|
||||
|
||||
-- the views
|
||||
create view community_view as
|
||||
with all_community as
|
||||
(
|
||||
select *,
|
||||
(select name from user_ u where c.creator_id = u.id) as creator_name,
|
||||
(select name from category ct where c.category_id = ct.id) as category_name,
|
||||
(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
|
||||
(select count(*) from post p where p.community_id = c.id) as number_of_posts,
|
||||
(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
|
||||
hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
|
||||
from community c
|
||||
)
|
||||
DROP VIEW post_view;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
u.id as user_id,
|
||||
(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
|
||||
from user_ u
|
||||
cross join all_community ac
|
||||
ALTER TABLE community
|
||||
DROP COLUMN nsfw;
|
||||
|
||||
union all
|
||||
ALTER TABLE post
|
||||
DROP COLUMN nsfw;
|
||||
|
||||
select
|
||||
ac.*,
|
||||
null as user_id,
|
||||
null as subscribed
|
||||
from all_community ac
|
||||
;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN show_nsfw;
|
||||
|
||||
-- the views
|
||||
CREATE VIEW community_view AS
|
||||
with all_community AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
c.creator_id = u.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
category ct
|
||||
WHERE
|
||||
c.category_id = ct.id) AS category_name,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id) AS number_of_subscribers,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.community_id = c.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment co,
|
||||
post p
|
||||
WHERE
|
||||
c.id = p.community_id
|
||||
AND p.id = co.post_id) AS number_of_comments,
|
||||
hot_rank ((
|
||||
SELECT
|
||||
count(*)
|
||||
FROM community_follower cf
|
||||
WHERE
|
||||
cf.community_id = c.id), c.published) AS hot_rank
|
||||
FROM
|
||||
community c
|
||||
)
|
||||
SELECT
|
||||
ac.*,
|
||||
u.id AS user_id,
|
||||
(
|
||||
SELECT
|
||||
cf.id::boolean
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND ac.id = cf.community_id) AS subscribed
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_community ac
|
||||
UNION ALL
|
||||
SELECT
|
||||
ac.*,
|
||||
NULL AS user_id,
|
||||
NULL AS subscribed
|
||||
FROM
|
||||
all_community ac;
|
||||
|
||||
-- Post view
|
||||
create view post_view as
|
||||
with all_post as
|
||||
(
|
||||
select
|
||||
p.*,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id
|
||||
)
|
||||
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
@ -1 +1,3 @@
|
||||
alter table user_ drop column theme;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN theme;
|
||||
|
||||
|
@ -1 +1,3 @@
|
||||
alter table user_ add column theme varchar(20) default 'darkly' not null;
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN theme varchar(20) DEFAULT 'darkly' NOT NULL;
|
||||
|
||||
|
@ -1,2 +1,4 @@
|
||||
drop view user_mention_view;
|
||||
drop table user_mention;
|
||||
DROP VIEW user_mention_view;
|
||||
|
||||
DROP TABLE user_mention;
|
||||
|
||||
|
@ -1,2 +1,6 @@
|
||||
alter table user_ drop column default_sort_type;
|
||||
alter table user_ drop column default_listing_type;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN default_sort_type;
|
||||
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN default_listing_type;
|
||||
|
||||
|
@ -1,2 +1,6 @@
|
||||
alter table user_ add column default_sort_type smallint default 0 not null;
|
||||
alter table user_ add column default_listing_type smallint default 1 not null;
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN default_sort_type smallint DEFAULT 0 NOT NULL;
|
||||
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN default_listing_type smallint DEFAULT 1 NOT NULL;
|
||||
|
||||
|
@ -1 +1,2 @@
|
||||
drop table password_reset_request;
|
||||
DROP TABLE password_reset_request;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
create table password_reset_request (
|
||||
id serial primary key,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
token_encrypted text not null,
|
||||
published timestamp not null default now()
|
||||
CREATE TABLE password_reset_request (
|
||||
id serial PRIMARY KEY,
|
||||
user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
token_encrypted text NOT NULL,
|
||||
published timestamp NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
|
@ -1 +1,3 @@
|
||||
alter table user_ drop column lang;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN lang;
|
||||
|
||||
|
@ -1 +1,3 @@
|
||||
alter table user_ add column lang varchar(20) default 'browser' not null;
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN lang varchar(20) DEFAULT 'browser' NOT NULL;
|
||||
|
||||
|
@ -1,15 +1,47 @@
|
||||
-- user
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
@ -1,16 +1,48 @@
|
||||
-- user
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
@ -1,20 +1,55 @@
|
||||
-- Drop the columns
|
||||
drop view user_view;
|
||||
alter table user_ drop column show_avatars;
|
||||
alter table user_ drop column send_notifications_to_email;
|
||||
DROP VIEW user_view;
|
||||
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN show_avatars;
|
||||
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN send_notifications_to_email;
|
||||
|
||||
-- Rebuild the view
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
@ -1,22 +1,57 @@
|
||||
-- Add columns
|
||||
alter table user_ add column show_avatars boolean default true not null;
|
||||
alter table user_ add column send_notifications_to_email boolean default false not null;
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN show_avatars boolean DEFAULT TRUE NOT NULL;
|
||||
|
||||
ALTER TABLE user_
|
||||
ADD COLUMN send_notifications_to_email boolean DEFAULT FALSE NOT NULL;
|
||||
|
||||
-- Rebuild the user_view
|
||||
drop view user_view;
|
||||
create view user_view as
|
||||
select id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
DROP VIEW user_view;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
avatar,
|
||||
email,
|
||||
fedi_name,
|
||||
admin,
|
||||
banned,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,34 +1,72 @@
|
||||
-- Drop the triggers
|
||||
drop trigger refresh_private_message on private_message;
|
||||
drop function refresh_private_message();
|
||||
DROP TRIGGER refresh_private_message ON private_message;
|
||||
|
||||
DROP FUNCTION refresh_private_message ();
|
||||
|
||||
-- Drop the view and table
|
||||
drop view private_message_view cascade;
|
||||
drop table private_message;
|
||||
DROP VIEW private_message_view CASCADE;
|
||||
|
||||
DROP TABLE private_message;
|
||||
|
||||
-- Rebuild the old views
|
||||
drop view user_view cascade;
|
||||
create view user_view as
|
||||
select
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
u.published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
|
||||
create materialized view user_mview as select * from user_view;
|
||||
|
||||
create unique index idx_user_mview_id on user_mview (id);
|
||||
DROP VIEW user_view CASCADE;
|
||||
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.fedi_name,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
u.published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
CREATE MATERIALIZED VIEW user_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
user_view;
|
||||
|
||||
CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
|
||||
|
||||
-- Drop the columns
|
||||
alter table user_ drop column matrix_user_id;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN matrix_user_id;
|
||||
|
||||
|
@ -1,25 +1,37 @@
|
||||
-- Drop the materialized / built views
|
||||
drop view reply_view;
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
DROP VIEW reply_view;
|
||||
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_view cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_view cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
@ -1,27 +1,38 @@
|
||||
-- https://github.com/dessalines/lemmy/issues/197
|
||||
drop view reply_view;
|
||||
DROP VIEW reply_view;
|
||||
|
||||
-- Do the reply_view referencing the comment_mview
|
||||
create view reply_view as
|
||||
with closereply as (
|
||||
select
|
||||
c2.id,
|
||||
c2.creator_id as sender_id,
|
||||
c.creator_id as recipient_id
|
||||
from comment c
|
||||
inner join comment c2 on c.id = c2.parent_id
|
||||
where c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
union
|
||||
select
|
||||
c.id,
|
||||
c.creator_id as sender_id,
|
||||
p.creator_id as recipient_id
|
||||
from comment c, post p
|
||||
where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
|
||||
CREATE VIEW reply_view AS
|
||||
with closereply AS (
|
||||
SELECT
|
||||
c2.id,
|
||||
c2.creator_id AS sender_id,
|
||||
c.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c
|
||||
INNER JOIN comment c2 ON c.id = c2.parent_id
|
||||
WHERE
|
||||
c2.creator_id != c.creator_id
|
||||
-- Do union where post is null
|
||||
UNION
|
||||
SELECT
|
||||
c.id,
|
||||
c.creator_id AS sender_id,
|
||||
p.creator_id AS recipient_id
|
||||
FROM
|
||||
comment c,
|
||||
post p
|
||||
WHERE
|
||||
c.post_id = p.id
|
||||
AND c.parent_id IS NULL
|
||||
AND c.creator_id != p.creator_id
|
||||
)
|
||||
select cv.*,
|
||||
closereply.recipient_id
|
||||
from comment_mview cv, closereply
|
||||
where closereply.id = cv.id
|
||||
;
|
||||
SELECT
|
||||
cv.*,
|
||||
closereply.recipient_id
|
||||
FROM
|
||||
comment_mview cv,
|
||||
closereply
|
||||
WHERE
|
||||
closereply.id = cv.id;
|
||||
|
||||
|
@ -1 +1,2 @@
|
||||
drop view user_mention_mview;
|
||||
DROP VIEW user_mention_mview;
|
||||
|
||||
|
@ -1,2 +1,4 @@
|
||||
drop index idx_user_name_lower;
|
||||
drop index idx_user_email_lower;
|
||||
DROP INDEX idx_user_name_lower;
|
||||
|
||||
DROP INDEX idx_user_email_lower;
|
||||
|
||||
|
@ -1,88 +1,206 @@
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
group by p.id;
|
||||
DROP VIEW post_mview;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
create view post_view as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
-- regen post view
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
GROUP BY
|
||||
p.id;
|
||||
|
||||
union all
|
||||
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
post_aggregates_view;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
@ -1,106 +1,227 @@
|
||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0),
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
left join (
|
||||
select post_id,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by 1
|
||||
) c on p.id = c.post_id
|
||||
group by p.id, c.recent_comment_time;
|
||||
DROP VIEW post_mview;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
create view post_view as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
-- regen post view
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
1) c ON p.id = c.post_id
|
||||
GROUP BY
|
||||
p.id,
|
||||
c.recent_comment_time;
|
||||
|
||||
union all
|
||||
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
post_aggregates_view;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
@ -1,112 +1,240 @@
|
||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
DROP VIEW post_view;
|
||||
|
||||
DROP VIEW post_mview;
|
||||
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
-- Drop the columns
|
||||
alter table post drop column embed_title;
|
||||
alter table post drop column embed_description;
|
||||
alter table post drop column embed_html;
|
||||
alter table post drop column thumbnail_url;
|
||||
ALTER TABLE post
|
||||
DROP COLUMN embed_title;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0),
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
left join (
|
||||
select post_id,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by 1
|
||||
) c on p.id = c.post_id
|
||||
group by p.id, c.recent_comment_time;
|
||||
ALTER TABLE post
|
||||
DROP COLUMN embed_description;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
ALTER TABLE post
|
||||
DROP COLUMN embed_html;
|
||||
|
||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
||||
ALTER TABLE post
|
||||
DROP COLUMN thumbnail_url;
|
||||
|
||||
create view post_view as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
-- regen post view
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
1) c ON p.id = c.post_id
|
||||
GROUP BY
|
||||
p.id,
|
||||
c.recent_comment_time;
|
||||
|
||||
union all
|
||||
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
post_aggregates_view;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
union all
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
@ -1,115 +1,241 @@
|
||||
-- Add the columns
|
||||
alter table post add column embed_title text;
|
||||
alter table post add column embed_description text;
|
||||
alter table post add column embed_html text;
|
||||
alter table post add column thumbnail_url text;
|
||||
ALTER TABLE post
|
||||
ADD COLUMN embed_title text;
|
||||
|
||||
-- Regenerate the views
|
||||
ALTER TABLE post
|
||||
ADD COLUMN embed_description text;
|
||||
|
||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
drop view post_view;
|
||||
drop view post_mview;
|
||||
drop materialized view post_aggregates_mview;
|
||||
drop view post_aggregates_view;
|
||||
ALTER TABLE post
|
||||
ADD COLUMN embed_html text;
|
||||
|
||||
-- regen post view
|
||||
create view post_aggregates_view as
|
||||
select
|
||||
p.*,
|
||||
(select u.banned from user_ u where p.creator_id = u.id) as banned,
|
||||
(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
|
||||
(select name from user_ where p.creator_id = user_.id) as creator_name,
|
||||
(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
|
||||
(select name from community where p.community_id = community.id) as community_name,
|
||||
(select removed from community c where p.community_id = c.id) as community_removed,
|
||||
(select deleted from community c where p.community_id = c.id) as community_deleted,
|
||||
(select nsfw from community c where p.community_id = c.id) as community_nsfw,
|
||||
(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
|
||||
coalesce(sum(pl.score), 0) as score,
|
||||
count (case when pl.score = 1 then 1 else null end) as upvotes,
|
||||
count (case when pl.score = -1 then 1 else null end) as downvotes,
|
||||
hot_rank(coalesce(sum(pl.score) , 0),
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
)
|
||||
) as hot_rank,
|
||||
(
|
||||
case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
|
||||
else greatest(c.recent_comment_time, p.published)
|
||||
end
|
||||
) as newest_activity_time
|
||||
from post p
|
||||
left join post_like pl on p.id = pl.post_id
|
||||
left join (
|
||||
select post_id,
|
||||
max(published) as recent_comment_time
|
||||
from comment
|
||||
group by 1
|
||||
) c on p.id = c.post_id
|
||||
group by p.id, c.recent_comment_time;
|
||||
ALTER TABLE post
|
||||
ADD COLUMN thumbnail_url text;
|
||||
|
||||
create materialized view post_aggregates_mview as select * from post_aggregates_view;
|
||||
-- Regenerate the views
|
||||
-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
|
||||
DROP VIEW post_view;
|
||||
|
||||
create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
|
||||
DROP VIEW post_mview;
|
||||
|
||||
create view post_view as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_view pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
DROP MATERIALIZED VIEW post_aggregates_mview;
|
||||
|
||||
union all
|
||||
DROP VIEW post_aggregates_view;
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
-- regen post view
|
||||
CREATE VIEW post_aggregates_view AS
|
||||
SELECT
|
||||
p.*,
|
||||
(
|
||||
SELECT
|
||||
u.banned
|
||||
FROM
|
||||
user_ u
|
||||
WHERE
|
||||
p.creator_id = u.id) AS banned,
|
||||
(
|
||||
SELECT
|
||||
cb.id::bool
|
||||
FROM
|
||||
community_user_ban cb
|
||||
WHERE
|
||||
p.creator_id = cb.user_id
|
||||
AND p.community_id = cb.community_id) AS banned_from_community,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_name,
|
||||
(
|
||||
SELECT
|
||||
avatar
|
||||
FROM
|
||||
user_
|
||||
WHERE
|
||||
p.creator_id = user_.id) AS creator_avatar,
|
||||
(
|
||||
SELECT
|
||||
name
|
||||
FROM
|
||||
community
|
||||
WHERE
|
||||
p.community_id = community.id) AS community_name,
|
||||
(
|
||||
SELECT
|
||||
removed
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_removed,
|
||||
(
|
||||
SELECT
|
||||
deleted
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_deleted,
|
||||
(
|
||||
SELECT
|
||||
nsfw
|
||||
FROM
|
||||
community c
|
||||
WHERE
|
||||
p.community_id = c.id) AS community_nsfw,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment
|
||||
WHERE
|
||||
comment.post_id = p.id) AS number_of_comments,
|
||||
coalesce(sum(pl.score), 0) AS score,
|
||||
count(
|
||||
CASE WHEN pl.score = 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS upvotes,
|
||||
count(
|
||||
CASE WHEN pl.score = - 1 THEN
|
||||
1
|
||||
ELSE
|
||||
NULL
|
||||
END) AS downvotes,
|
||||
hot_rank (coalesce(sum(pl.score), 0), (
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END)) AS hot_rank,
|
||||
(
|
||||
CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
|
||||
p.published -- Prevents necro-bumps
|
||||
ELSE
|
||||
greatest (c.recent_comment_time, p.published)
|
||||
END) AS newest_activity_time
|
||||
FROM
|
||||
post p
|
||||
LEFT JOIN post_like pl ON p.id = pl.post_id
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
max(published) AS recent_comment_time
|
||||
FROM
|
||||
comment
|
||||
GROUP BY
|
||||
1) c ON p.id = c.post_id
|
||||
GROUP BY
|
||||
p.id,
|
||||
c.recent_comment_time;
|
||||
|
||||
create view post_mview as
|
||||
with all_post as (
|
||||
select
|
||||
pa.*
|
||||
from post_aggregates_mview pa
|
||||
)
|
||||
select
|
||||
ap.*,
|
||||
u.id as user_id,
|
||||
coalesce(pl.score, 0) as my_vote,
|
||||
(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
|
||||
(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
|
||||
(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
|
||||
from user_ u
|
||||
cross join all_post ap
|
||||
left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
|
||||
CREATE MATERIALIZED VIEW post_aggregates_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
post_aggregates_view;
|
||||
|
||||
union all
|
||||
CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
|
||||
|
||||
select
|
||||
ap.*,
|
||||
null as user_id,
|
||||
null as my_vote,
|
||||
null as subscribed,
|
||||
null as read,
|
||||
null as saved
|
||||
from all_post ap
|
||||
;
|
||||
CREATE VIEW post_view AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_view pa
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
CREATE VIEW post_mview AS
|
||||
with all_post AS (
|
||||
SELECT
|
||||
pa.*
|
||||
FROM
|
||||
post_aggregates_mview pa
|
||||
)
|
||||
SELECT
|
||||
ap.*,
|
||||
u.id AS user_id,
|
||||
coalesce(pl.score, 0) AS my_vote,
|
||||
(
|
||||
SELECT
|
||||
cf.id::bool
|
||||
FROM
|
||||
community_follower cf
|
||||
WHERE
|
||||
u.id = cf.user_id
|
||||
AND cf.community_id = ap.community_id) AS subscribed,
|
||||
(
|
||||
SELECT
|
||||
pr.id::bool
|
||||
FROM
|
||||
post_read pr
|
||||
WHERE
|
||||
u.id = pr.user_id
|
||||
AND pr.post_id = ap.id) AS read,
|
||||
(
|
||||
SELECT
|
||||
ps.id::bool
|
||||
FROM
|
||||
post_saved ps
|
||||
WHERE
|
||||
u.id = ps.user_id
|
||||
AND ps.post_id = ap.id) AS saved
|
||||
FROM
|
||||
user_ u
|
||||
CROSS JOIN all_post ap
|
||||
LEFT JOIN post_like pl ON u.id = pl.user_id
|
||||
AND ap.id = pl.post_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
ap.*,
|
||||
NULL AS user_id,
|
||||
NULL AS my_vote,
|
||||
NULL AS subscribed,
|
||||
NULL AS read,
|
||||
NULL AS saved
|
||||
FROM
|
||||
all_post ap;
|
||||
|
||||
|
@ -1,14 +1,11 @@
|
||||
-- Add federation columns to post, comment
|
||||
|
||||
alter table post
|
||||
ALTER TABLE post
|
||||
-- TODO uniqueness constraints should be added on these 3 columns later
|
||||
add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
||||
add column local boolean not null default true
|
||||
;
|
||||
ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
||||
ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
|
||||
|
||||
alter table comment
|
||||
ALTER TABLE comment
|
||||
-- TODO uniqueness constraints should be added on these 3 columns later
|
||||
add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
||||
add column local boolean not null default true
|
||||
;
|
||||
ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
|
||||
ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
|
||||
|
||||
|
@ -1,38 +1,70 @@
|
||||
-- User table
|
||||
|
||||
-- Need to regenerate user_view, user_mview
|
||||
drop view user_view cascade;
|
||||
DROP VIEW user_view CASCADE;
|
||||
|
||||
-- Remove the fedi_name constraint, drop that useless column
|
||||
alter table user_
|
||||
drop constraint user__name_fedi_name_key;
|
||||
ALTER TABLE user_
|
||||
DROP CONSTRAINT user__name_fedi_name_key;
|
||||
|
||||
alter table user_
|
||||
drop column fedi_name;
|
||||
ALTER TABLE user_
|
||||
DROP COLUMN fedi_name;
|
||||
|
||||
-- Community
|
||||
alter table community
|
||||
drop constraint community_name_key;
|
||||
ALTER TABLE community
|
||||
DROP CONSTRAINT community_name_key;
|
||||
|
||||
create view user_view as
|
||||
select
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
u.published,
|
||||
(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
|
||||
(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
|
||||
(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
|
||||
(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
|
||||
from user_ u;
|
||||
CREATE VIEW user_view AS
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.avatar,
|
||||
u.email,
|
||||
u.matrix_user_id,
|
||||
u.admin,
|
||||
u.banned,
|
||||
u.show_avatars,
|
||||
u.send_notifications_to_email,
|
||||
u.published,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
post p
|
||||
WHERE
|
||||
p.creator_id = u.id) AS number_of_posts,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
post p,
|
||||
post_like pl
|
||||
WHERE
|
||||
u.id = p.creator_id
|
||||
AND p.id = pl.post_id) AS post_score,
|
||||
(
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
comment c
|
||||
WHERE
|
||||
c.creator_id = u.id) AS number_of_comments,
|
||||
(
|
||||
SELECT
|
||||
coalesce(sum(score), 0)
|
||||
FROM
|
||||
comment c,
|
||||
comment_like cl
|
||||
WHERE
|
||||
u.id = c.creator_id
|
||||
AND c.id = cl.comment_id) AS comment_score
|
||||
FROM
|
||||
user_ u;
|
||||
|
||||
create materialized view user_mview as select * from user_view;
|
||||
CREATE MATERIALIZED VIEW user_mview AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
user_view;
|
||||
|
||||
create unique index idx_user_mview_id on user_mview (id);
|
||||
CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,5 @@
|
||||
-- The username index
|
||||
drop index idx_user_name_lower_actor_id;
|
||||
create unique index idx_user_name_lower on user_ (lower(name));
|
||||
DROP INDEX idx_user_name_lower_actor_id;
|
||||
|
||||
CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
|
||||
|
||||
|
@ -1,2 +1,4 @@
|
||||
drop index idx_user_name_lower;
|
||||
create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
|
||||
DROP INDEX idx_user_name_lower;
|
||||
|
||||
CREATE UNIQUE INDEX idx_user_name_lower_actor_id ON user_ (lower(name), lower(actor_id));
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue