From 6935acba2ffbdf887b02e2701486cfacc84fccaa Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Mon, 8 Apr 2024 23:14:03 +0200 Subject: [PATCH] Remove local_site_id from tagline and custom_emoji --- crates/api/src/site/leave_admin.rs | 2 +- crates/api_crud/src/custom_emoji/create.rs | 3 -- crates/api_crud/src/custom_emoji/list.rs | 13 ++------ crates/api_crud/src/custom_emoji/update.rs | 3 -- crates/api_crud/src/site/read.rs | 2 +- crates/api_crud/src/tagline/create.rs | 5 +-- crates/api_crud/src/tagline/list.rs | 11 ++----- crates/db_schema/src/impls/tagline.rs | 12 ++----- crates/db_schema/src/schema.rs | 4 --- crates/db_schema/src/source/custom_emoji.rs | 14 ++------ crates/db_schema/src/source/tagline.rs | 12 +------ crates/db_views/src/custom_emoji_view.rs | 11 ++----- .../down.sql | 32 +++++++++++++++++++ .../up.sql | 6 ++++ 14 files changed, 54 insertions(+), 76 deletions(-) create mode 100644 migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql create mode 100644 migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index fa91c726f..57a134238 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -61,7 +61,7 @@ pub async fn leave_admin( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; - let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?; + let tagline = Tagline::get_random(&mut context.pool()).await?; Ok(Json(GetSiteResponse { site_view, diff --git a/crates/api_crud/src/custom_emoji/create.rs b/crates/api_crud/src/custom_emoji/create.rs index 54745577a..5846c1243 100644 --- a/crates/api_crud/src/custom_emoji/create.rs +++ b/crates/api_crud/src/custom_emoji/create.rs @@ -9,7 +9,6 @@ use lemmy_db_schema::{ source::{ custom_emoji::{CustomEmoji, CustomEmojiInsertForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, - local_site::LocalSite, }, traits::Crud, }; @@ -22,12 +21,10 @@ pub async fn create_custom_emoji( context: Data, local_user_view: LocalUserView, ) -> Result, LemmyError> { - let local_site = LocalSite::read(&mut context.pool()).await?; // Make sure user is an admin is_admin(&local_user_view)?; let emoji_form = CustomEmojiInsertForm::builder() - .local_site_id(local_site.id) .shortcode(data.shortcode.to_lowercase().trim().to_string()) .alt_text(data.alt_text.to_string()) .category(data.category.to_string()) diff --git a/crates/api_crud/src/custom_emoji/list.rs b/crates/api_crud/src/custom_emoji/list.rs index dab3e8823..c60a03ed4 100644 --- a/crates/api_crud/src/custom_emoji/list.rs +++ b/crates/api_crud/src/custom_emoji/list.rs @@ -3,7 +3,7 @@ use lemmy_api_common::{ context::LemmyContext, custom_emoji::{ListCustomEmojis, ListCustomEmojisResponse}, }; -use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; +use lemmy_db_views::structs::{CustomEmojiView, LocalUserView}; use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] @@ -12,15 +12,8 @@ pub async fn list_custom_emojis( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let local_site = SiteView::read_local(&mut context.pool()).await?; - let custom_emojis = CustomEmojiView::list( - &mut context.pool(), - local_site.local_site.id, - &data.category, - data.page, - data.limit, - ) - .await?; + let custom_emojis = + CustomEmojiView::list(&mut context.pool(), &data.category, data.page, data.limit).await?; Ok(Json(ListCustomEmojisResponse { custom_emojis })) } diff --git a/crates/api_crud/src/custom_emoji/update.rs b/crates/api_crud/src/custom_emoji/update.rs index 0cabf77b9..4119ed944 100644 --- a/crates/api_crud/src/custom_emoji/update.rs +++ b/crates/api_crud/src/custom_emoji/update.rs @@ -9,7 +9,6 @@ use lemmy_db_schema::{ source::{ custom_emoji::{CustomEmoji, CustomEmojiUpdateForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, - local_site::LocalSite, }, traits::Crud, }; @@ -22,12 +21,10 @@ pub async fn update_custom_emoji( context: Data, local_user_view: LocalUserView, ) -> Result, LemmyError> { - let local_site = LocalSite::read(&mut context.pool()).await?; // Make sure user is an admin is_admin(&local_user_view)?; let emoji_form = CustomEmojiUpdateForm::builder() - .local_site_id(local_site.id) .alt_text(data.alt_text.to_string()) .category(data.category.to_string()) .image_url(data.clone().image_url.into()) diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 90e04946e..bbead78a0 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -46,7 +46,7 @@ pub async fn get_site( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; - let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?; + let tagline = Tagline::get_random(&mut context.pool()).await?; Ok(GetSiteResponse { site_view, admins, diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 0b33a03f9..50bb50c12 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -31,10 +31,7 @@ pub async fn create_tagline( let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?; is_valid_tagline_content(&content)?; - let tagline_form = TaglineInsertForm { - local_site_id: local_site.id, - content, - }; + let tagline_form = TaglineInsertForm { content }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; diff --git a/crates/api_crud/src/tagline/list.rs b/crates/api_crud/src/tagline/list.rs index d8f067ef1..21929f547 100644 --- a/crates/api_crud/src/tagline/list.rs +++ b/crates/api_crud/src/tagline/list.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ tagline::{ListTaglines, ListTaglinesResponse}, }; use lemmy_db_schema::source::tagline::Tagline; -use lemmy_db_views::structs::{LocalUserView, SiteView}; +use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] @@ -13,14 +13,7 @@ pub async fn list_taglines( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let local_site = SiteView::read_local(&mut context.pool()).await?; - let taglines = Tagline::list( - &mut context.pool(), - local_site.local_site.id, - data.page, - data.limit, - ) - .await?; + let taglines = Tagline::list(&mut context.pool(), data.page, data.limit).await?; Ok(Json(ListTaglinesResponse { taglines })) } diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 1bd755562..656d537d6 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -1,6 +1,6 @@ use crate::{ - newtypes::{LocalSiteId, TaglineId}, - schema::tagline::dsl::{local_site_id, published, tagline}, + newtypes::TaglineId, + schema::tagline::dsl::{published, tagline}, source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm}, traits::Crud, utils::{get_conn, limit_and_offset, DbPool}, @@ -38,7 +38,6 @@ impl Crud for Tagline { impl Tagline { pub async fn list( pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, page: Option, limit: Option, ) -> Result, Error> { @@ -48,21 +47,16 @@ impl Tagline { .order(published.desc()) .offset(offset) .limit(limit) - .filter(local_site_id.eq(for_local_site_id)) .get_results::(conn) .await } - pub async fn get_random( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - ) -> Result, Error> { + pub async fn get_random(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; sql_function!(fn random() -> Text); tagline .order(random()) .limit(1) - .filter(local_site_id.eq(for_local_site_id)) .first::(conn) .await .optional() diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 408ed0540..dca1cac48 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -254,7 +254,6 @@ diesel::table! { diesel::table! { custom_emoji (id) { id -> Int4, - local_site_id -> Int4, #[max_length = 128] shortcode -> Varchar, image_url -> Text, @@ -929,7 +928,6 @@ diesel::table! { diesel::table! { tagline (id) { id -> Int4, - local_site_id -> Int4, content -> Text, published -> Timestamptz, updated -> Nullable, @@ -966,7 +964,6 @@ diesel::joinable!(community_moderator -> community (community_id)); diesel::joinable!(community_moderator -> person (person_id)); diesel::joinable!(community_person_ban -> community (community_id)); diesel::joinable!(community_person_ban -> person (person_id)); -diesel::joinable!(custom_emoji -> local_site (local_site_id)); diesel::joinable!(custom_emoji_keyword -> custom_emoji (custom_emoji_id)); diesel::joinable!(email_verification -> local_user (local_user_id)); diesel::joinable!(federation_allowlist -> instance (instance_id)); @@ -1028,7 +1025,6 @@ diesel::joinable!(site -> instance (instance_id)); diesel::joinable!(site_aggregates -> site (site_id)); diesel::joinable!(site_language -> language (language_id)); diesel::joinable!(site_language -> site (site_id)); -diesel::joinable!(tagline -> local_site (local_site_id)); diesel::allow_tables_to_appear_in_same_query!( admin_purge_comment, diff --git a/crates/db_schema/src/source/custom_emoji.rs b/crates/db_schema/src/source/custom_emoji.rs index 3217c9736..788885c97 100644 --- a/crates/db_schema/src/source/custom_emoji.rs +++ b/crates/db_schema/src/source/custom_emoji.rs @@ -1,4 +1,4 @@ -use crate::newtypes::{CustomEmojiId, DbUrl, LocalSiteId}; +use crate::newtypes::{CustomEmojiId, DbUrl}; #[cfg(feature = "full")] use crate::schema::custom_emoji; use chrono::{DateTime, Utc}; @@ -10,21 +10,13 @@ use typed_builder::TypedBuilder; #[skip_serializing_none] #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] -#[cfg_attr( - feature = "full", - derive(Queryable, Selectable, Associations, Identifiable, TS) -)] +#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))] #[cfg_attr(feature = "full", diesel(table_name = custom_emoji))] -#[cfg_attr( - feature = "full", - diesel(belongs_to(crate::source::local_site::LocalSite)) -)] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "full", ts(export))] /// A custom emoji. pub struct CustomEmoji { pub id: CustomEmojiId, - pub local_site_id: LocalSiteId, pub shortcode: String, pub image_url: DbUrl, pub alt_text: String, @@ -37,7 +29,6 @@ pub struct CustomEmoji { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = custom_emoji))] pub struct CustomEmojiInsertForm { - pub local_site_id: LocalSiteId, pub shortcode: String, pub image_url: DbUrl, pub alt_text: String, @@ -48,7 +39,6 @@ pub struct CustomEmojiInsertForm { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = custom_emoji))] pub struct CustomEmojiUpdateForm { - pub local_site_id: LocalSiteId, pub image_url: DbUrl, pub alt_text: String, pub category: String, diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index df207e10e..ac6726601 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -1,4 +1,3 @@ -use crate::newtypes::LocalSiteId; #[cfg(feature = "full")] use crate::schema::tagline; use chrono::{DateTime, Utc}; @@ -9,21 +8,13 @@ use ts_rs::TS; #[skip_serializing_none] #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] -#[cfg_attr( - feature = "full", - derive(Queryable, Selectable, Associations, Identifiable, TS) -)] +#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] -#[cfg_attr( - feature = "full", - diesel(belongs_to(crate::source::local_site::LocalSite)) -)] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "full", ts(export))] /// A tagline, shown at the top of your site. pub struct Tagline { pub id: i32, - pub local_site_id: LocalSiteId, pub content: String, pub published: DateTime, pub updated: Option>, @@ -33,7 +24,6 @@ pub struct Tagline { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineInsertForm { - pub local_site_id: LocalSiteId, pub content: String, } diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 70dfb31e5..8c17190a2 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -2,7 +2,7 @@ use crate::structs::CustomEmojiView; use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - newtypes::{CustomEmojiId, LocalSiteId}, + newtypes::CustomEmojiId, schema::{custom_emoji, custom_emoji_keyword}, source::{custom_emoji::CustomEmoji, custom_emoji_keyword::CustomEmojiKeyword}, utils::{get_conn, limit_and_offset, DbPool}, @@ -35,13 +35,9 @@ impl CustomEmojiView { } } - pub async fn get_all( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - ) -> Result, Error> { + pub async fn get_all(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; let emojis = custom_emoji::table - .filter(custom_emoji::local_site_id.eq(for_local_site_id)) .left_join( custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)), ) @@ -59,7 +55,6 @@ impl CustomEmojiView { pub async fn list( pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, category: &Option, page: Option, limit: Option, @@ -72,8 +67,6 @@ impl CustomEmojiView { ) .into_boxed(); - query = query.filter(custom_emoji::local_site_id.eq(for_local_site_id)); - if let Some(category) = category { query = query.filter(custom_emoji::category.eq(category)) } diff --git a/migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql new file mode 100644 index 000000000..a6b01a1d1 --- /dev/null +++ b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql @@ -0,0 +1,32 @@ +ALTER TABLE custom_emoji + ADD COLUMN local_site_id int REFERENCES local_site (site_id) ON UPDATE CASCADE ON DELETE CASCADE; + +UPDATE + custom_emoji +SET + local_site_id = ( + SELECT + site_id + FROM + local_site + LIMIT 1); + +ALTER TABLE custom_emoji + ALTER COLUMN local_site_id SET NOT NULL; + +ALTER TABLE tagline + ADD COLUMN local_site_id int REFERENCES local_site (site_id) ON UPDATE CASCADE ON DELETE CASCADE; + +UPDATE + tagline +SET + local_site_id = ( + SELECT + site_id + FROM + local_site + LIMIT 1); + +ALTER TABLE tagline + ALTER COLUMN local_site_id SET NOT NULL; + diff --git a/migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql new file mode 100644 index 000000000..5aa073f76 --- /dev/null +++ b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql @@ -0,0 +1,6 @@ +ALTER TABLE custom_emoji + DROP COLUMN local_site_id; + +ALTER TABLE tagline + DROP COLUMN local_site_id; +