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 duplicate calendar events #456

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion apps/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ryot"
version = "3.3.4"
version = "3.3.5"
edition = "2021"
repository = "https://github.com/IgnisDa/ryot"
license = "GPL-V3"
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/entities/calendar_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Model {
pub id: i32,
pub date: NaiveDate,
pub metadata_id: Option<i32>,
pub metadata_extra_information: Option<String>,
pub metadata_extra_information: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
65 changes: 41 additions & 24 deletions apps/backend/src/miscellaneous/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,7 @@ impl MiscellaneousService {
}
};
}
SeenOrReviewOrCalendarEventExtraInformation::Other => {}
}
}
if image.is_none() {
Expand Down Expand Up @@ -3636,6 +3637,7 @@ impl MiscellaneousService {
SeenOrReviewOrCalendarEventExtraInformation::Podcast(d) => {
(None, None, Some(d.episode))
}
SeenOrReviewOrCalendarEventExtraInformation::Other => (None, None, None),
},
None => (None, None, None),
};
Expand Down Expand Up @@ -4324,6 +4326,9 @@ impl MiscellaneousService {
MediaSpecifics::Show(item) => {
ls.unique_items.shows.insert(seen.metadata_id);
match seen.extra_information.to_owned().unwrap() {
SeenOrReviewOrCalendarEventExtraInformation::Other => {
unreachable!()
}
SeenOrReviewOrCalendarEventExtraInformation::Podcast(_) => {
unreachable!()
}
Expand All @@ -4347,6 +4352,9 @@ impl MiscellaneousService {
MediaSpecifics::Podcast(item) => {
ls.unique_items.podcasts.insert(seen.metadata_id);
match seen.extra_information.to_owned().unwrap() {
SeenOrReviewOrCalendarEventExtraInformation::Other => {
unreachable!()
}
SeenOrReviewOrCalendarEventExtraInformation::Show(_) => {
unreachable!()
}
Expand Down Expand Up @@ -6379,33 +6387,36 @@ impl MiscellaneousService {
.await?
.unwrap();
let mut need_to_delete = false;
if let Some(ei) = &cal_event.metadata_extra_information {
let info = serde_json::from_str::<SeenOrReviewOrCalendarEventExtraInformation>(ei)
.unwrap();
match info {
SeenOrReviewOrCalendarEventExtraInformation::Show(show) => {
if let MediaSpecifics::Show(show_info) = meta.specifics {
if let Some((_, ep)) = show_info.get_episode(show.season, show.episode)
{
if ep.publish_date.unwrap() != cal_event.date {
need_to_delete = true;
}
let info = serde_json::from_str::<SeenOrReviewOrCalendarEventExtraInformation>(
&cal_event.metadata_extra_information,
)
.unwrap();
match info {
SeenOrReviewOrCalendarEventExtraInformation::Other => {
if cal_event.date != meta.publish_date.unwrap() {
need_to_delete = true;
}
}
SeenOrReviewOrCalendarEventExtraInformation::Show(show) => {
if let MediaSpecifics::Show(show_info) = meta.specifics {
if let Some((_, ep)) = show_info.get_episode(show.season, show.episode) {
if ep.publish_date.unwrap() != cal_event.date {
need_to_delete = true;
}
}
}
SeenOrReviewOrCalendarEventExtraInformation::Podcast(podcast) => {
if let MediaSpecifics::Podcast(podcast_info) = meta.specifics {
if let Some(ep) = podcast_info.get_episode(podcast.episode) {
if ep.publish_date != cal_event.date {
need_to_delete = true;
}
}
SeenOrReviewOrCalendarEventExtraInformation::Podcast(podcast) => {
if let MediaSpecifics::Podcast(podcast_info) = meta.specifics {
if let Some(ep) = podcast_info.get_episode(podcast.episode) {
if ep.publish_date != cal_event.date {
need_to_delete = true;
}
}
}
}
} else if cal_event.date != meta.publish_date.unwrap() {
need_to_delete = true;
}

if need_to_delete {
tracing::trace!(
"Need to delete calendar event id = {:#?} since it is invalid",
Expand Down Expand Up @@ -6433,7 +6444,7 @@ impl MiscellaneousService {
let event = calendar_event::ActiveModel {
metadata_id: ActiveValue::Set(Some(meta.id)),
date: ActiveValue::Set(episode.publish_date),
metadata_extra_information: ActiveValue::Set(Some(
metadata_extra_information: ActiveValue::Set(
serde_json::to_string(
&SeenOrReviewOrCalendarEventExtraInformation::Podcast(
SeenPodcastExtraInformation {
Expand All @@ -6442,7 +6453,7 @@ impl MiscellaneousService {
),
)
.unwrap(),
)),
),
..Default::default()
};
calendar_events_inserts.push(event);
Expand All @@ -6455,7 +6466,7 @@ impl MiscellaneousService {
let event = calendar_event::ActiveModel {
metadata_id: ActiveValue::Set(Some(meta.id)),
date: ActiveValue::Set(date),
metadata_extra_information: ActiveValue::Set(Some(
metadata_extra_information: ActiveValue::Set(
serde_json::to_string(
&SeenOrReviewOrCalendarEventExtraInformation::Show(
SeenShowExtraInformation {
Expand All @@ -6465,7 +6476,7 @@ impl MiscellaneousService {
),
)
.unwrap(),
)),
),
..Default::default()
};
calendar_events_inserts.push(event);
Expand All @@ -6477,7 +6488,12 @@ impl MiscellaneousService {
let event = calendar_event::ActiveModel {
metadata_id: ActiveValue::Set(Some(meta.id)),
date: ActiveValue::Set(meta.publish_date.unwrap()),
metadata_extra_information: ActiveValue::Set(None),
metadata_extra_information: ActiveValue::Set(
serde_json::to_string(
&SeenOrReviewOrCalendarEventExtraInformation::Other,
)
.unwrap(),
),
..Default::default()
};
calendar_events_inserts.push(event);
Expand Down Expand Up @@ -6617,6 +6633,7 @@ fn modify_seen_elements(all_seen: &mut [seen::Model]) {
all_seen.iter_mut().for_each(|s| {
if let Some(i) = s.extra_information.as_ref() {
match i {
SeenOrReviewOrCalendarEventExtraInformation::Other => {}
SeenOrReviewOrCalendarEventExtraInformation::Show(sea) => {
s.show_information = Some(sea.clone());
}
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,8 @@ pub mod media {
pub enum SeenOrReviewOrCalendarEventExtraInformation {
Show(SeenShowExtraInformation),
Podcast(SeenPodcastExtraInformation),
#[serde(other)]
Other,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ const ExerciseDisplay = (props: {
key={`${idx}`}
justify="space-between"
align="center"
bg={s.confirmed ? "gray" : undefined}
py={2}
py={4}
>
<Menu>
<Menu.Target>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use sea_orm::{
prelude::Date, ActiveModelBehavior, ActiveValue, ColumnTrait, DeriveEntityModel,
DerivePrimaryKey, DeriveRelation, EntityTrait, EnumIter, PrimaryKeyTrait, QueryFilter,
QuerySelect,
};
use sea_orm_migration::prelude::*;
use sea_query::Asterisk;
use serde::{Deserialize, Serialize};

use crate::migrations::m20230912_create_calendar_event::CalendarEvent;

#[derive(DeriveMigrationName)]
pub struct Migration;

mod ce {
use super::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "calendar_event")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub date: Date,
pub metadata_id: Option<i32>,
pub metadata_extra_information: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
let events = ce::Entity::find()
.select_only()
.column(ce::Column::Date)
.column(ce::Column::MetadataId)
.filter(ce::Column::MetadataExtraInformation.is_null())
.group_by(Expr::cust("date, metadata_id"))
.having(Expr::expr(Func::count(Expr::col(Asterisk))).gt(1))
.into_tuple::<(Date, i32)>()
.all(db)
.await?;
println!(
"\nFound {} duplicate calendar events. Removing them...",
events.len()
);
for evt in events {
let mut duplicate_events_group = ce::Entity::find()
.select_only()
.column(ce::Column::Id)
.filter(ce::Column::Date.eq(evt.0))
.filter(ce::Column::MetadataId.eq(evt.1))
.into_tuple::<i32>()
.all(db)
.await?;
duplicate_events_group.pop();
ce::Entity::delete_many()
.filter(ce::Column::Id.is_in(duplicate_events_group))
.exec(db)
.await?;
}
ce::Entity::update_many()
.filter(ce::Column::MetadataExtraInformation.is_null())
.set(ce::ActiveModel {
metadata_extra_information: ActiveValue::Set(r#""Other""#.to_owned()),
..Default::default()
})
.exec(db)
.await?;
manager
.alter_table(
Table::alter()
.table(CalendarEvent::Table)
.modify_column(
ColumnDef::new(CalendarEvent::MetadataExtraInformation).not_null(),
)
.to_owned(),
)
.await?;
println!("Operation successful!\n",);
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}
2 changes: 2 additions & 0 deletions libs/database/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod m20231016_create_collection_to_entity;
mod m20231017_create_user_to_entity;
mod m20231024_add_metadata_group_id_field_to_review;
mod m20231025_add_collection_id_field_to_review;
mod m20231025_remove_duplicate_calendar_events;

pub use m20230410_create_metadata::{Metadata as AliasedMetadata, MetadataLot, MetadataSource};
pub use m20230413_create_person::Person as AliasedPerson;
Expand Down Expand Up @@ -64,6 +65,7 @@ impl MigratorTrait for Migrator {
Box::new(m20231017_create_user_to_entity::Migration),
Box::new(m20231024_add_metadata_group_id_field_to_review::Migration),
Box::new(m20231025_add_collection_id_field_to_review::Migration),
Box::new(m20231025_remove_duplicate_calendar_events::Migration),
]
}
}