Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

refactor(calendar): drop the email ICS source model and schema (#5483)

gbirmanf35101cecabe
diff --git a/.sqlx/query-ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d.json b/.sqlx/query-4d119454360426cdf88d2df06f194193029a754d47241203f405298052185c46.json
similarity index 52%
rename from .sqlx/query-ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d.json
rename to .sqlx/query-4d119454360426cdf88d2df06f194193029a754d47241203f405298052185c46.json
index f0b682becd..848298e39e 100644
--- a/.sqlx/query-ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d.json
+++ b/.sqlx/query-4d119454360426cdf88d2df06f194193029a754d47241203f405298052185c46.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT event_id, normalized_payload\n FROM calendar_event_sources\n WHERE source_kind = 'google'\n AND account_id = $1\n AND calendar_id = $2\n AND provider_event_id = $3\n ",
+ "query": "\n SELECT event_id, normalized_payload\n FROM calendar_event_sources\n WHERE source_kind = 'google'\n AND account_id = $1\n AND calendar_id = $2\n AND provider_event_id = $3\n ",
"describe": {
"columns": [
{
@@ -26,5 +26,5 @@
false
]
},
- "hash": "ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d"
+ "hash": "4d119454360426cdf88d2df06f194193029a754d47241203f405298052185c46"
}
diff --git a/.sqlx/query-c65dbb2fce28734477e2c1e40d3a0f0b49e75e41bceb0133b74794fbe2144ce7.json b/.sqlx/query-c65dbb2fce28734477e2c1e40d3a0f0b49e75e41bceb0133b74794fbe2144ce7.json
deleted file mode 100644
index 554d3124f4..0000000000
--- a/.sqlx/query-c65dbb2fce28734477e2c1e40d3a0f0b49e75e41bceb0133b74794fbe2144ce7.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO calendar_event_sources (\n id, event_id, source_link_id, source_kind, email_link_id, email_thread_id,\n email_message_id, email_attachment_id, content_hash,\n raw_payload, source_sequence, source_updated_at, normalized_payload\n )\n VALUES (\n $1, $2, $3, 'email_ics', $3, $4, $5, $6, $7, $8,\n $9, $10, $11\n )\n ON CONFLICT (\n email_link_id,\n email_message_id,\n COALESCE(email_attachment_id, ''),\n content_hash,\n event_id\n ) WHERE source_kind = 'email_ics'\n DO UPDATE SET\n raw_payload = EXCLUDED.raw_payload,\n source_sequence = EXCLUDED.source_sequence,\n source_updated_at = EXCLUDED.source_updated_at,\n normalized_payload = EXCLUDED.normalized_payload,\n last_seen_at = now()\n WHERE\n EXCLUDED.source_sequence > calendar_event_sources.source_sequence\n OR (\n EXCLUDED.source_sequence = calendar_event_sources.source_sequence\n AND EXCLUDED.source_updated_at >= calendar_event_sources.source_updated_at\n )\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Uuid",
- "Uuid",
- "Uuid",
- "Text",
- "Text",
- "Jsonb",
- "Int4",
- "Timestamptz",
- "Jsonb"
- ]
- },
- "nullable": []
- },
- "hash": "c65dbb2fce28734477e2c1e40d3a0f0b49e75e41bceb0133b74794fbe2144ce7"
-}
diff --git a/.sqlx/query-d34ab0c8dd9fb917c9978a4b861ee510d52c4b7d3d71b995888e0948ec570e86.json b/.sqlx/query-e9304c505cfe92a0198d8b5fd32f0cfa88f9ac86a5b57a20411da13de0114b83.json
similarity index 67%
rename from .sqlx/query-d34ab0c8dd9fb917c9978a4b861ee510d52c4b7d3d71b995888e0948ec570e86.json
rename to .sqlx/query-e9304c505cfe92a0198d8b5fd32f0cfa88f9ac86a5b57a20411da13de0114b83.json
index 2ead3f42da..400a3aea75 100644
--- a/.sqlx/query-d34ab0c8dd9fb917c9978a4b861ee510d52c4b7d3d71b995888e0948ec570e86.json
+++ b/.sqlx/query-e9304c505cfe92a0198d8b5fd32f0cfa88f9ac86a5b57a20411da13de0114b83.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO calendar_events (\n id, owner_id, source_link_id, ical_uid, title, description, location,\n status, visibility, transparency,\n starts_at, ends_at, start_date, end_date, time_zone,\n recurrence_lines, organizer_email, organizer_name,\n conference_url, sequence, is_read_only, canonical_source_kind,\n canonical_source_updated_at,\n created_at, updated_at\n )\n VALUES (\n $1, $2, $3, $4, $5, $6, $7,\n $8, $9, $10,\n $11, $12, $13, $14, $15,\n $16, $17, $18,\n $19, $20, $21, $22, $24,\n $23, $24\n )\n ON CONFLICT (owner_id, source_link_id, ical_uid) DO UPDATE SET\n title = EXCLUDED.title,\n description = EXCLUDED.description,\n location = EXCLUDED.location,\n status = EXCLUDED.status,\n visibility = EXCLUDED.visibility,\n transparency = EXCLUDED.transparency,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n start_date = EXCLUDED.start_date,\n end_date = EXCLUDED.end_date,\n time_zone = EXCLUDED.time_zone,\n recurrence_lines = EXCLUDED.recurrence_lines,\n organizer_email = EXCLUDED.organizer_email,\n organizer_name = EXCLUDED.organizer_name,\n conference_url = EXCLUDED.conference_url,\n sequence = EXCLUDED.sequence,\n is_read_only = EXCLUDED.is_read_only,\n canonical_source_kind = EXCLUDED.canonical_source_kind,\n canonical_source_updated_at = EXCLUDED.canonical_source_updated_at,\n updated_at = GREATEST(calendar_events.updated_at, EXCLUDED.updated_at)\n WHERE\n (\n $25 = 'google'\n AND (\n EXCLUDED.sequence > calendar_events.sequence\n OR (\n EXCLUDED.sequence = calendar_events.sequence\n AND EXCLUDED.canonical_source_updated_at\n >= calendar_events.canonical_source_updated_at\n )\n OR calendar_events.canonical_source_kind <> 'google'\n )\n )\n OR (\n $25 = 'email_ics'\n AND calendar_events.canonical_source_kind <> 'google'\n AND (\n EXCLUDED.sequence > calendar_events.sequence\n OR (\n EXCLUDED.sequence = calendar_events.sequence\n AND EXCLUDED.canonical_source_updated_at\n >= calendar_events.canonical_source_updated_at\n )\n )\n )\n RETURNING id\n ",
+ "query": "\n INSERT INTO calendar_events (\n id, owner_id, source_link_id, ical_uid, title, description, location,\n status, visibility, transparency,\n starts_at, ends_at, start_date, end_date, time_zone,\n recurrence_lines, organizer_email, organizer_name,\n conference_url, sequence, is_read_only, canonical_source_kind,\n canonical_source_updated_at,\n created_at, updated_at\n )\n VALUES (\n $1, $2, $3, $4, $5, $6, $7,\n $8, $9, $10,\n $11, $12, $13, $14, $15,\n $16, $17, $18,\n $19, $20, $21, $22, $24,\n $23, $24\n )\n ON CONFLICT (owner_id, source_link_id, ical_uid) DO UPDATE SET\n title = EXCLUDED.title,\n description = EXCLUDED.description,\n location = EXCLUDED.location,\n status = EXCLUDED.status,\n visibility = EXCLUDED.visibility,\n transparency = EXCLUDED.transparency,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n start_date = EXCLUDED.start_date,\n end_date = EXCLUDED.end_date,\n time_zone = EXCLUDED.time_zone,\n recurrence_lines = EXCLUDED.recurrence_lines,\n organizer_email = EXCLUDED.organizer_email,\n organizer_name = EXCLUDED.organizer_name,\n conference_url = EXCLUDED.conference_url,\n sequence = EXCLUDED.sequence,\n is_read_only = EXCLUDED.is_read_only,\n canonical_source_kind = EXCLUDED.canonical_source_kind,\n canonical_source_updated_at = EXCLUDED.canonical_source_updated_at,\n updated_at = GREATEST(calendar_events.updated_at, EXCLUDED.updated_at)\n WHERE\n EXCLUDED.sequence > calendar_events.sequence\n OR (\n EXCLUDED.sequence = calendar_events.sequence\n AND EXCLUDED.canonical_source_updated_at\n >= calendar_events.canonical_source_updated_at\n )\n RETURNING id\n ",
"describe": {
"columns": [
{
@@ -34,13 +34,12 @@
"Bool",
"Text",
"Timestamptz",
- "Timestamptz",
- "Text"
+ "Timestamptz"
]
},
"nullable": [
false
]
},
- "hash": "d34ab0c8dd9fb917c9978a4b861ee510d52c4b7d3d71b995888e0948ec570e86"
+ "hash": "e9304c505cfe92a0198d8b5fd32f0cfa88f9ac86a5b57a20411da13de0114b83"
}
diff --git a/.sqlx/query-c7eb1a0ad7368e30d78b27d81884e96846e1fdf6adf018274c3df850a7592d2c.json b/.sqlx/query-ffcd32ca5bbb5d45a9d336b96e9f16ca85e634ad035b7b7f580de56304adc3e9.json
similarity index 66%
rename from .sqlx/query-c7eb1a0ad7368e30d78b27d81884e96846e1fdf6adf018274c3df850a7592d2c.json
rename to .sqlx/query-ffcd32ca5bbb5d45a9d336b96e9f16ca85e634ad035b7b7f580de56304adc3e9.json
index 78e7bc70ce..64769f748c 100644
--- a/.sqlx/query-c7eb1a0ad7368e30d78b27d81884e96846e1fdf6adf018274c3df850a7592d2c.json
+++ b/.sqlx/query-ffcd32ca5bbb5d45a9d336b96e9f16ca85e634ad035b7b7f580de56304adc3e9.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT source_kind, source_updated_at, normalized_payload\n FROM calendar_event_sources\n WHERE event_id = $1\n ORDER BY\n CASE source_kind WHEN 'google' THEN 0 ELSE 1 END,\n source_sequence DESC,\n source_updated_at DESC,\n last_seen_at DESC,\n id DESC\n LIMIT 1\n ",
+ "query": "\n SELECT source_kind, source_updated_at, normalized_payload\n FROM calendar_event_sources\n WHERE event_id = $1\n ORDER BY\n source_sequence DESC,\n source_updated_at DESC,\n last_seen_at DESC,\n id DESC\n LIMIT 1\n ",
"describe": {
"columns": [
{
@@ -30,5 +30,5 @@
false
]
},
- "hash": "c7eb1a0ad7368e30d78b27d81884e96846e1fdf6adf018274c3df850a7592d2c"
+ "hash": "ffcd32ca5bbb5d45a9d336b96e9f16ca85e634ad035b7b7f580de56304adc3e9"
}
diff --git a/crates/calendar_events/src/domain/models.rs b/crates/calendar_events/src/domain/models.rs
index 698e39e077..cd66bb6c81 100644
--- a/crates/calendar_events/src/domain/models.rs
+++ b/crates/calendar_events/src/domain/models.rs
@@ -479,23 +479,6 @@ fn month_ceil(instant: DateTime<Utc>) -> DateTime<Utc> {
#[cfg(test)]
mod test;
-/// Email attachment/message identity for an iCalendar source.
-#[derive(Clone, Debug, PartialEq, Eq)]
-pub struct EmailIcsSource {
- /// Connected inbox.
- pub email_link_id: Uuid,
- /// Email thread containing the invitation.
- pub email_thread_id: Option<Uuid>,
- /// Email message containing the invitation.
- pub email_message_id: Uuid,
- /// Provider attachment identifier, or `None` for inline calendar MIME parts.
- pub email_attachment_id: Option<String>,
- /// SHA-256 of the source bytes.
- pub content_hash: String,
- /// Source payload retained for reconciliation/debugging.
- pub raw_payload: serde_json::Value,
-}
-
/// Google provider identity for an event source.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GoogleEventSource {
@@ -518,8 +501,6 @@ pub struct GoogleEventSource {
/// Source-specific metadata attached to a canonical event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CalendarEventSource {
- /// Event found in an email iCalendar part.
- EmailIcs(EmailIcsSource),
/// Event fetched from Google Calendar.
Google(GoogleEventSource),
}
diff --git a/crates/calendar_events/src/domain/ports.rs b/crates/calendar_events/src/domain/ports.rs
index e74befb08b..c18265456b 100644
--- a/crates/calendar_events/src/domain/ports.rs
+++ b/crates/calendar_events/src/domain/ports.rs
@@ -75,8 +75,6 @@ pub struct GoogleEventSyncContext {
/// Authorized ingestion command for one normalized calendar event.
pub enum CalendarEventWrite {
- /// Event extracted from a connected inbox's RFC 5545 content.
- EmailIcs(CalendarEventUpsert),
/// Google event written while holding a durable backfill lease.
GoogleBackfill {
/// Durable job and connected-inbox identity.
diff --git a/crates/calendar_events/src/domain/service.rs b/crates/calendar_events/src/domain/service.rs
index cf8dfa7410..31815d8ced 100644
--- a/crates/calendar_events/src/domain/service.rs
+++ b/crates/calendar_events/src/domain/service.rs
@@ -66,23 +66,6 @@ where
.await
}
- /// Validate and atomically reconcile a provider or email source.
- #[tracing::instrument(skip(self, upsert), fields(ical_uid = %upsert.event.ical_uid), err)]
- pub async fn upsert_email_event(&self, upsert: CalendarEventUpsert) -> Result<Uuid, Report> {
- validate_upsert(&upsert)?;
- if !matches!(
- upsert.source,
- super::models::CalendarEventSource::EmailIcs(_)
- ) {
- return Err(rootcause::report!(
- "unfenced calendar ingestion only accepts email ICS sources"
- ));
- }
- self.repository
- .upsert_event(CalendarEventWrite::EmailIcs(upsert))
- .await
- }
-
/// Query a bounded occurrence viewport.
#[tracing::instrument(skip(self, requester_id, range), err)]
pub async fn list_occurrences(
@@ -509,9 +492,8 @@ where
);
continue;
}
- if let super::models::CalendarEventSource::Google(source) = &upsert.source {
- debug_assert_eq!(source.calendar_id, calendar_id);
- }
+ let super::models::CalendarEventSource::Google(source) = &upsert.source;
+ debug_assert_eq!(source.calendar_id, calendar_id);
self.repository
.upsert_event(CalendarEventWrite::GoogleBackfill {
key,
diff --git a/crates/calendar_events/src/domain/service/test.rs b/crates/calendar_events/src/domain/service/test.rs
index 870ca00478..22a6d7ea58 100644
--- a/crates/calendar_events/src/domain/service/test.rs
+++ b/crates/calendar_events/src/domain/service/test.rs
@@ -3,9 +3,9 @@ use crate::domain::{
models::{
AttendeeResponseStatus, CalendarAttendee, CalendarBackfillClaim,
CalendarBackfillFailureDisposition, CalendarBackfillJobKey, CalendarEvent,
- CalendarEventSource, CalendarOccurrence, CalendarSyncStatus, EmailIcsSource, EventStatus,
- EventTime, EventTransparency, EventVisibility, GOOGLE_CALENDAR_SCOPES,
- GoogleCalendarSyncSnapshot, GoogleEventSyncBatch, GoogleWatchChannel, GoogleWatchConfig,
+ CalendarEventSource, CalendarOccurrence, CalendarSyncStatus, EventStatus, EventTime,
+ EventTransparency, EventVisibility, GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot,
+ GoogleEventSource, GoogleEventSyncBatch, GoogleWatchChannel, GoogleWatchConfig,
ProviderCalendar, StoredGoogleCalendar,
},
ports::{
@@ -33,8 +33,7 @@ impl CalendarRepository for FakeRepo {
async fn upsert_event(&self, write: CalendarEventWrite) -> Result<Uuid, Report> {
let upsert = match write {
- CalendarEventWrite::EmailIcs(upsert)
- | CalendarEventWrite::GoogleBackfill { upsert, .. }
+ CalendarEventWrite::GoogleBackfill { upsert, .. }
| CalendarEventWrite::Fixture(upsert) => upsert,
};
let id = upsert.event.id;
@@ -155,12 +154,13 @@ fn valid_upsert() -> CalendarEventUpsert {
created_at: starts_at,
updated_at: starts_at,
},
- source: CalendarEventSource::EmailIcs(EmailIcsSource {
+ source: CalendarEventSource::Google(GoogleEventSource {
email_link_id: Uuid::now_v7(),
- email_thread_id: None,
- email_message_id: Uuid::now_v7(),
- email_attachment_id: None,
- content_hash: "hash".to_string(),
+ account_id: Uuid::now_v7(),
+ calendar_id: Uuid::now_v7(),
+ provider_event_id: "provider-event".to_string(),
+ provider_recurring_event_id: None,
+ provider_etag: None,
raw_payload: serde_json::json!({}),
}),
overrides: Vec::new(),
@@ -178,21 +178,13 @@ fn valid_upsert() -> CalendarEventUpsert {
}
}
-#[tokio::test]
-async fn accepts_valid_event() {
- let repo = FakeRepo::default();
- let service = CalendarService::new(repo.clone());
- let upsert = valid_upsert();
-
- service.upsert_email_event(upsert).await.unwrap();
-
- assert_eq!(repo.upserts.lock().unwrap().len(), 1);
+#[test]
+fn accepts_valid_event() {
+ assert!(validate_upsert(&valid_upsert()).is_ok());
}
-#[tokio::test]
-async fn rejects_invalid_occurrence_time() {
- let repo = FakeRepo::default();
- let service = CalendarService::new(repo.clone());
+#[test]
+fn rejects_invalid_occurrence_time() {
let mut upsert = valid_upsert();
let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
upsert.occurrences[0].time = EventTime::Timed {
@@ -201,8 +193,7 @@ async fn rejects_invalid_occurrence_time() {
time_zone: None,
};
- assert!(service.upsert_email_event(upsert).await.is_err());
- assert!(repo.upserts.lock().unwrap().is_empty());
+ assert!(validate_upsert(&upsert).is_err());
}
#[test]
diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rs
index 25b30701e3..6bbe344c1e 100644
--- a/crates/calendar_events/src/outbound/pg.rs
+++ b/crates/calendar_events/src/outbound/pg.rs
@@ -458,24 +458,12 @@ impl CalendarRepository for PgCalendarRepository {
async fn upsert_event(&self, write: CalendarEventWrite) -> Result<Uuid, Report> {
let mut tx = self.pool.begin().await.map_err(report)?;
let upsert = match write {
- CalendarEventWrite::EmailIcs(upsert) => {
- if !matches!(upsert.source, CalendarEventSource::EmailIcs(_)) {
- return Err(rootcause::report!(
- "email ICS ingestion requires an email ICS source"
- ));
- }
- upsert
- }
CalendarEventWrite::GoogleBackfill {
key,
lease_token,
upsert,
} => {
- let CalendarEventSource::Google(source) = &upsert.source else {
- return Err(rootcause::report!(
- "Google backfill ingestion requires a Google source"
- ));
- };
+ let CalendarEventSource::Google(source) = &upsert.source;
if source.email_link_id != key.email_link_id {
return Err(rootcause::report!(
"Google calendar event fence does not match its connected inbox"
@@ -488,14 +476,9 @@ impl CalendarRepository for PgCalendarRepository {
#[cfg(test)]
CalendarEventWrite::Fixture(upsert) => upsert,
};
- let source_kind = match &upsert.source {
- CalendarEventSource::Google(_) => "google",
- CalendarEventSource::EmailIcs(_) => "email_ics",
- };
- let source_link_id = match &upsert.source {
- CalendarEventSource::Google(source) => source.email_link_id,
- CalendarEventSource::EmailIcs(source) => source.email_link_id,
- };
+ let CalendarEventSource::Google(source) = &upsert.source;
+ let source_kind = "google";
+ let source_link_id = source.email_link_id;
let reconciliation_lock = event_reconciliation_lock(source_link_id, &upsert.event.ical_uid);
sqlx::query_scalar!(
r#"SELECT 1 AS "locked!" FROM pg_advisory_xact_lock($1)"#,
@@ -508,31 +491,28 @@ impl CalendarRepository for PgCalendarRepository {
// sync-token reset makes that happen wholesale. When the incoming
// Google projection is identical to the stored one, skip the write
// path entirely so rebuilds cost reads, not occurrence churn.
- if let CalendarEventSource::Google(source) = &upsert.source {
- let existing = sqlx::query!(
- r#"
- SELECT event_id, normalized_payload
- FROM calendar_event_sources
- WHERE source_kind = 'google'
- AND account_id = $1
- AND calendar_id = $2
- AND provider_event_id = $3
- "#,
- source.account_id,
- source.calendar_id,
- &source.provider_event_id,
- )
- .fetch_optional(&mut *tx)
- .await
- .map_err(report)?;
- if let Some(row) = existing {
- let incoming =
- serde_json::to_value(StoredSourceProjection::from(&upsert)).map_err(report)?;
- if canonical_projection(&row.normalized_payload) == canonical_projection(&incoming)
- {
- tx.commit().await.map_err(report)?;
- return Ok(row.event_id);
- }
+ let existing = sqlx::query!(
+ r#"
+ SELECT event_id, normalized_payload
+ FROM calendar_event_sources
+ WHERE source_kind = 'google'
+ AND account_id = $1
+ AND calendar_id = $2
+ AND provider_event_id = $3
+ "#,
+ source.account_id,
+ source.calendar_id,
+ &source.provider_event_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(report)?;
+ if let Some(row) = existing {
+ let incoming =
+ serde_json::to_value(StoredSourceProjection::from(&upsert)).map_err(report)?;
+ if canonical_projection(&row.normalized_payload) == canonical_projection(&incoming) {
+ tx.commit().await.map_err(report)?;
+ return Ok(row.event_id);
}
}
@@ -583,29 +563,11 @@ impl CalendarRepository for PgCalendarRepository {
canonical_source_updated_at = EXCLUDED.canonical_source_updated_at,
updated_at = GREATEST(calendar_events.updated_at, EXCLUDED.updated_at)
WHERE
- (
- $25 = 'google'
- AND (
- EXCLUDED.sequence > calendar_events.sequence
- OR (
- EXCLUDED.sequence = calendar_events.sequence
- AND EXCLUDED.canonical_source_updated_at
- >= calendar_events.canonical_source_updated_at
- )
- OR calendar_events.canonical_source_kind <> 'google'
- )
- )
+ EXCLUDED.sequence > calendar_events.sequence
OR (
- $25 = 'email_ics'
- AND calendar_events.canonical_source_kind <> 'google'
- AND (
- EXCLUDED.sequence > calendar_events.sequence
- OR (
- EXCLUDED.sequence = calendar_events.sequence
- AND EXCLUDED.canonical_source_updated_at
- >= calendar_events.canonical_source_updated_at
- )
- )
+ EXCLUDED.sequence = calendar_events.sequence
+ AND EXCLUDED.canonical_source_updated_at
+ >= calendar_events.canonical_source_updated_at
)
RETURNING id
"#,
@@ -633,7 +595,6 @@ impl CalendarRepository for PgCalendarRepository {
source_kind,
upsert.event.created_at,
upsert.event.updated_at,
- source_kind,
)
.fetch_optional(&mut *tx)
.await
@@ -1791,11 +1752,9 @@ async fn persist_source(
) -> Result<(), Report> {
let normalized_payload =
serde_json::to_value(StoredSourceProjection::from(upsert)).map_err(report)?;
- let source = &upsert.source;
- match source {
- CalendarEventSource::Google(source) => {
- sqlx::query!(
- r#"
+ let CalendarEventSource::Google(source) = &upsert.source;
+ sqlx::query!(
+ r#"
INSERT INTO calendar_event_sources (
id, event_id, source_link_id, source_kind, account_id, calendar_id,
provider_event_id, provider_recurring_event_id,
@@ -1824,72 +1783,22 @@ async fn persist_source(
AND EXCLUDED.source_updated_at >= calendar_event_sources.source_updated_at
)
"#,
- Uuid::now_v7(),
- event_id,
- source.email_link_id,
- source.account_id,
- source.calendar_id,
- &source.provider_event_id,
- source.provider_recurring_event_id.as_deref(),
- source.provider_etag.as_deref(),
- &source.raw_payload,
- db_sequence(upsert.event.sequence)?,
- upsert.event.updated_at,
- &normalized_payload,
- )
- .execute(&mut **tx)
- .await
- .map_err(report)?;
- }
- CalendarEventSource::EmailIcs(source) => {
- sqlx::query!(
- r#"
- INSERT INTO calendar_event_sources (
- id, event_id, source_link_id, source_kind, email_link_id, email_thread_id,
- email_message_id, email_attachment_id, content_hash,
- raw_payload, source_sequence, source_updated_at, normalized_payload
- )
- VALUES (
- $1, $2, $3, 'email_ics', $3, $4, $5, $6, $7, $8,
- $9, $10, $11
- )
- ON CONFLICT (
- email_link_id,
- email_message_id,
- COALESCE(email_attachment_id, ''),
- content_hash,
- event_id
- ) WHERE source_kind = 'email_ics'
- DO UPDATE SET
- raw_payload = EXCLUDED.raw_payload,
- source_sequence = EXCLUDED.source_sequence,
- source_updated_at = EXCLUDED.source_updated_at,
- normalized_payload = EXCLUDED.normalized_payload,
- last_seen_at = now()
- WHERE
- EXCLUDED.source_sequence > calendar_event_sources.source_sequence
- OR (
- EXCLUDED.source_sequence = calendar_event_sources.source_sequence
- AND EXCLUDED.source_updated_at >= calendar_event_sources.source_updated_at
- )
- "#,
- Uuid::now_v7(),
- event_id,
- source.email_link_id,
- source.email_thread_id,
- source.email_message_id,
- source.email_attachment_id.as_deref(),
- &source.content_hash,
- &source.raw_payload,
- db_sequence(upsert.event.sequence)?,
- upsert.event.updated_at,
- &normalized_payload,
- )
- .execute(&mut **tx)
- .await
- .map_err(report)?;
- }
- }
+ Uuid::now_v7(),
+ event_id,
+ source.email_link_id,
+ source.account_id,
+ source.calendar_id,
+ &source.provider_event_id,
+ source.provider_recurring_event_id.as_deref(),
+ source.provider_etag.as_deref(),
+ &source.raw_payload,
+ db_sequence(upsert.event.sequence)?,
+ upsert.event.updated_at,
+ &normalized_payload,
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(report)?;
Ok(())
}
@@ -1927,7 +1836,6 @@ async fn restore_best_source_or_delete(
FROM calendar_event_sources
WHERE event_id = $1
ORDER BY
- CASE source_kind WHEN 'google' THEN 0 ELSE 1 END,
source_sequence DESC,
source_updated_at DESC,
last_seen_at DESC,
diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rs
index fd9664b02f..01ea4ee028 100644
--- a/crates/calendar_events/src/outbound/pg/test.rs
+++ b/crates/calendar_events/src/outbound/pg/test.rs
@@ -1,7 +1,6 @@
use super::*;
use crate::domain::models::{
- EmailIcsSource, GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleEventSource,
- GoogleWatchChannel,
+ GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleEventSource, GoogleWatchChannel,
};
use crate::domain::ports::GoogleCalendarSyncRepository;
use crate::domain::service::GoogleCalendarBackfillFailureService;
@@ -83,13 +82,37 @@ async fn insert_user(pool: &PgPool, id: &str) {
.unwrap();
}
+/// Provision the account and calendar every event source now requires.
+async fn provider_ids(repo: &PgCalendarRepository, link_id: Uuid) -> (Uuid, Uuid) {
+ let account_id = repo.upsert_google_account(link_id).await.unwrap();
+ let calendar_id = repo
+ .upsert_calendar_fixture(
+ account_id,
+ ProviderCalendar {
+ provider_calendar_id: "primary".to_string(),
+ name: "Primary".to_string(),
+ description: None,
+ time_zone: Some("UTC".to_string()),
+ color: None,
+ access_role: Some("owner".to_string()),
+ is_primary: true,
+ is_selected: true,
+ },
+ )
+ .await
+ .unwrap();
+ (account_id, calendar_id)
+}
+
fn timed_upsert(
owner_id: &str,
link_id: Uuid,
+ provider: (Uuid, Uuid),
uid: &str,
title: &str,
sequence: u32,
) -> CalendarEventUpsert {
+ let (account_id, calendar_id) = provider;
let id = Uuid::now_v7();
let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
let ends_at = starts_at + Duration::hours(1);
@@ -128,12 +151,13 @@ fn timed_upsert(
created_at: starts_at,
updated_at: starts_at + Duration::minutes(i64::from(sequence)),
},
- source: CalendarEventSource::EmailIcs(EmailIcsSource {
+ source: CalendarEventSource::Google(GoogleEventSource {
email_link_id: link_id,
- email_thread_id: None,
- email_message_id: Uuid::now_v7(),
- email_attachment_id: Some("invite.ics".to_string()),
- content_hash: format!("hash-{sequence}"),
+ account_id,
+ calendar_id,
+ provider_event_id: format!("provider-{uid}"),
+ provider_recurring_event_id: None,
+ provider_etag: None,
raw_payload: serde_json::json!({}),
}),
overrides: Vec::new(),
@@ -332,6 +356,7 @@ async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(poo
let mut google = timed_upsert(
owner_id,
link_id,
+ (account_id, calendar_id),
"Removed with scope",
1,
@@ -566,176 +591,43 @@ async fn google_claim_rejects_a_job_without_its_grant_provisioned_account(pool:
assert!(state.lease_token.is_none());
}
-#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn google_source_reconciles_email_invite_into_one_canonical_entity(pool: PgPool) {
- let owner_id = "macro|[email protected]";
- let link_id = insert_link(&pool, owner_id).await;
- let repo = PgCalendarRepository::new(pool.clone());
- let email = timed_upsert(
- owner_id,
- link_id,
- "Email title",
- 7,
- );
- let canonical_id = repo.upsert_event_fixture(email.clone()).await.unwrap();
-
- repo.apply_google_grant(link_id, complete_grant())
- .await
- .unwrap();
- let account_id = repo.upsert_google_account(link_id).await.unwrap();
- let calendar_id = repo
- .upsert_calendar_fixture(
- account_id,
- ProviderCalendar {
- provider_calendar_id: "primary".to_string(),
- name: "Primary".to_string(),
- description: None,
- time_zone: Some("UTC".to_string()),
- color: None,
- access_role: Some("owner".to_string()),
- is_primary: true,
- is_selected: true,
- },
- )
- .await
- .unwrap();
-
- let mut google = timed_upsert(
- owner_id,
- link_id,
- "Google title",
- 1,
- );
- google.source = CalendarEventSource::Google(GoogleEventSource {
- email_link_id: link_id,
- account_id,
- calendar_id,
- provider_event_id: "google-event".to_string(),
- provider_recurring_event_id: None,
- provider_etag: Some("\"etag\"".to_string()),
- raw_payload: serde_json::json!({}),
- });
- google.event.is_read_only = false;
- let reconciled_id = repo.upsert_event_fixture(google).await.unwrap();
-
- assert_eq!(reconciled_id, canonical_id);
- let row = sqlx::query!(
- r#"
- SELECT
- (SELECT count(*) FROM calendar_events WHERE owner_id = $1) AS "event_count!",
- (SELECT title FROM calendar_events WHERE id = $2) AS "title!",
- (SELECT count(*) FROM calendar_event_sources WHERE event_id = $2) AS "source_count!"
- "#,
- owner_id,
- canonical_id,
- )
- .fetch_one(&pool)
- .await
- .unwrap();
- assert_eq!(
- (row.event_count, row.title, row.source_count),
- (1, "Google title".to_string(), 2)
- );
-
- let mut later_email = timed_upsert(
- owner_id,
- link_id,
- "Stale email title",
- 99,
- );
- later_email.event.updated_at += Duration::days(1);
- repo.upsert_event_fixture(later_email).await.unwrap();
- let title = sqlx::query_scalar!(
- "SELECT title FROM calendar_events WHERE id = $1",
- canonical_id
- )
- .fetch_one(&pool)
- .await
- .unwrap();
- assert_eq!(title, "Google title");
-}
-
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn canonical_precedence_uses_the_selected_source_clock(pool: PgPool) {
let owner_id = "macro|[email protected]";
let link_id = insert_link(&pool, owner_id).await;
let repo = PgCalendarRepository::new(pool.clone());
let source_clock = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
- let google_t1 = source_clock + Duration::hours(1);
- let google_t2 = source_clock + Duration::hours(2);
- let email_t3 = source_clock + Duration::hours(3);
-
- let mut email = timed_upsert(
- owner_id,
- link_id,
- "Email at T3",
- 1,
- );
- email.event.updated_at = email_t3;
- let event_id = repo.upsert_event_fixture(email).await.unwrap();
+ let later_clock = source_clock + Duration::hours(3);
+ let winning_clock = source_clock + Duration::hours(2);
repo.apply_google_grant(link_id, complete_grant())
.await
.unwrap();
- let account_id = repo.upsert_google_account(link_id).await.unwrap();
- let calendar_id = repo
- .upsert_calendar_fixture(
- account_id,
- ProviderCalendar {
- provider_calendar_id: "primary".to_string(),
- name: "Primary".to_string(),
- description: None,
- time_zone: Some("UTC".to_string()),
- color: None,
- access_role: Some("owner".to_string()),
- is_primary: true,
- is_selected: true,
- },
- )
- .await
- .unwrap();
+ let provider = provider_ids(&repo, link_id).await;
- let mut first_google = timed_upsert(
+ let mut first = timed_upsert(
owner_id,
link_id,
+ provider,
- "Google at T1",
+ "Earlier sequence, later clock",
1,
);
- first_google.event.updated_at = google_t1;
- first_google.source = CalendarEventSource::Google(GoogleEventSource {
- email_link_id: link_id,
- account_id,
- calendar_id,
- provider_event_id: "source-clock-google-event".to_string(),
- provider_recurring_event_id: None,
- provider_etag: Some("\"t1\"".to_string()),
- raw_payload: serde_json::json!({}),
- });
- repo.upsert_event_fixture(first_google).await.unwrap();
+ first.event.updated_at = later_clock;
+ let event_id = repo.upsert_event_fixture(first).await.unwrap();
- let mut second_google = timed_upsert(
+ // A higher sequence wins regardless of clock, so the canonical clock moves
+ // backwards while the entity's own updated_at keeps the latest it has seen.
+ let mut second = timed_upsert(
owner_id,
link_id,
+ provider,
- "Google at T2",
- 1,
+ "Later sequence, earlier clock",
+ 2,
);
- second_google.event.updated_at = google_t2;
- second_google.source = CalendarEventSource::Google(GoogleEventSource {
- email_link_id: link_id,
- account_id,
- calendar_id,
- provider_event_id: "source-clock-google-event".to_string(),
- provider_recurring_event_id: None,
- provider_etag: Some("\"t2\"".to_string()),
- raw_payload: serde_json::json!({}),
- });
- repo.upsert_event_fixture(second_google).await.unwrap();
+ second.event.updated_at = winning_clock;
+ repo.upsert_event_fixture(second).await.unwrap();
let canonical = sqlx::query!(
r#"
@@ -748,9 +640,9 @@ async fn canonical_precedence_uses_the_selected_source_clock(pool: PgPool) {
.fetch_one(&pool)
.await
.unwrap();
- assert_eq!(canonical.title, "Google at T2");
- assert_eq!(canonical.canonical_source_updated_at, google_t2);
- assert_eq!(canonical.updated_at, email_t3);
+ assert_eq!(canonical.title, "Later sequence, earlier clock");
+ assert_eq!(canonical.canonical_source_updated_at, winning_clock);
+ assert_eq!(canonical.updated_at, later_clock);
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -782,6 +674,7 @@ async fn fenced_google_snapshot_removes_deleted_events_and_calendars(pool: PgPoo
let mut google = timed_upsert(
owner_id,
link_id,
+ (account_id, calendar_id),
"Deleted Google event",
1,
@@ -919,6 +812,7 @@ async fn expired_google_worker_cannot_resurrect_reconciled_provider_data(pool: P
let mut google = timed_upsert(
owner_id,
link_id,
+ (account_id, calendar_id),
"Stale worker event",
1,
@@ -1021,42 +915,19 @@ async fn expired_google_worker_cannot_resurrect_reconciled_provider_data(pool: P
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn google_snapshot_deletion_restores_the_surviving_email_source(pool: PgPool) {
+async fn google_snapshot_deletion_removes_an_event_without_a_surviving_source(pool: PgPool) {
let owner_id = "macro|[email protected]";
let link_id = insert_link(&pool, owner_id).await;
let repo = PgCalendarRepository::new(pool.clone());
- let email = timed_upsert(
- owner_id,
- link_id,
- "Email fallback",
- 1,
- );
- let event_id = repo.upsert_event_fixture(email).await.unwrap();
let enabled = repo
.apply_google_grant(link_id, complete_grant())
.await
.unwrap();
- let account_id = repo.upsert_google_account(link_id).await.unwrap();
- let calendar_id = repo
- .upsert_calendar_fixture(
- account_id,
- ProviderCalendar {
- provider_calendar_id: "primary".to_string(),
- name: "Primary".to_string(),
- description: None,
- time_zone: Some("UTC".to_string()),
- color: None,
- access_role: Some("owner".to_string()),
- is_primary: true,
- is_selected: true,
- },
- )
- .await
- .unwrap();
+ let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
let mut google = timed_upsert(
owner_id,
link_id,
+ (account_id, calendar_id),
"Google canonical",
2,
@@ -1070,7 +941,7 @@ async fn google_snapshot_deletion_restores_the_surviving_email_source(pool: PgPo
provider_etag: Some("\"etag\"".to_string()),
raw_payload: serde_json::json!({}),
});
- repo.upsert_event_fixture(google).await.unwrap();
+ let event_id = repo.upsert_event_fixture(google).await.unwrap();
let google_job = enabled
.jobs
@@ -1105,21 +976,18 @@ async fn google_snapshot_deletion_restores_the_surviving_email_source(pool: PgPo
.await
.unwrap();
- let restored = sqlx::query!(
+ let remaining = sqlx::query!(
r#"
- SELECT title, canonical_source_kind,
- (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
- FROM calendar_events
- WHERE id = $1
+ SELECT
+ (SELECT count(*) FROM calendar_events WHERE id = $1) AS "event_count!",
+ (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
"#,
event_id,
)
.fetch_one(&pool)
.await
.unwrap();
- assert_eq!(restored.title, "Email fallback");
- assert_eq!(restored.canonical_source_kind, "email_ics");
- assert_eq!(restored.source_count, 1);
+ assert_eq!((remaining.event_count, remaining.source_count), (0, 0));
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -1127,38 +995,15 @@ async fn incremental_cancellation_tombstones_retire_sources_without_a_snapshot(p
let owner_id = "macro|[email protected]";
let link_id = insert_link(&pool, owner_id).await;
let repo = PgCalendarRepository::new(pool.clone());
- let email = timed_upsert(
- owner_id,
- link_id,
- "Email fallback",
- 1,
- );
- let event_id = repo.upsert_event_fixture(email).await.unwrap();
let enabled = repo
.apply_google_grant(link_id, complete_grant())
.await
.unwrap();
- let account_id = repo.upsert_google_account(link_id).await.unwrap();
- let calendar_id = repo
- .upsert_calendar_fixture(
- account_id,
- ProviderCalendar {
- provider_calendar_id: "primary".to_string(),
- name: "Primary".to_string(),
- description: None,
- time_zone: Some("UTC".to_string()),
- color: None,
- access_role: Some("owner".to_string()),
- is_primary: true,
- is_selected: true,
- },
- )
- .await
- .unwrap();
+ let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
let mut google = timed_upsert(
owner_id,
link_id,
+ (account_id, calendar_id),
"Google canonical",
2,
@@ -1172,10 +1017,11 @@ async fn incremental_cancellation_tombstones_retire_sources_without_a_snapshot(p
provider_etag: Some("\"etag\"".to_string()),
raw_payload: serde_json::json!({}),
});
- repo.upsert_event_fixture(google).await.unwrap();
+ let event_id = repo.upsert_event_fixture(google).await.unwrap();
let mut instance = timed_upsert(
owner_id,
link_id,
+ (account_id, calendar_id),
"Series instance",
1,
@@ -1224,21 +1070,19 @@ async fn incremental_cancellation_tombstones_retire_sources_without_a_snapshot(p
.await
.unwrap();
- let restored = sqlx::query!(
+ // The tombstone retires the only source, so the entity goes with it.
+ let retired = sqlx::query!(
r#"
- SELECT title, canonical_source_kind,
- (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
- FROM calendar_events
- WHERE id = $1
+ SELECT
+ (SELECT count(*) FROM calendar_events WHERE id = $1) AS "event_count!",
+ (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
"#,
event_id,
)
.fetch_one(&pool)
.await
.unwrap();
- assert_eq!(restored.title, "Email fallback");
- assert_eq!(restored.canonical_source_kind, "email_ics");
- assert_eq!(restored.source_count, 1);
+ assert_eq!((retired.event_count, retired.source_count), (0, 0));
assert_eq!(
sqlx::query_scalar!(
@@ -1268,7 +1112,15 @@ async fn occurrence_range_uses_overlap_indexes_and_preserves_attendees(pool: PgP
let owner_id = "macro|[email protected]";
let link_id = insert_link(&pool, owner_id).await;
let repo = PgCalendarRepository::new(pool);
- let upsert = timed_upsert(owner_id, link_id, "[email protected]", "Range test", 1);
+ let provider = provider_ids(&repo, link_id).await;
+ let upsert = timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Range test",
+ 1,
+ );
repo.upsert_event_fixture(upsert).await.unwrap();
let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 14, 30, 0).unwrap();
@@ -1297,10 +1149,12 @@ async fn occurrence_cursor_is_stable_when_occurrences_share_a_start(pool: PgPool
let owner_id = "macro|[email protected]";
let link_id = insert_link(&pool, owner_id).await;
let repo = PgCalendarRepository::new(pool);
+ let provider = provider_ids(&repo, link_id).await;
for ordinal in 1..=3 {
repo.upsert_event_fixture(timed_upsert(
owner_id,
link_id,
+ provider,
&format!("pagination-{ordinal}@example.com"),
&format!("Pagination event {ordinal}"),
ordinal,
@@ -1525,7 +1379,14 @@ async fn unchanged_google_projection_skips_the_write_path(pool: PgPool) {
.unwrap();
let build = |title: &str| {
- let mut upsert = timed_upsert(owner_id, link_id, "[email protected]", title, 1);
+ let mut upsert = timed_upsert(
+ owner_id,
+ link_id,
+ (account_id, calendar_id),
+ title,
+ 1,
+ );
upsert.source = CalendarEventSource::Google(GoogleEventSource {
email_link_id: link_id,
account_id,
@@ -1583,9 +1444,11 @@ async fn delegated_inbox_source_grants_calendar_visibility(pool: PgPool) {
insert_user(&pool, primary_id).await;
let link_id = insert_link(&pool, child_id).await;
let repo = PgCalendarRepository::new(pool.clone());
+ let provider = provider_ids(&repo, link_id).await;
repo.upsert_event_fixture(timed_upsert(
child_id,
link_id,
+ provider,
"Delegated event",
1,
@@ -1637,10 +1500,13 @@ async fn identical_uids_from_distinct_inboxes_remain_distinct_and_link_scoped(po
let delegated_link_id = insert_link(&pool, child_id).await;
let private_link_id = insert_link(&pool, child_id).await;
let repo = PgCalendarRepository::new(pool.clone());
+ let delegated_provider = provider_ids(&repo, delegated_link_id).await;
+ let private_provider = provider_ids(&repo, private_link_id).await;
repo.upsert_event_fixture(timed_upsert(
child_id,
delegated_link_id,
+ delegated_provider,
"Delegated inbox",
1,
@@ -1650,6 +1516,7 @@ async fn identical_uids_from_distinct_inboxes_remain_distinct_and_link_scoped(po
repo.upsert_event_fixture(timed_upsert(
child_id,
private_link_id,
+ private_provider,
"Private inbox",
1,
@@ -1707,11 +1574,26 @@ async fn equal_sequence_cannot_replace_a_newer_projection(pool: PgPool) {
let owner_id = "macro|[email protected]";
let link_id = insert_link(&pool, owner_id).await;
let repo = PgCalendarRepository::new(pool.clone());
- let mut latest = timed_upsert(owner_id, link_id, "[email protected]", "Latest", 4);
+ let provider = provider_ids(&repo, link_id).await;
+ let mut latest = timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Latest",
+ 4,
+ );
latest.event.updated_at += Duration::days(2);
let event_id = repo.upsert_event_fixture(latest).await.unwrap();
- let mut stale = timed_upsert(owner_id, link_id, "[email protected]", "Stale", 4);
+ let mut stale = timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Stale",
+ 4,
+ );
stale.event.updated_at -= Duration::days(2);
repo.upsert_event_fixture(stale).await.unwrap();
@@ -1768,6 +1650,7 @@ async fn stale_google_source_projection_cannot_resurface_during_reconciliation(p
let mut latest = timed_upsert(
owner_id,
link_id,
+ (account_id, primary_calendar_id),
"Latest",
4,
@@ -1787,6 +1670,7 @@ async fn stale_google_source_projection_cannot_resurface_during_reconciliation(p
let mut sibling = timed_upsert(
owner_id,
link_id,
+ (account_id, sibling_calendar_id),
"Sibling",
1,
@@ -1805,6 +1689,7 @@ async fn stale_google_source_projection_cannot_resurface_during_reconciliation(p
let mut stale = timed_upsert(
owner_id,
link_id,
+ (account_id, primary_calendar_id),
"Stale",
4,
diff --git a/crates/email_db_client/src/backfill/job/update/test.rs b/crates/email_db_client/src/backfill/job/update/test.rs
index 220a9e7d14..55d13a7341 100644
--- a/crates/email_db_client/src/backfill/job/update/test.rs
+++ b/crates/email_db_client/src/backfill/job/update/test.rs
@@ -5,7 +5,6 @@ use super::{
release_init_lease, renew_completion_effects, renew_init_lease,
};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
-use serde_json::json;
use sqlx::{PgPool, types::Uuid};
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -286,7 +285,6 @@ async fn zero_thread_completion_is_fenced_by_init_lease(pool: PgPool) -> anyhow:
async fn failing_a_terminal_scan_is_an_idempotent_noop(pool: PgPool) -> anyhow::Result<()> {
let link_id = Uuid::new_v4();
let email_job_id = Uuid::new_v4();
- let calendar_job_id = Uuid::new_v4();
sqlx::query!(
r#"
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
@@ -309,19 +307,6 @@ async fn failing_a_terminal_scan_is_an_idempotent_noop(pool: PgPool) -> anyhow::
)
.execute(&pool)
.await?;
- sqlx::query!(
- r#"
- INSERT INTO calendar_backfill_jobs (
- id, email_link_id, kind, grant_version, status, cursor, completed_at
- )
- VALUES ($1, $2, 'email_ics', 1, 'complete', $3, now())
- "#,
- calendar_job_id,
- link_id,
- json!({ "emailBackfillJobId": email_job_id }),
- )
- .execute(&pool)
- .await?;
assert!(!fail_backfill_job(&pool, email_job_id).await?);
assert!(!fail_backfill_job(&pool, Uuid::new_v4()).await?);
@@ -332,14 +317,7 @@ async fn failing_a_terminal_scan_is_an_idempotent_noop(pool: PgPool) -> anyhow::
)
.fetch_one(&pool)
.await?;
- let calendar_status = sqlx::query_scalar!(
- "SELECT status FROM calendar_backfill_jobs WHERE id = $1",
- calendar_job_id,
- )
- .fetch_one(&pool)
- .await?;
assert_eq!(email_status, "Complete");
- assert_eq!(calendar_status, "complete");
Ok(())
}
diff --git a/crates/macro_db_client/migrations/20260806202039_drop_email_ics_calendar_sources.down.sql b/crates/macro_db_client/migrations/20260806202039_drop_email_ics_calendar_sources.down.sql
new file mode 100644
index 0000000000..69e23b6d37
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260806202039_drop_email_ics_calendar_sources.down.sql
@@ -0,0 +1,61 @@
+-- Restores the email-ICS schema shape. Rows deleted by the up migration are
+-- not recoverable; a reversal leaves the email columns empty.
+
+ALTER TABLE calendar_backfill_jobs
+ DROP CONSTRAINT IF EXISTS calendar_backfill_jobs_kind_check;
+ALTER TABLE calendar_backfill_jobs
+ ADD CONSTRAINT calendar_backfill_jobs_kind_check
+ CHECK (kind IN ('google_calendar', 'email_ics'));
+
+ALTER TABLE calendar_events
+ DROP CONSTRAINT IF EXISTS calendar_events_canonical_source_kind_check;
+ALTER TABLE calendar_events
+ ADD CONSTRAINT calendar_events_canonical_source_kind_check
+ CHECK (canonical_source_kind IN ('google', 'email_ics'));
+
+ALTER TABLE calendar_event_sources
+ ADD COLUMN IF NOT EXISTS email_link_id uuid,
+ ADD COLUMN IF NOT EXISTS email_thread_id uuid,
+ ADD COLUMN IF NOT EXISTS email_message_id uuid,
+ ADD COLUMN IF NOT EXISTS email_attachment_id text,
+ ADD COLUMN IF NOT EXISTS content_hash text;
+
+ALTER TABLE calendar_event_sources
+ ADD CONSTRAINT calendar_event_sources_email_link_id_fkey
+ FOREIGN KEY (email_link_id) REFERENCES email_links(id) ON DELETE CASCADE;
+
+ALTER TABLE calendar_event_sources
+ DROP CONSTRAINT IF EXISTS calendar_event_sources_shape,
+ DROP CONSTRAINT IF EXISTS calendar_event_sources_source_kind_check;
+
+ALTER TABLE calendar_event_sources
+ ADD CONSTRAINT calendar_event_sources_source_kind_check
+ CHECK (source_kind IN ('google', 'email_ics')),
+ ADD CONSTRAINT calendar_event_sources_shape CHECK (
+ (
+ source_kind = 'google'
+ AND account_id IS NOT NULL
+ AND calendar_id IS NOT NULL
+ AND provider_event_id IS NOT NULL
+ AND email_link_id IS NULL
+ )
+ OR (
+ source_kind = 'email_ics'
+ AND email_link_id IS NOT NULL
+ AND email_message_id IS NOT NULL
+ AND content_hash IS NOT NULL
+ AND account_id IS NULL
+ AND calendar_id IS NULL
+ AND provider_event_id IS NULL
+ )
+ );
+
+CREATE UNIQUE INDEX IF NOT EXISTS calendar_event_sources_email_idx
+ ON calendar_event_sources (
+ email_link_id, email_message_id, COALESCE(email_attachment_id, ''), content_hash, event_id
+ )
+ WHERE source_kind = 'email_ics';
+
+CREATE INDEX IF NOT EXISTS calendar_event_sources_email_link_idx
+ ON calendar_event_sources (email_link_id)
+ WHERE email_link_id IS NOT NULL;
diff --git a/crates/macro_db_client/migrations/20260806202039_drop_email_ics_calendar_sources.up.sql b/crates/macro_db_client/migrations/20260806202039_drop_email_ics_calendar_sources.up.sql
new file mode 100644
index 0000000000..c58818db44
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260806202039_drop_email_ics_calendar_sources.up.sql
@@ -0,0 +1,53 @@
+-- Email ICS ceased to be a calendar source: extraction only ever ran for
+-- inboxes that also held the Google Calendar grant, where Google takes
+-- unconditional precedence, so nothing it wrote survived reconciliation.
+--
+-- The down migration restores the schema shape but cannot restore rows.
+
+-- Events whose only source was email drop out entirely; attendees,
+-- overrides, occurrences, and sources all cascade from calendar_events.
+DELETE FROM calendar_events WHERE canonical_source_kind = 'email_ics';
+
+-- Google-canonical events keep their event row but lose the email source
+-- record that was never reachable from the product.
+DELETE FROM calendar_event_sources WHERE source_kind = 'email_ics';
+
+-- calendar_sync_outbox rows cascade from the job.
+DELETE FROM calendar_backfill_jobs WHERE kind = 'email_ics';
+
+DROP INDEX IF EXISTS calendar_event_sources_email_idx;
+DROP INDEX IF EXISTS calendar_event_sources_email_link_idx;
+
+ALTER TABLE calendar_event_sources
+ DROP CONSTRAINT IF EXISTS calendar_event_sources_email_link_id_fkey,
+ DROP COLUMN IF EXISTS email_link_id,
+ DROP COLUMN IF EXISTS email_thread_id,
+ DROP COLUMN IF EXISTS email_message_id,
+ DROP COLUMN IF EXISTS email_attachment_id,
+ DROP COLUMN IF EXISTS content_hash;
+
+ALTER TABLE calendar_event_sources
+ DROP CONSTRAINT IF EXISTS calendar_event_sources_shape,
+ DROP CONSTRAINT IF EXISTS calendar_event_sources_source_kind_check;
+
+ALTER TABLE calendar_event_sources
+ ADD CONSTRAINT calendar_event_sources_source_kind_check
+ CHECK (source_kind = 'google'),
+ ADD CONSTRAINT calendar_event_sources_shape CHECK (
+ source_kind = 'google'
+ AND account_id IS NOT NULL
+ AND calendar_id IS NOT NULL
+ AND provider_event_id IS NOT NULL
+ );
+
+ALTER TABLE calendar_events
+ DROP CONSTRAINT IF EXISTS calendar_events_canonical_source_kind_check;
+ALTER TABLE calendar_events
+ ADD CONSTRAINT calendar_events_canonical_source_kind_check
+ CHECK (canonical_source_kind = 'google');
+
+ALTER TABLE calendar_backfill_jobs
+ DROP CONSTRAINT IF EXISTS calendar_backfill_jobs_kind_check;
+ALTER TABLE calendar_backfill_jobs
+ ADD CONSTRAINT calendar_backfill_jobs_kind_check
+ CHECK (kind = 'google_calendar');