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

Remove id column and use different primary key on some tables #4093

Merged
merged 46 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
4601027
post_saved
dullbananas Oct 24, 2023
67c97e3
Merge branch 'main' into primary-keys
dullbananas Oct 24, 2023
3eb0ccc
Merge branch 'main' into primary-keys
dullbananas Oct 24, 2023
73c7ac0
fmt
dullbananas Oct 25, 2023
afb4851
remove unique and not null
dullbananas Oct 28, 2023
327b836
Merge remote-tracking branch 'upstream/main' into primary-keys
dullbananas Oct 28, 2023
486396b
put person_id first in primary key and remove index
dullbananas Oct 28, 2023
8bcb3bb
use post_saved.find
dullbananas Oct 28, 2023
ab31355
change captcha_answer
dullbananas Oct 28, 2023
924900d
remove removal of not null
dullbananas Oct 28, 2023
41f0270
comment_aggregates
dullbananas Oct 28, 2023
c321bcc
comment_like
dullbananas Oct 28, 2023
e6e3748
comment_saved
dullbananas Oct 28, 2023
b350775
aggregates
dullbananas Oct 28, 2023
2df2758
remove "\"
dullbananas Oct 28, 2023
b52bdec
deduplicate site_aggregates
dullbananas Oct 28, 2023
585a651
person_post_aggregates
dullbananas Oct 29, 2023
68f517b
community_moderator
dullbananas Oct 29, 2023
e0a5ca6
community_block
dullbananas Oct 29, 2023
2cc6552
community_person_ban
dullbananas Oct 29, 2023
53cb31c
custom_emoji_keyword
dullbananas Oct 29, 2023
861326e
federation allow/block list
dullbananas Oct 29, 2023
17e870c
federation_queue_state
dullbananas Oct 29, 2023
0b7cbb4
instance_block
dullbananas Oct 29, 2023
c8386f9
local_site_rate_limit, local_user_language, login_token
dullbananas Oct 29, 2023
870d96c
person_ban, person_block, person_follower, post_like, post_read, rece…
dullbananas Oct 30, 2023
78795e9
community_follower, community_language, site_language
dullbananas Nov 2, 2023
f949ca8
Merge remote-tracking branch 'upstream/main' into primary-keys
dullbananas Nov 2, 2023
201ba64
fmt
dullbananas Nov 4, 2023
d239c35
image_upload
dullbananas Nov 4, 2023
2833b15
remove unused newtypes
dullbananas Nov 4, 2023
fb1fa2e
remove more indexes
dullbananas Nov 4, 2023
fb345db
use .find
dullbananas Nov 4, 2023
44555e9
merge
dullbananas Nov 4, 2023
91cea0b
Merge branch 'main' into primary-keys
dullbananas Nov 4, 2023
b5d97b7
fix site_aggregates_site function
dullbananas Nov 4, 2023
260e314
fmt
dullbananas Nov 4, 2023
a9d0668
Primary keys dess (#17)
dessalines Nov 7, 2023
a3ba0eb
Merge branch 'main' into primary-keys
dullbananas Nov 7, 2023
671ea17
fmt
dullbananas Nov 7, 2023
9822771
Merge branch 'main' into primary-keys
dullbananas Nov 8, 2023
175660a
Merge branch 'main' into primary-keys
dullbananas Nov 9, 2023
e6d1674
Update community_block.rs
dullbananas Nov 9, 2023
3aafbd2
Update instance_block.rs
dullbananas Nov 9, 2023
ae951ea
Update person_block.rs
dullbananas Nov 9, 2023
ccd5ad0
Update person_block.rs
dullbananas Nov 9, 2023
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
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