From 5b83045d17dd0e0ca4155c7a85b5c09e1683e0d2 Mon Sep 17 00:00:00 2001 From: Deokhaeng Lee Date: Thu, 14 Aug 2025 10:20:33 -0700 Subject: [PATCH 1/5] before branching --- plugins/apple-calendar/src/sync.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/apple-calendar/src/sync.rs b/plugins/apple-calendar/src/sync.rs index 56785d5abb..2b32595c00 100644 --- a/plugins/apple-calendar/src/sync.rs +++ b/plugins/apple-calendar/src/sync.rs @@ -150,6 +150,7 @@ async fn _sync_events( continue; } + if let Some(ref calendar_id) = db_event.calendar_id { if let Some(events) = system_events_per_selected_calendar.get(calendar_id) { // Check if event exists with same tracking_id @@ -269,6 +270,7 @@ async fn _sync_events( continue; } + // This is a genuinely new event let new_event = hypr_db_user::Event { id: uuid::Uuid::new_v4().to_string(), From 7619290fb0055dd3ac8ce845a14575ba9a6c9d11 Mon Sep 17 00:00:00 2001 From: Deokhaeng Lee Date: Thu, 14 Aug 2025 11:51:07 -0700 Subject: [PATCH 2/5] fixed is_recurring not being passed error --- apps/desktop/src-tauri/src/ext.rs | 4 +++- crates/db-user/src/events_ops.rs | 15 +++++++++++---- plugins/apple-calendar/src/sync.rs | 3 ++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/desktop/src-tauri/src/ext.rs b/apps/desktop/src-tauri/src/ext.rs index f62449a14b..e4eba22d3c 100644 --- a/apps/desktop/src-tauri/src/ext.rs +++ b/apps/desktop/src-tauri/src/ext.rs @@ -140,7 +140,9 @@ impl> AppExt for T { } #[cfg(debug_assertions)] - hypr_db_user::init::seed(user_db, &user_id).await.unwrap(); + { + //hypr_db_user::init::seed(user_db, &user_id).await.unwrap(); + } } #[cfg(target_os = "macos")] diff --git a/crates/db-user/src/events_ops.rs b/crates/db-user/src/events_ops.rs index 90e52d5a59..d451e142c4 100644 --- a/crates/db-user/src/events_ops.rs +++ b/crates/db-user/src/events_ops.rs @@ -40,7 +40,8 @@ impl UserDatabase { start_date = :start_date, end_date = :end_date, google_event_url = :google_event_url, - participants = :participants + participants = :participants, + is_recurring = :is_recurring WHERE id = :id RETURNING *", libsql::named_params! { @@ -53,6 +54,7 @@ impl UserDatabase { ":end_date": event.end_date.to_rfc3339(), ":google_event_url": event.google_event_url, ":participants": event.participants, + ":is_recurring": event.is_recurring, }, ) .await?; @@ -84,7 +86,8 @@ impl UserDatabase { start_date, end_date, google_event_url, - participants + participants, + is_recurring ) VALUES ( :id, :user_id, @@ -95,14 +98,16 @@ impl UserDatabase { :start_date, :end_date, :google_event_url, - :participants + :participants, + :is_recurring ) ON CONFLICT(tracking_id) DO UPDATE SET name = :name, note = :note, start_date = :start_date, end_date = :end_date, google_event_url = :google_event_url, - participants = :participants + participants = :participants, + is_recurring = :is_recurring RETURNING *", libsql::named_params! { ":id": event.id, @@ -115,6 +120,7 @@ impl UserDatabase { ":end_date": event.end_date.to_rfc3339(), ":google_event_url": event.google_event_url, ":participants": event.participants, + ":is_recurring": event.is_recurring, }, ) .await?; @@ -241,6 +247,7 @@ mod tests { end_date: chrono::Utc::now(), google_event_url: None, participants: None, + is_recurring: false, }; let event = db.upsert_event(event).await.unwrap(); diff --git a/plugins/apple-calendar/src/sync.rs b/plugins/apple-calendar/src/sync.rs index 2b32595c00..1b87eaa993 100644 --- a/plugins/apple-calendar/src/sync.rs +++ b/plugins/apple-calendar/src/sync.rs @@ -270,7 +270,6 @@ async fn _sync_events( continue; } - // This is a genuinely new event let new_event = hypr_db_user::Event { id: uuid::Uuid::new_v4().to_string(), @@ -288,6 +287,8 @@ async fn _sync_events( ), is_recurring: system_event.is_recurring, }; + + state.to_upsert.push(new_event); } } From 44e92dedd0c414db46165ea3b5c71ed177e37a5e Mon Sep 17 00:00:00 2001 From: Deokhaeng Lee Date: Thu, 14 Aug 2025 13:09:49 -0700 Subject: [PATCH 3/5] experiment 1 --- plugins/apple-calendar/src/sync.rs | 61 +++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/plugins/apple-calendar/src/sync.rs b/plugins/apple-calendar/src/sync.rs index 1b87eaa993..75dfa64f02 100644 --- a/plugins/apple-calendar/src/sync.rs +++ b/plugins/apple-calendar/src/sync.rs @@ -270,6 +270,44 @@ async fn _sync_events( continue; } + // Check for backward compatibility: recurring event replacing old non-recurring + if system_event.is_recurring { + // Look for old format event with session + if let Some((_, session)) = db_events_with_session.iter().find(|(db_event, session)| { + db_event.tracking_id == system_event.id && // Old format used base ID only + db_event.start_date == system_event.start_date && + db_event.name == system_event.name && + !db_event.is_recurring && // Was stored as non-recurring + session.is_some() // Has a session + }) { + // Store session ID for transfer after new event is created + if let Some(session) = session { + let new_event_id = uuid::Uuid::new_v4().to_string(); + state.session_transfers.push((session.id.clone(), new_event_id.clone())); + + let new_event = hypr_db_user::Event { + id: new_event_id, + tracking_id: composite_tracking_id, + user_id: user_id.clone(), + calendar_id: Some(db_calendar.id.clone()), + name: system_event.name.clone(), + note: system_event.note.clone(), + start_date: system_event.start_date, + end_date: system_event.end_date, + google_event_url: None, + participants: Some( + serde_json::to_string(&system_event.participants) + .unwrap_or_else(|_| "[]".to_string()), + ), + is_recurring: system_event.is_recurring, + }; + + state.to_upsert.push(new_event); + continue; + } + } + } + // This is a genuinely new event let new_event = hypr_db_user::Event { id: uuid::Uuid::new_v4().to_string(), @@ -501,6 +539,7 @@ struct EventSyncState { to_delete: Vec, to_upsert: Vec, to_update: Vec, + session_transfers: Vec<(String, String)>, // (session_id, new_event_id) } impl CalendarSyncState { @@ -521,21 +560,31 @@ impl CalendarSyncState { impl EventSyncState { async fn execute(self, db: &hypr_db_user::UserDatabase) { - for event in self.to_delete { - if let Err(e) = db.delete_event(&event.id).await { - tracing::error!("delete_event_error: {}", e); + // 1. Create new events first + for event in self.to_upsert { + if let Err(e) = db.upsert_event(event).await { + tracing::error!("upsert_event_error: {}", e); } } + // 2. Update existing events for event in self.to_update { if let Err(e) = db.update_event(event).await { tracing::error!("update_event_error: {}", e); } } - for event in self.to_upsert { - if let Err(e) = db.upsert_event(event).await { - tracing::error!("upsert_event_error: {}", e); + // 3. Transfer sessions from old events to new events + for (session_id, new_event_id) in self.session_transfers { + if let Err(e) = db.session_set_event(session_id, Some(new_event_id)).await { + tracing::error!("session_transfer_error: {}", e); + } + } + + // 4. Delete old events last (after sessions have been transferred) + for event in self.to_delete { + if let Err(e) = db.delete_event(&event.id).await { + tracing::error!("delete_event_error: {}", e); } } } From 61b90c0703a542823728a486081caae65d867a02 Mon Sep 17 00:00:00 2001 From: Deokhaeng Lee Date: Thu, 14 Aug 2025 13:10:19 -0700 Subject: [PATCH 4/5] ran tests --- plugins/apple-calendar/src/sync.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/plugins/apple-calendar/src/sync.rs b/plugins/apple-calendar/src/sync.rs index 75dfa64f02..679ae38c28 100644 --- a/plugins/apple-calendar/src/sync.rs +++ b/plugins/apple-calendar/src/sync.rs @@ -150,7 +150,6 @@ async fn _sync_events( continue; } - if let Some(ref calendar_id) = db_event.calendar_id { if let Some(events) = system_events_per_selected_calendar.get(calendar_id) { // Check if event exists with same tracking_id @@ -273,18 +272,22 @@ async fn _sync_events( // Check for backward compatibility: recurring event replacing old non-recurring if system_event.is_recurring { // Look for old format event with session - if let Some((_, session)) = db_events_with_session.iter().find(|(db_event, session)| { - db_event.tracking_id == system_event.id && // Old format used base ID only + if let Some((_, session)) = + db_events_with_session.iter().find(|(db_event, session)| { + db_event.tracking_id == system_event.id && // Old format used base ID only db_event.start_date == system_event.start_date && db_event.name == system_event.name && !db_event.is_recurring && // Was stored as non-recurring - session.is_some() // Has a session - }) { + session.is_some() // Has a session + }) + { // Store session ID for transfer after new event is created if let Some(session) = session { let new_event_id = uuid::Uuid::new_v4().to_string(); - state.session_transfers.push((session.id.clone(), new_event_id.clone())); - + state + .session_transfers + .push((session.id.clone(), new_event_id.clone())); + let new_event = hypr_db_user::Event { id: new_event_id, tracking_id: composite_tracking_id, @@ -301,7 +304,7 @@ async fn _sync_events( ), is_recurring: system_event.is_recurring, }; - + state.to_upsert.push(new_event); continue; } @@ -326,7 +329,6 @@ async fn _sync_events( is_recurring: system_event.is_recurring, }; - state.to_upsert.push(new_event); } } From c8f13a6ad6751bf4fab611157883633e26b1f7be Mon Sep 17 00:00:00 2001 From: Deokhaeng Lee Date: Thu, 14 Aug 2025 13:11:55 -0700 Subject: [PATCH 5/5] uncommented --- apps/desktop/src-tauri/src/ext.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/desktop/src-tauri/src/ext.rs b/apps/desktop/src-tauri/src/ext.rs index e4eba22d3c..f62449a14b 100644 --- a/apps/desktop/src-tauri/src/ext.rs +++ b/apps/desktop/src-tauri/src/ext.rs @@ -140,9 +140,7 @@ impl> AppExt for T { } #[cfg(debug_assertions)] - { - //hypr_db_user::init::seed(user_db, &user_id).await.unwrap(); - } + hypr_db_user::init::seed(user_db, &user_id).await.unwrap(); } #[cfg(target_os = "macos")]