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.

feat(search): add since and target index overrides for SPS backfill (#1963)

gbirmanfbe048872b0a
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-1fa9e2989967fe9d44ee4e7c7db0091aebd9369c066fde6ccfe090bc97271e71.json b/rust/cloud-storage/email_db_client/.sqlx/query-1fa9e2989967fe9d44ee4e7c7db0091aebd9369c066fde6ccfe090bc97271e71.json
new file mode 100644
index 0000000000..fbeff2f208
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-1fa9e2989967fe9d44ee4e7c7db0091aebd9369c066fde6ccfe090bc97271e71.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT t.id, l.macro_id\n FROM email_threads t\n JOIN email_links l ON t.link_id = l.id\n WHERE t.updated_at >= $3\n ORDER BY t.latest_inbound_message_ts DESC NULLS LAST\n LIMIT $1 OFFSET $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "macro_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8",
+ "Int8",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "1fa9e2989967fe9d44ee4e7c7db0091aebd9369c066fde6ccfe090bc97271e71"
+}
diff --git a/rust/cloud-storage/email_db_client/src/threads/get.rs b/rust/cloud-storage/email_db_client/src/threads/get.rs
index b26c2f02cd..a04e4fd569 100644
--- a/rust/cloud-storage/email_db_client/src/threads/get.rs
+++ b/rust/cloud-storage/email_db_client/src/threads/get.rs
@@ -32,6 +32,34 @@ pub async fn get_paginated_thread_ids_with_macro_user_id(
Ok(result)
}
+/// gets a list of thread ids with the macro user id, filtered to threads updated since a given timestamp.
+#[tracing::instrument(skip(pool), err)]
+pub async fn get_paginated_thread_ids_with_macro_user_id_since(
+ pool: &PgPool,
+ thread_limit: i64,
+ thread_offset: i64,
+ since: chrono::DateTime<chrono::Utc>,
+) -> anyhow::Result<Vec<(Uuid, String)>> {
+ let result = sqlx::query!(
+ r#"
+ SELECT t.id, l.macro_id
+ FROM email_threads t
+ JOIN email_links l ON t.link_id = l.id
+ WHERE t.updated_at >= $3
+ ORDER BY t.latest_inbound_message_ts DESC NULLS LAST
+ LIMIT $1 OFFSET $2
+ "#,
+ thread_limit,
+ thread_offset,
+ since
+ )
+ .map(|row| (row.id, row.macro_id))
+ .fetch_all(pool)
+ .await?;
+
+ Ok(result)
+}
+
/// get the ids of the latest-updated threads for the user.
#[tracing::instrument(skip(pool), err)]
pub async fn get_latest_thread_ids_paginated(
diff --git a/rust/cloud-storage/email_service/src/bin/backfill_search/backfill_search.rs b/rust/cloud-storage/email_service/src/bin/backfill_search/backfill_search.rs
index 68c0414d22..60227c526a 100644
--- a/rust/cloud-storage/email_service/src/bin/backfill_search/backfill_search.rs
+++ b/rust/cloud-storage/email_service/src/bin/backfill_search/backfill_search.rs
@@ -3,6 +3,12 @@
/// Required environment variables:
/// - DATABASE_URL
/// - SEARCH_EVENT_QUEUE
+///
+/// Optional:
+/// - SINCE: ISO 8601 timestamp to only backfill threads updated since that time
+/// e.g. SINCE=2026-03-16T00:00:00Z
+/// - EMAIL_INDEX_OVERRIDE: Override the target OpenSearch index for email upserts
+/// e.g. EMAIL_INDEX_OVERRIDE=emails_v2
use anyhow::Context;
use macro_entrypoint::MacroEntrypoint;
use sqlx::postgres::PgPoolOptions;
@@ -23,22 +29,50 @@ async fn main() -> anyhow::Result<()> {
let search_event_queue =
std::env::var("SEARCH_EVENT_QUEUE").context("SEARCH_EVENT_QUEUE not set")?;
+ let since = std::env::var("SINCE")
+ .ok()
+ .map(|s| {
+ s.parse::<chrono::DateTime<chrono::Utc>>()
+ .context("SINCE must be a valid ISO 8601 timestamp (e.g. 2026-03-16T00:00:00Z)")
+ })
+ .transpose()?;
+
+ let index_override = std::env::var("EMAIL_INDEX_OVERRIDE").ok();
+
+ if let Some(ref index) = index_override {
+ println!("Index override: {}", index);
+ }
+
let queue_aws_config = macro_aws_config::get_macro_aws_config().await;
let sqs_client = sqs_client::SQS::new(aws_sdk_sqs::Client::new(&queue_aws_config))
.search_event_queue(&search_event_queue);
+ if let Some(since) = &since {
+ println!("Backfilling threads updated since: {}", since);
+ } else {
+ println!("Backfilling all threads");
+ }
+
let limit = 1000;
let mut offset = 0;
let mut total = 0;
loop {
- let thread_and_macro_user_ids =
- email_db_client::threads::get::get_paginated_thread_ids_with_macro_user_id(
+ let thread_and_macro_user_ids = match since {
+ Some(since) => {
+ email_db_client::threads::get::get_paginated_thread_ids_with_macro_user_id_since(
+ &db, limit, offset, since,
+ )
+ .await
+ .context("Failed to get thread ids with macro user id")?
+ }
+ None => email_db_client::threads::get::get_paginated_thread_ids_with_macro_user_id(
&db, limit, offset,
)
.await
- .context("Failed to get thread ids with macro user id")?;
+ .context("Failed to get thread ids with macro user id")?,
+ };
if thread_and_macro_user_ids.is_empty() {
tracing::trace!("no more thread ids with macro user id to process");
@@ -53,6 +87,7 @@ async fn main() -> anyhow::Result<()> {
SearchQueueMessage::ExtractEmailThreadMessage(EmailThreadMessage {
thread_id: thread_id.to_string(),
macro_user_id,
+ index_override: index_override.clone(),
})
})
.collect();
diff --git a/rust/cloud-storage/email_service/src/pubsub/backfill/update_metadata.rs b/rust/cloud-storage/email_service/src/pubsub/backfill/update_metadata.rs
index 2ef27e0db0..be13bbe166 100644
--- a/rust/cloud-storage/email_service/src/pubsub/backfill/update_metadata.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/backfill/update_metadata.rs
@@ -84,6 +84,7 @@ pub async fn update_thread_metadata(
let search_message = SearchQueueMessage::ExtractEmailThreadMessage(EmailThreadMessage {
thread_id: p.thread_db_id.clone().to_string(),
macro_user_id: link.macro_id.to_string(),
+ index_override: None,
});
ctx.sqs_client
diff --git a/rust/cloud-storage/opensearch_client/src/email.rs b/rust/cloud-storage/opensearch_client/src/email.rs
index 3520defa49..999bcd1fd7 100644
--- a/rust/cloud-storage/opensearch_client/src/email.rs
+++ b/rust/cloud-storage/opensearch_client/src/email.rs
@@ -15,8 +15,9 @@ impl OpensearchClient {
pub async fn bulk_upsert_email_messages(
&self,
messages: &[UpsertEmailArgs],
+ index_override: Option<&str>,
) -> Result<BulkUpsertResult> {
- upsert::email::bulk_upsert_email_messages(&self.inner, messages).await
+ upsert::email::bulk_upsert_email_messages(&self.inner, messages, index_override).await
}
/// Deletes all email messages with the specified thread_id
diff --git a/rust/cloud-storage/opensearch_client/src/upsert/email.rs b/rust/cloud-storage/opensearch_client/src/upsert/email.rs
index 1d2be8fe8c..417915f74c 100644
--- a/rust/cloud-storage/opensearch_client/src/upsert/email.rs
+++ b/rust/cloud-storage/opensearch_client/src/upsert/email.rs
@@ -97,6 +97,7 @@ pub(crate) async fn upsert_email_message(
pub(crate) async fn bulk_upsert_email_messages(
client: &opensearch::OpenSearch,
messages: &[UpsertEmailArgs],
+ index_override: Option<&str>,
) -> Result<BulkUpsertResult> {
if messages.is_empty() {
return Ok(BulkUpsertResult::default());
@@ -122,11 +123,7 @@ pub(crate) async fn bulk_upsert_email_messages(
})?);
}
- super::bulk_upsert_to_index(
- client,
- SearchIndex::Emails.as_ref(),
- bulk_body,
- "bulk_upsert_email_messages",
- )
- .await
+ let index = index_override.unwrap_or(SearchIndex::Emails.as_ref());
+
+ super::bulk_upsert_to_index(client, index, bulk_body, "bulk_upsert_email_messages").await
}
diff --git a/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs b/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs
index a1d128d79a..1f0449423b 100644
--- a/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs
+++ b/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs
@@ -224,7 +224,10 @@ pub async fn process_upsert_thread_message(
if !upsert_email_message_args.is_empty() {
let result = opensearch_client
- .bulk_upsert_email_messages(&upsert_email_message_args)
+ .bulk_upsert_email_messages(
+ &upsert_email_message_args,
+ upsert_email_thread_message.index_override.as_deref(),
+ )
.await?;
if result.failed > 0 {
diff --git a/rust/cloud-storage/sqs_client/src/search/email.rs b/rust/cloud-storage/sqs_client/src/search/email.rs
index 547f027ab1..59da8a82f9 100644
--- a/rust/cloud-storage/sqs_client/src/search/email.rs
+++ b/rust/cloud-storage/sqs_client/src/search/email.rs
@@ -12,6 +12,9 @@ pub struct EmailThreadMessage {
pub thread_id: String,
/// The macro user id of the user who the message is for
pub macro_user_id: String,
+ /// Optional override for the target OpenSearch index (e.g. "emails_v2" for migration backfills)
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub index_override: Option<String>,
}
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Eq, Debug)]