Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine action tables #4459

Merged
merged 269 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
269 commits
Select commit Hold shift + click to select a range
a597eb6
Update comment_report_view.rs
dullbananas Jan 21, 2024
b7f11f1
Update comment_report_view.rs
dullbananas Jan 21, 2024
0f9ffa8
Update post_view.rs
dullbananas Feb 11, 2024
d4ffdf5
Update utils.rs
dullbananas Feb 11, 2024
aa7077f
Update schema.rs
dullbananas Feb 12, 2024
67394bf
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Feb 16, 2024
9761ea8
stuff
dullbananas Feb 16, 2024
893d62a
stuff
dullbananas Feb 16, 2024
66bdc49
fix actions
dullbananas Feb 16, 2024
224b1e4
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Feb 19, 2024
8cb325b
PostLike
dullbananas Feb 20, 2024
e12e2c2
fmt
dullbananas Feb 20, 2024
d3f57ba
more post stuff (partial)
dullbananas Feb 24, 2024
9a18388
remove uplete
dullbananas Feb 24, 2024
3f56324
returning
dullbananas Feb 24, 2024
af44c6c
rename read_comments field
dullbananas Feb 24, 2024
152a53f
PersonPostAggregates
dullbananas Feb 24, 2024
61c75ea
a
dullbananas Feb 24, 2024
cedce7d
fix usage of read_comments_amount
dullbananas Feb 24, 2024
fb143b5
comment
dullbananas Feb 24, 2024
042860c
community
dullbananas Feb 25, 2024
c93994b
community_block
dullbananas Feb 25, 2024
5293929
instance_block
dullbananas Feb 25, 2024
798e326
LocalUser::export_backup
dullbananas Feb 25, 2024
ea69354
person_block
dullbananas Feb 26, 2024
d3996dd
person
dullbananas Feb 26, 2024
074a112
stuff (partial)
dullbananas Mar 7, 2024
3efa161
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Mar 7, 2024
fdb9f79
update cargo.lock
dullbananas Mar 7, 2024
0d0fa01
fix lemmy_db_schema
dullbananas Mar 7, 2024
db403ec
post_view
dullbananas Mar 8, 2024
f84e731
comment_report_view
dullbananas Mar 8, 2024
c46ae9a
comment_view
dullbananas Mar 8, 2024
27bb4b1
post_report_view
dullbananas Mar 8, 2024
2233fea
find and replace some selected values
dullbananas Mar 8, 2024
af59298
private_message_view
dullbananas Mar 9, 2024
4091606
vote_view
dullbananas Mar 9, 2024
0b3090b
comment_reply_view
dullbananas Mar 9, 2024
9bde054
some action views
dullbananas Mar 9, 2024
5c68ec0
action_query, find_action
dullbananas Mar 10, 2024
1650fa8
community_view
dullbananas Mar 10, 2024
82414e1
block views
dullbananas Mar 10, 2024
4b9291c
person_mention_view
dullbananas Mar 10, 2024
5e27ed7
remove struct update
dullbananas Mar 10, 2024
56e0262
refactor actions function
dullbananas Mar 10, 2024
dfe1c74
actions_alias
dullbananas Mar 10, 2024
231fadb
clean up return types and trait bounds
dullbananas Mar 12, 2024
1f96d7d
fix all rust code
dullbananas Mar 12, 2024
169e175
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Mar 16, 2024
cfe4bf9
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Mar 29, 2024
0f3df35
fmt
dullbananas Mar 29, 2024
f71e8b4
clippy fix
dullbananas Mar 29, 2024
7cf8684
Migrate tables
dullbananas Apr 1, 2024
54faf78
nullable read_comments_amount
dullbananas Apr 1, 2024
530926f
migrate indexes and contraints, and add statistics
dullbananas Apr 4, 2024
dbbb6ba
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Apr 5, 2024
ded71dd
fix what appears to be a messed up merge
dullbananas Apr 5, 2024
afec4c0
commented thing
dullbananas Apr 21, 2024
e0b45aa
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas May 23, 2024
a09f8a8
Create uplete.rs
dullbananas May 24, 2024
bf541d7
Update uplete.rs
dullbananas May 24, 2024
43813cf
Update uplete.rs
dullbananas May 24, 2024
3ae5eef
Update up.sql
dullbananas May 24, 2024
10b3009
Update comment.rs
dullbananas May 24, 2024
59f41b2
Update Cargo.toml
dullbananas May 24, 2024
3dc483c
Update comment.rs
dullbananas May 24, 2024
0174029
Update post.rs
dullbananas May 24, 2024
2b584e2
Update comment_view.rs
dullbananas May 24, 2024
c4fba28
Update post_view.rs
dullbananas May 24, 2024
2de9499
Update comment_reply_view.rs
dullbananas May 24, 2024
ed7fcc6
Update person_mention_view.rs
dullbananas May 24, 2024
db475a9
Update Cargo.toml
dullbananas May 24, 2024
4f70eb1
Update utils.rs
dullbananas May 24, 2024
7de0915
Update comment.rs
dullbananas May 24, 2024
3d5140e
Update utils.rs
dullbananas May 24, 2024
88a7ab5
Update uplete.rs
dullbananas May 24, 2024
dacdda2
Update uplete.rs
dullbananas May 24, 2024
0b1132d
Update uplete.rs
dullbananas May 24, 2024
235e547
Update comment.rs
dullbananas May 24, 2024
3980cf1
Update uplete.rs
dullbananas May 24, 2024
9d5f9ca
Update uplete.rs
dullbananas May 24, 2024
237a38a
Update uplete.rs
dullbananas May 24, 2024
df2d776
Update uplete.rs
dullbananas May 24, 2024
e1aaf80
Update uplete.rs
dullbananas May 24, 2024
d68b3ad
Update uplete.rs
dullbananas May 24, 2024
0945a76
Update comment_view.rs
dullbananas May 24, 2024
efc47c9
Update post_view.rs
dullbananas May 24, 2024
beb3b9f
Update triggers.sql
dullbananas May 25, 2024
a821538
Update triggers.sql
dullbananas May 25, 2024
6bbe0a9
Update triggers.sql
dullbananas May 25, 2024
eccd722
Update comment_reply_view.rs
dullbananas May 25, 2024
5bc8e79
Update person_mention_view.rs
dullbananas May 25, 2024
4ae63c6
Update person_mention_view.rs
dullbananas May 25, 2024
6662871
Update comment_reply_view.rs
dullbananas May 25, 2024
c73ab7f
Update uplete.rs
dullbananas May 25, 2024
34cba5b
start removing post_id column
dullbananas May 25, 2024
4771e69
Update down.sql
dullbananas May 25, 2024
9dd9057
Update schema.rs
dullbananas May 25, 2024
2935ff4
Update comment.rs
dullbananas May 25, 2024
7c00bcc
Update comment.rs
dullbananas May 25, 2024
1cd3969
Update comment.rs
dullbananas May 25, 2024
d5740bc
Update schema.rs
dullbananas May 25, 2024
bfb5698
Update comment.rs
dullbananas May 25, 2024
b7a904b
Update like.rs
dullbananas May 25, 2024
71b0fe7
Update comment.rs
dullbananas May 25, 2024
67eadce
Update up.sql
dullbananas May 25, 2024
398718b
Update down.sql
dullbananas May 25, 2024
b2d0ff5
Update down.sql
dullbananas May 25, 2024
1e112c0
Update up.sql
dullbananas May 25, 2024
b6de369
Update up.sql
dullbananas May 25, 2024
0f2916c
Update down.sql
dullbananas May 25, 2024
f25205c
Update comment.rs
dullbananas May 25, 2024
5e3ce5a
Update vote_view.rs
dullbananas May 25, 2024
a309aaa
Update vote_view.rs
dullbananas May 25, 2024
86838e2
Update comment_aggregates.rs
dullbananas May 25, 2024
7a8e53d
Update person_aggregates.rs
dullbananas May 25, 2024
fde92c6
Update comment_view.rs
dullbananas May 25, 2024
0794ff9
Update vote_view.rs
dullbananas May 25, 2024
4918e72
Update mod.rs
dullbananas May 25, 2024
421e056
Update create.rs
dullbananas May 25, 2024
1a08f27
Update comment.rs
dullbananas May 25, 2024
437200f
Update community.rs
dullbananas May 25, 2024
f62fcc4
Update community.rs
dullbananas May 25, 2024
488797b
Update up.sql
dullbananas May 25, 2024
90b10b7
Update uplete.rs
dullbananas May 25, 2024
d502b68
Update uplete.rs
dullbananas May 25, 2024
29f4fa7
revert to UpleteCount
dullbananas May 25, 2024
730f4cb
Update comment.rs
dullbananas May 25, 2024
cadab90
Update traits.rs
dullbananas May 25, 2024
f613f98
Update comment.rs
dullbananas May 26, 2024
8c42d95
Update community.rs
dullbananas May 26, 2024
dabb479
Update community_block.rs
dullbananas May 26, 2024
4c3af86
Update community.rs
dullbananas May 26, 2024
c3e4835
Update instance_block.rs
dullbananas May 26, 2024
11a90f1
Update instance.rs
dullbananas May 26, 2024
7b56d5b
Update community_block.rs
dullbananas May 26, 2024
d264f04
Update person.rs
dullbananas May 26, 2024
436dc11
Update person_block.rs
dullbananas May 26, 2024
86a1740
Update person.rs
dullbananas May 26, 2024
6090afe
Update person_block.rs
dullbananas May 26, 2024
09dea6b
Update person.rs
dullbananas May 26, 2024
7c6ffb4
Update instance.rs
dullbananas May 26, 2024
abb1a74
Update instance_block.rs
dullbananas May 26, 2024
322aa73
Update instance.rs
dullbananas May 26, 2024
dba0e37
Update person.rs
dullbananas May 26, 2024
b70c9fe
Update post.rs
dullbananas May 26, 2024
d894652
Update comment.rs
dullbananas May 26, 2024
4b3d9ce
Update community.rs
dullbananas May 26, 2024
e454d81
Update person.rs
dullbananas May 26, 2024
c82c8e0
Update post_view.rs
dullbananas May 26, 2024
080b536
Merge branch 'main' into smoosh-tables-together
dullbananas May 27, 2024
422df50
Update comment.rs
dullbananas May 27, 2024
359f691
Merge branch 'main' into smoosh-tables-together
dullbananas May 27, 2024
0d210d9
reduce diff
dullbananas May 27, 2024
b23429c
revert some changes in views
dullbananas May 28, 2024
602db61
Update post_view.rs
dullbananas May 28, 2024
56ba24b
Update comment.rs
dullbananas May 28, 2024
1c1249a
Update post.rs
dullbananas May 28, 2024
89a7f6a
fix missing cfg_attr
dullbananas May 28, 2024
aa157d4
rewrite uplete
dullbananas May 28, 2024
216b6d2
Update Cargo.toml
dullbananas May 28, 2024
db20b69
Update Cargo.toml
dullbananas May 28, 2024
7f8a689
Update uplete.rs
dullbananas May 28, 2024
e6a7a5a
add `pub` to structs that appear in trait bounds
dullbananas May 28, 2024
c1435b6
optional = true
dullbananas May 28, 2024
cd508cc
Update uplete.rs
dullbananas May 28, 2024
287947a
Update community.rs
dullbananas May 28, 2024
4e89913
Update comment.rs
dullbananas May 28, 2024
7ed7080
Update uplete.rs
dullbananas May 28, 2024
bc941a2
Update uplete.rs
dullbananas May 28, 2024
19c4ba1
Update uplete.rs
dullbananas May 29, 2024
60af6ed
Update uplete.rs
dullbananas May 29, 2024
0e441c8
something is wrong with me
dullbananas May 29, 2024
4751d28
use new uplete function everywhere
dullbananas May 29, 2024
95879d9
Merge branch 'main' into smoosh-tables-together
dullbananas May 29, 2024
591895e
fmt
dullbananas May 29, 2024
eb720a9
fmt
dullbananas May 29, 2024
6ce8f67
Keep post_id when comment_actions::liked is not null
dullbananas May 29, 2024
3c56d1b
Update up.sql
dullbananas May 29, 2024
13a63e6
Update up.sql
dullbananas May 29, 2024
d2f8710
clean up up.sql
dullbananas May 29, 2024
fdb7bce
clean up down.sql
dullbananas May 29, 2024
64eb930
fix
dullbananas May 29, 2024
4cd46ed
Update person_aggregates.rs
dullbananas May 29, 2024
a0b0566
fmt
dullbananas May 29, 2024
5ba61c4
Update uplete.rs
dullbananas May 29, 2024
85053e5
fmt
dullbananas May 29, 2024
83901d1
Update uplete.rs
dullbananas May 29, 2024
0064227
Update community.rs
dullbananas May 29, 2024
b949213
Update uplete.rs
dullbananas May 30, 2024
e3f1e83
Merge branch 'main' into smoosh-tables-together
dullbananas May 30, 2024
1761eb5
Update local_user.rs
dullbananas May 30, 2024
37ed651
fmt
dullbananas May 30, 2024
c95f0b0
fix
dullbananas May 30, 2024
561e043
fix
dullbananas May 30, 2024
d9890f1
fmt
dullbananas May 30, 2024
266edf4
improve uplete api
dullbananas May 30, 2024
85b52c6
Update uplete.rs
dullbananas May 30, 2024
ce75697
fix
dullbananas May 30, 2024
d124a58
fix
dullbananas May 30, 2024
c72ec04
Update uplete.rs
dullbananas May 30, 2024
2aaddb1
Update uplete.rs
dullbananas May 30, 2024
fa312c2
Update uplete.rs
dullbananas May 30, 2024
4b0c059
Update uplete.rs
dullbananas May 30, 2024
102f4b3
fix
dullbananas May 30, 2024
3ee0bec
fix test
dullbananas May 30, 2024
ba13257
fix db_views_actor imports
dullbananas May 30, 2024
37f3406
fix uplete import in post_view test
dullbananas May 30, 2024
ae7838e
rerun ci
dullbananas May 30, 2024
334c0f6
fix AllNull syntax error
dullbananas May 30, 2024
acfc3a0
fix DynColumn
dullbananas May 30, 2024
314f336
Fix rust syntax
dullbananas May 31, 2024
542ad8e
fmt
dullbananas May 31, 2024
d469e9a
fix iter
dullbananas May 31, 2024
b841679
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas May 31, 2024
82cae91
pain
dullbananas May 31, 2024
c948240
Update community_moderators.rs
dullbananas May 31, 2024
ee1de0e
Update community_moderator_view.rs
dullbananas May 31, 2024
7fc1913
Update uplete.rs
dullbananas May 31, 2024
383dc4a
Fix mistake found by chatgpt
dullbananas May 31, 2024
a471061
revert debugging stuff, change migration date, refactor constraint
dullbananas May 31, 2024
590542b
Update down.sql
dullbananas May 31, 2024
55cecf2
Merge branch 'main' into smoosh-tables-together
dullbananas May 31, 2024
e1b0e1d
Update down.sql
dullbananas Jun 1, 2024
f2f6984
Merge branch 'main' into smoosh-tables-together
dullbananas Jun 6, 2024
dcc93f3
Merge branch 'main' into smoosh-tables-together
dullbananas Jun 10, 2024
06de0c5
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Jun 25, 2024
689d2ef
Merge branch 'smoosh-tables-together' of https://github.com/dullbanan…
dullbananas Jun 25, 2024
0762623
fmt
dullbananas Jun 25, 2024
71667d9
Merge branch 'main' into smoosh-tables-together
dullbananas Jul 2, 2024
f517643
make things added to db_schema::utils more understandable
dullbananas Jul 2, 2024
02f0513
update rust version for woodpecker
dullbananas Jul 2, 2024
a811d82
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Jul 19, 2024
3351090
Merge branch 'main' into smoosh-tables-together
dullbananas Jul 26, 2024
ed3267e
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Aug 21, 2024
a190cce
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Oct 15, 2024
9f262f5
finish merge
dullbananas Oct 15, 2024
b13f988
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Oct 15, 2024
c8aceb2
Fix index that checked read_comments twice instead of also checking r…
dullbananas Oct 27, 2024
b7a84cf
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Oct 27, 2024
3822d77
Merge branch 'main' into smoosh-tables-together
phiresky Oct 28, 2024
51cdbb8
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Oct 30, 2024
53d135e
fix
dullbananas Oct 31, 2024
2bac1b3
uplete: test_count, test_generated_sql_setting_one_column_null, test_…
dullbananas Nov 6, 2024
92b1c28
refactor uplete sql test
dullbananas Nov 6, 2024
4e4444e
test setting both columns to null in uplete
dullbananas Nov 6, 2024
5ffd4b1
make AllNull generic
dullbananas Nov 6, 2024
83b4b90
test AllNull
dullbananas Nov 6, 2024
9bde15e
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Nov 11, 2024
e3bbb12
Merge remote-tracking branch 'upstream/main' into smoosh-tables-together
dullbananas Nov 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ i-love-jesus = { version = "0.1.0" }
clap = { version = "4.5.13", features = ["derive", "env"] }
pretty_assertions = "1.4.0"
derive-new = "0.7.0"
diesel-bind-if-some = "0.1.0"
tuplex = "0.1.2"

[dependencies]
lemmy_api = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ pub async fn update_read_comments(
person_id,
post_id,
read_comments,
..PersonPostAggregatesForm::default()
};

PersonPostAggregates::upsert(pool, &person_post_agg_form).await?;
Expand Down
4 changes: 4 additions & 0 deletions crates/db_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ full = [
"tokio-postgres-rustls",
"rustls",
"i-love-jesus",
"tuplex",
"diesel-bind-if-some",
]

[dependencies]
Expand Down Expand Up @@ -76,8 +78,10 @@ rustls = { workspace = true, optional = true }
uuid = { workspace = true, features = ["v4"] }
i-love-jesus = { workspace = true, optional = true }
anyhow = { workspace = true }
diesel-bind-if-some = { workspace = true, optional = true }
moka.workspace = true
derive-new.workspace = true
tuplex = { workspace = true, optional = true }

[dev-dependencies]
serial_test = { workspace = true }
Expand Down
16 changes: 9 additions & 7 deletions crates/db_schema/replaceable_schema/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ AS $a$
BEGIN
EXECUTE replace($b$
-- When a thing gets a vote, update its aggregates and its creator's aggregates
CALL r.create_triggers ('thing_like', $$
CALL r.create_triggers ('thing_actions', $$
BEGIN
WITH thing_diff AS ( UPDATE
thing_aggregates AS a
SET
score = a.score + diff.upvotes - diff.downvotes, upvotes = a.upvotes + diff.upvotes, downvotes = a.downvotes + diff.downvotes, controversy_rank = r.controversy_rank ((a.upvotes + diff.upvotes)::numeric, (a.downvotes + diff.downvotes)::numeric)
FROM (
SELECT
(thing_like).thing_id, coalesce(sum(count_diff) FILTER (WHERE (thing_like).score = 1), 0) AS upvotes, coalesce(sum(count_diff) FILTER (WHERE (thing_like).score != 1), 0) AS downvotes FROM select_old_and_new_rows AS old_and_new_rows GROUP BY (thing_like).thing_id) AS diff
(thing_actions).thing_id, coalesce(sum(count_diff) FILTER (WHERE (thing_actions).like_score = 1), 0) AS upvotes, coalesce(sum(count_diff) FILTER (WHERE (thing_actions).like_score != 1), 0) AS downvotes FROM select_old_and_new_rows AS old_and_new_rows
WHERE (thing_actions).like_score IS NOT NULL GROUP BY (thing_actions).thing_id) AS diff
WHERE
a.thing_id = diff.thing_id
AND (diff.upvotes, diff.downvotes) != (0, 0)
Expand Down Expand Up @@ -360,18 +361,19 @@ CREATE TRIGGER comment_count
-- Count subscribers for communities.
-- subscribers should be updated only when a local community is followed by a local or remote person.
-- subscribers_local should be updated only when a local person follows a local or remote community.
CALL r.create_triggers ('community_follower', $$
CALL r.create_triggers ('community_actions', $$
BEGIN
UPDATE
community_aggregates AS a
SET
subscribers = a.subscribers + diff.subscribers, subscribers_local = a.subscribers_local + diff.subscribers_local
FROM (
SELECT
(community_follower).community_id, coalesce(sum(count_diff) FILTER (WHERE community.local), 0) AS subscribers, coalesce(sum(count_diff) FILTER (WHERE person.local), 0) AS subscribers_local
(community_actions).community_id, coalesce(sum(count_diff) FILTER (WHERE community.local), 0) AS subscribers, coalesce(sum(count_diff) FILTER (WHERE person.local), 0) AS subscribers_local
FROM select_old_and_new_rows AS old_and_new_rows
LEFT JOIN community ON community.id = (community_follower).community_id
LEFT JOIN person ON person.id = (community_follower).person_id GROUP BY (community_follower).community_id) AS diff
LEFT JOIN community ON community.id = (community_actions).community_id
LEFT JOIN person ON person.id = (community_actions).person_id
WHERE (community_actions).followed IS NOT NULL GROUP BY (community_actions).community_id) AS diff
WHERE
a.community_id = diff.community_id
AND (diff.subscribers, diff.subscribers_local) != (0, 0);
Expand Down Expand Up @@ -541,7 +543,7 @@ CREATE FUNCTION r.delete_follow_before_person ()
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM community_follower AS c
DELETE FROM community_actions AS c
WHERE c.person_id = OLD.id;
RETURN OLD;
END;
Expand Down
23 changes: 16 additions & 7 deletions crates/db_schema/src/aggregates/person_post_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ use crate::{
aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
diesel::OptionalExtension,
newtypes::{PersonId, PostId},
schema::person_post_aggregates::dsl::{person_id, person_post_aggregates, post_id},
utils::{get_conn, DbPool},
schema::post_actions,
utils::{find_action, get_conn, now, DbPool},
};
use diesel::{
expression::SelectableHelper,
insert_into,
result::Error,
ExpressionMethods,
NullableExpressionMethods,
QueryDsl,
};
use diesel::{insert_into, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;

impl PersonPostAggregates {
Expand All @@ -14,11 +21,13 @@ impl PersonPostAggregates {
form: &PersonPostAggregatesForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(person_post_aggregates)
let form = (form, post_actions::read_comments.eq(now().nullable()));
insert_into(post_actions::table)
.values(form)
.on_conflict((person_id, post_id))
.on_conflict((post_actions::person_id, post_actions::post_id))
.do_update()
.set(form)
.returning(Self::as_select())
.get_result::<Self>(conn)
.await
}
Expand All @@ -28,8 +37,8 @@ impl PersonPostAggregates {
post_id_: PostId,
) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
person_post_aggregates
.find((person_id_, post_id_))
find_action(post_actions::read_comments, (person_id_, post_id_))
.select(Self::as_select())
.first(conn)
.await
.optional()
Expand Down
14 changes: 10 additions & 4 deletions crates/db_schema/src/aggregates/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use crate::schema::{
comment_aggregates,
community_aggregates,
person_aggregates,
person_post_aggregates,
post_actions,
post_aggregates,
site_aggregates,
};
use chrono::{DateTime, Utc};
#[cfg(feature = "full")]
use diesel::{dsl, expression_methods::NullableExpressionMethods};
#[cfg(feature = "full")]
use i_love_jesus::CursorKeysModule;
use serde::{Deserialize, Serialize};
#[cfg(feature = "full")]
Expand Down Expand Up @@ -151,7 +153,7 @@ pub struct PostAggregates {
feature = "full",
derive(Queryable, Selectable, Associations, Identifiable)
)]
#[cfg_attr(feature = "full", diesel(table_name = person_post_aggregates))]
#[cfg_attr(feature = "full", diesel(table_name = post_actions))]
#[cfg_attr(feature = "full", diesel(primary_key(person_id, post_id)))]
#[cfg_attr(feature = "full", diesel(belongs_to(crate::source::person::Person)))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
Expand All @@ -162,18 +164,22 @@ pub struct PersonPostAggregates {
/// The number of comments they've read on that post.
///
/// This is updated to the current post comment count every time they view a post.
#[cfg_attr(feature = "full", diesel(select_expression = post_actions::read_comments_amount.assume_not_null()))]
#[cfg_attr(feature = "full", diesel(select_expression_type = dsl::AssumeNotNull<post_actions::read_comments_amount>))]
pub read_comments: i64,
#[cfg_attr(feature = "full", diesel(select_expression = post_actions::read_comments.assume_not_null()))]
#[cfg_attr(feature = "full", diesel(select_expression_type = dsl::AssumeNotNull<post_actions::read_comments>))]
pub published: DateTime<Utc>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these columns are nullable how can you assume that they are not null? Isnt it gonna crash?

}

#[derive(Clone, Default)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = person_post_aggregates))]
#[cfg_attr(feature = "full", diesel(table_name = post_actions))]
pub struct PersonPostAggregatesForm {
pub person_id: PersonId,
pub post_id: PostId,
#[cfg_attr(feature = "full", diesel(column_name = read_comments_amount))]
pub read_comments: i64,
pub published: Option<DateTime<Utc>>,
}

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Copy, Hash)]
Expand Down
66 changes: 45 additions & 21 deletions crates/db_schema/src/impls/comment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
diesel::{DecoratableTarget, OptionalExtension},
newtypes::{CommentId, DbUrl, PersonId},
schema::comment,
schema::{comment, comment_actions},
source::comment::{
Comment,
CommentInsertForm,
Expand All @@ -12,10 +12,25 @@ use crate::{
CommentUpdateForm,
},
traits::{Crud, Likeable, Saveable},
utils::{functions::coalesce, get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT},
utils::{
functions::coalesce,
get_conn,
naive_now,
now,
uplete,
DbPool,
DELETED_REPLACEMENT_TEXT,
},
};
use chrono::{DateTime, Utc};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel::{
dsl::insert_into,
expression::SelectableHelper,
result::Error,
ExpressionMethods,
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use diesel_ltree::Ltree;
use url::Url;
Expand Down Expand Up @@ -141,25 +156,30 @@ impl Likeable for CommentLike {
type Form = CommentLikeForm;
type IdType = CommentId;
async fn like(pool: &mut DbPool<'_>, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use crate::schema::comment_like::dsl::{comment_id, comment_like, person_id};
let conn = &mut get_conn(pool).await?;
insert_into(comment_like)
let comment_like_form = (
comment_like_form,
comment_actions::liked.eq(now().nullable()),
);
insert_into(comment_actions::table)
.values(comment_like_form)
.on_conflict((comment_id, person_id))
.on_conflict((comment_actions::comment_id, comment_actions::person_id))
.do_update()
.set(comment_like_form)
.returning(Self::as_select())
.get_result::<Self>(conn)
.await
}
async fn remove(
pool: &mut DbPool<'_>,
person_id: PersonId,
comment_id: CommentId,
) -> Result<usize, Error> {
use crate::schema::comment_like::dsl::comment_like;
) -> Result<uplete::Count, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(comment_like.find((person_id, comment_id)))
.execute(conn)
uplete::new(comment_actions::table.find((person_id, comment_id)))
.set_null(comment_actions::like_score)
.set_null(comment_actions::liked)
.get_result(conn)
.await
}
}
Expand All @@ -171,26 +191,30 @@ impl Saveable for CommentSaved {
pool: &mut DbPool<'_>,
comment_saved_form: &CommentSavedForm,
) -> Result<Self, Error> {
use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id};
let conn = &mut get_conn(pool).await?;
insert_into(comment_saved)
let comment_saved_form = (
comment_saved_form,
comment_actions::saved.eq(now().nullable()),
);
insert_into(comment_actions::table)
.values(comment_saved_form)
.on_conflict((comment_id, person_id))
.on_conflict((comment_actions::comment_id, comment_actions::person_id))
.do_update()
.set(comment_saved_form)
.returning(Self::as_select())
.get_result::<Self>(conn)
.await
}
async fn unsave(
pool: &mut DbPool<'_>,
comment_saved_form: &CommentSavedForm,
) -> Result<usize, Error> {
use crate::schema::comment_saved::dsl::comment_saved;
) -> Result<uplete::Count, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(
comment_saved.find((comment_saved_form.person_id, comment_saved_form.comment_id)),
uplete::new(
comment_actions::table.find((comment_saved_form.person_id, comment_saved_form.comment_id)),
)
.execute(conn)
.set_null(comment_actions::saved)
.get_result(conn)
.await
}
}
Expand All @@ -216,7 +240,7 @@ mod tests {
post::{Post, PostInsertForm},
},
traits::{Crud, Likeable, Saveable},
utils::build_db_pool_for_tests,
utils::{build_db_pool_for_tests, uplete},
};
use diesel_ltree::Ltree;
use lemmy_utils::error::LemmyResult;
Expand Down Expand Up @@ -342,8 +366,8 @@ mod tests {
format!("0.{}.{}", expected_comment.id, inserted_child_comment.id),
inserted_child_comment.path.0,
);
assert_eq!(1, like_removed);
assert_eq!(1, saved_removed);
assert_eq!(uplete::Count::only_updated(1), like_removed);
assert_eq!(uplete::Count::only_deleted(1), saved_removed);
assert_eq!(1, num_deleted);

Ok(())
Expand Down
Loading