Skip to content

Commit

Permalink
Remove id column and use different primary key on some tables (#4093)
Browse files Browse the repository at this point in the history
* post_saved

* fmt

* remove unique and not null

* put person_id first in primary key and remove index

* use post_saved.find

* change captcha_answer

* remove removal of not null

* comment_aggregates

* comment_like

* comment_saved

* aggregates

* remove "\"

* deduplicate site_aggregates

* person_post_aggregates

* community_moderator

* community_block

* community_person_ban

* custom_emoji_keyword

* federation allow/block list

* federation_queue_state

* instance_block

* local_site_rate_limit, local_user_language, login_token

* person_ban, person_block, person_follower, post_like, post_read, received_activity

* community_follower, community_language, site_language

* fmt

* image_upload

* remove unused newtypes

* remove more indexes

* use .find

* merge

* fix site_aggregates_site function

* fmt

* Primary keys dess (#17)

* Also order reports by oldest first (ref #4123) (#4129)

* Support signed fetch for federation (fixes #868) (#4125)

* Support signed fetch for federation (fixes #868)

* taplo

* add federation queue state to get_federated_instances api (#4104)

* add federation queue state to get_federated_instances api

* feature gate

* move retry sleep function

* move stuff around

* Add UI setting for collapsing bot comments. Fixes #3838 (#4098)

* Add UI setting for collapsing bot comments. Fixes #3838

* Fixing clippy check.

* Only keep sent and received activities for 7 days (fixes #4113, fixes #4110) (#4131)

* Only check auth secure on release mode. (#4127)

* Only check auth secure on release mode.

* Fixing wrong js-client.

* Adding is_debug_mode var.

* Fixing the desktop image on the README. (#4135)

* Delete dupes and add possibly missing unique constraint on person_aggregates.

* Fixing clippy lints.

---------

Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>

* fmt

* Update community_block.rs

* Update instance_block.rs

* Update person_block.rs

* Update person_block.rs

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
  • Loading branch information
4 people authored Nov 13, 2023
1 parent 1dc6c60 commit 8e2cbc9
Show file tree
Hide file tree
Showing 48 changed files with 597 additions and 333 deletions.
5 changes: 2 additions & 3 deletions crates/db_schema/src/aggregates/comment_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl CommentAggregates {
pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
comment_aggregates::table
.filter(comment_aggregates::comment_id.eq(comment_id))
.find(comment_id)
.first::<Self>(conn)
.await
}
Expand All @@ -22,8 +22,7 @@ impl CommentAggregates {
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;

diesel::update(comment_aggregates::table)
.filter(comment_aggregates::comment_id.eq(comment_id))
diesel::update(comment_aggregates::table.find(comment_id))
.set(comment_aggregates::hot_rank.eq(hot_rank(
comment_aggregates::score,
comment_aggregates::published,
Expand Down
9 changes: 3 additions & 6 deletions crates/db_schema/src/aggregates/community_aggregates.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
aggregates::structs::CommunityAggregates,
newtypes::CommunityId,
schema::{
community_aggregates,
community_aggregates::{community_id, subscribers},
},
schema::{community_aggregates, community_aggregates::subscribers},
utils::{get_conn, DbPool},
};
use diesel::{result::Error, ExpressionMethods, QueryDsl};
Expand All @@ -14,7 +11,7 @@ impl CommunityAggregates {
pub async fn read(pool: &mut DbPool<'_>, for_community_id: CommunityId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
community_aggregates::table
.filter(community_id.eq(for_community_id))
.find(for_community_id)
.first::<Self>(conn)
.await
}
Expand All @@ -26,7 +23,7 @@ impl CommunityAggregates {
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let new_subscribers: i64 = new_subscribers.into();
diesel::update(community_aggregates::table.filter(community_id.eq(for_community_id)))
diesel::update(community_aggregates::table.find(for_community_id))
.set(subscribers.eq(new_subscribers))
.get_result::<Self>(conn)
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/db_schema/src/aggregates/person_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::{
schema::person_aggregates,
utils::{get_conn, DbPool},
};
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel::{result::Error, QueryDsl};
use diesel_async::RunQueryDsl;

impl PersonAggregates {
pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
person_aggregates::table
.filter(person_aggregates::person_id.eq(person_id))
.find(person_id)
.first::<Self>(conn)
.await
}
Expand Down
5 changes: 2 additions & 3 deletions crates/db_schema/src/aggregates/person_post_aggregates.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{
aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
diesel::BoolExpressionMethods,
newtypes::{PersonId, PostId},
schema::person_post_aggregates::dsl::{person_id, person_post_aggregates, post_id},
utils::{get_conn, DbPool},
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel::{insert_into, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;

impl PersonPostAggregates {
Expand All @@ -29,7 +28,7 @@ impl PersonPostAggregates {
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
person_post_aggregates
.filter(post_id.eq(post_id_).and(person_id.eq(person_id_)))
.find((person_id_, post_id_))
.first::<Self>(conn)
.await
}
Expand Down
5 changes: 2 additions & 3 deletions crates/db_schema/src/aggregates/post_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl PostAggregates {
pub async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
post_aggregates::table
.filter(post_aggregates::post_id.eq(post_id))
.find(post_id)
.first::<Self>(conn)
.await
}
Expand All @@ -33,8 +33,7 @@ impl PostAggregates {
.first::<i64>(conn)
.await?;

diesel::update(post_aggregates::table)
.filter(post_aggregates::post_id.eq(post_id))
diesel::update(post_aggregates::table.find(post_id))
.set((
post_aggregates::hot_rank.eq(hot_rank(post_aggregates::score, post_aggregates::published)),
post_aggregates::hot_rank_active.eq(hot_rank(
Expand Down
12 changes: 6 additions & 6 deletions crates/db_schema/src/aggregates/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use ts_rs::TS;
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = comment_aggregates))]
#[cfg_attr(feature = "full", diesel(belongs_to(crate::source::comment::Comment)))]
#[cfg_attr(feature = "full", diesel(primary_key(comment_id)))]
#[cfg_attr(feature = "full", ts(export))]
/// Aggregate data for a comment.
pub struct CommentAggregates {
pub id: i32,
pub comment_id: CommentId,
pub score: i64,
pub upvotes: i64,
Expand All @@ -40,10 +40,10 @@ pub struct CommentAggregates {
feature = "full",
diesel(belongs_to(crate::source::community::Community))
)]
#[cfg_attr(feature = "full", diesel(primary_key(community_id)))]
#[cfg_attr(feature = "full", ts(export))]
/// Aggregate data for a community.
pub struct CommunityAggregates {
pub id: i32,
pub community_id: CommunityId,
pub subscribers: i64,
pub posts: i64,
Expand All @@ -65,10 +65,10 @@ pub struct CommunityAggregates {
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = person_aggregates))]
#[cfg_attr(feature = "full", diesel(belongs_to(crate::source::person::Person)))]
#[cfg_attr(feature = "full", diesel(primary_key(person_id)))]
#[cfg_attr(feature = "full", ts(export))]
/// Aggregate data for a person.
pub struct PersonAggregates {
pub id: i32,
pub person_id: PersonId,
pub post_count: i64,
#[serde(skip)]
Expand All @@ -82,10 +82,10 @@ pub struct PersonAggregates {
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = post_aggregates))]
#[cfg_attr(feature = "full", diesel(belongs_to(crate::source::post::Post)))]
#[cfg_attr(feature = "full", diesel(primary_key(post_id)))]
#[cfg_attr(feature = "full", ts(export))]
/// Aggregate data for a post.
pub struct PostAggregates {
pub id: i32,
pub post_id: PostId,
pub comments: i64,
pub score: i64,
Expand Down Expand Up @@ -124,10 +124,10 @@ pub struct PostAggregates {
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable))]
#[cfg_attr(feature = "full", diesel(table_name = person_post_aggregates))]
#[cfg_attr(feature = "full", diesel(primary_key(person_id, post_id)))]
#[cfg_attr(feature = "full", diesel(belongs_to(crate::source::person::Person)))]
/// Aggregate data for a person's post.
pub struct PersonPostAggregates {
pub id: i32,
pub person_id: PersonId,
pub post_id: PostId,
/// The number of comments they've read on that post.
Expand All @@ -151,10 +151,10 @@ pub struct PersonPostAggregatesForm {
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = site_aggregates))]
#[cfg_attr(feature = "full", diesel(belongs_to(crate::source::site::Site)))]
#[cfg_attr(feature = "full", diesel(primary_key(site_id)))]
#[cfg_attr(feature = "full", ts(export))]
/// Aggregate data for a site.
pub struct SiteAggregates {
pub id: i32,
pub site_id: SiteId,
pub users: i64,
pub posts: i64,
Expand Down
9 changes: 4 additions & 5 deletions crates/db_schema/src/impls/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ impl SentActivity {

impl ReceivedActivity {
pub async fn create(pool: &mut DbPool<'_>, ap_id_: &DbUrl) -> Result<(), Error> {
use crate::schema::received_activity::dsl::{ap_id, id, received_activity};
use crate::schema::received_activity::dsl::{ap_id, received_activity};
let conn = &mut get_conn(pool).await?;
let res = insert_into(received_activity)
let rows_affected = insert_into(received_activity)
.values(ap_id.eq(ap_id_))
.on_conflict_do_nothing()
.returning(id)
.get_result::<i64>(conn)
.execute(conn)
.await
.optional()?;
if res.is_some() {
if rows_affected == Some(1) {
// new activity inserted successfully
Ok(())
} else {
Expand Down
8 changes: 3 additions & 5 deletions crates/db_schema/src/impls/actor_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl SiteLanguage {
site::table
.inner_join(local_site::table)
.inner_join(site_language::table)
.order(site_language::id)
.order(site_language::language_id)
.select(site_language::language_id)
.load(conn)
.await
Expand Down Expand Up @@ -191,14 +191,12 @@ impl CommunityLanguage {
for_language_id: Option<LanguageId>,
for_community_id: CommunityId,
) -> Result<(), LemmyError> {
use crate::schema::community_language::dsl::{community_id, community_language, language_id};
use crate::schema::community_language::dsl::community_language;
let conn = &mut get_conn(pool).await?;

if let Some(for_language_id) = for_language_id {
let is_allowed = select(exists(
community_language
.filter(language_id.eq(for_language_id))
.filter(community_id.eq(for_community_id)),
community_language.find((for_community_id, for_language_id)),
))
.get_result(conn)
.await?;
Expand Down
17 changes: 8 additions & 9 deletions crates/db_schema/src/impls/captcha_answer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
schema::captcha_answer::dsl::{answer, captcha_answer, uuid},
schema::captcha_answer::dsl::{answer, captcha_answer},
source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
utils::{functions::lower, get_conn, DbPool},
};
Expand Down Expand Up @@ -31,16 +31,15 @@ impl CaptchaAnswer {
let conn = &mut get_conn(pool).await?;

// fetch requested captcha
let captcha_exists = select(exists(
captcha_answer
.filter((uuid).eq(to_check.uuid))
.filter(lower(answer).eq(to_check.answer.to_lowercase().clone())),
))
.get_result::<bool>(conn)
.await?;
let captcha_exists =
select(exists(captcha_answer.find(to_check.uuid).filter(
lower(answer).eq(to_check.answer.to_lowercase().clone()),
)))
.get_result::<bool>(conn)
.await?;

// delete checked captcha
delete(captcha_answer.filter(uuid.eq(to_check.uuid)))
delete(captcha_answer.find(to_check.uuid))
.execute(conn)
.await?;

Expand Down
24 changes: 8 additions & 16 deletions crates/db_schema/src/impls/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,14 @@ impl Likeable for CommentLike {
}
async fn remove(
pool: &mut DbPool<'_>,
person_id_: PersonId,
comment_id_: CommentId,
person_id: PersonId,
comment_id: CommentId,
) -> Result<usize, Error> {
use crate::schema::comment_like::dsl::{comment_id, comment_like, person_id};
use crate::schema::comment_like::dsl::comment_like;
let conn = &mut get_conn(pool).await?;
diesel::delete(
comment_like
.filter(comment_id.eq(comment_id_))
.filter(person_id.eq(person_id_)),
)
.execute(conn)
.await
diesel::delete(comment_like.find((person_id, comment_id)))
.execute(conn)
.await
}
}

Expand All @@ -228,12 +224,10 @@ impl Saveable for CommentSaved {
pool: &mut DbPool<'_>,
comment_saved_form: &CommentSavedForm,
) -> Result<usize, Error> {
use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id};
use crate::schema::comment_saved::dsl::comment_saved;
let conn = &mut get_conn(pool).await?;
diesel::delete(
comment_saved
.filter(comment_id.eq(comment_saved_form.comment_id))
.filter(person_id.eq(comment_saved_form.person_id)),
comment_saved.find((comment_saved_form.person_id, comment_saved_form.comment_id)),
)
.execute(conn)
.await
Expand Down Expand Up @@ -349,7 +343,6 @@ mod tests {
let inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();

let expected_comment_like = CommentLike {
id: inserted_comment_like.id,
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
Expand All @@ -366,7 +359,6 @@ mod tests {
let inserted_comment_saved = CommentSaved::save(pool, &comment_saved_form).await.unwrap();

let expected_comment_saved = CommentSaved {
id: inserted_comment_saved.id,
comment_id: inserted_comment.id,
person_id: inserted_person.id,
published: inserted_comment_saved.published,
Expand Down
Loading

0 comments on commit 8e2cbc9

Please sign in to comment.