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(opensearch): update names index (#318)

Will Hutchinsond47d3404d300
diff --git a/infra/stacks/opensearch/helpers/scripts/create_indices.ts b/infra/stacks/opensearch/helpers/scripts/create_indices.ts
index 34cff3afad..aba9728412 100644
--- a/infra/stacks/opensearch/helpers/scripts/create_indices.ts
+++ b/infra/stacks/opensearch/helpers/scripts/create_indices.ts
@@ -45,6 +45,12 @@ async function createNamesIndex(opensearchClient: Client) {
entity_type: {
type: 'keyword',
},
+ // The user id of who created the entity
+ user_id: {
+ type: 'keyword',
+ index: true,
+ doc_values: true,
+ },
},
},
},
diff --git a/infra/stacks/opensearch/helpers/scripts/delete_indices.ts b/infra/stacks/opensearch/helpers/scripts/delete_indices.ts
index ee6ab6057e..28deaaba5f 100644
--- a/infra/stacks/opensearch/helpers/scripts/delete_indices.ts
+++ b/infra/stacks/opensearch/helpers/scripts/delete_indices.ts
@@ -1,37 +1,76 @@
+/**
+ * This script deletes the provided comma-separated list of indices.
+ * Usage: bun scripts/delete_indices.ts <indices>
+ */
+require('dotenv').config();
+
import { client } from '../client';
-import {
- CHANNEL_INDEX,
- CHAT_INDEX,
- DOCUMENT_INDEX,
- EMAIL_INDEX,
-} from '../constants';
-
-const indices = [
- /* DOCUMENT_INDEX, CHAT_INDEX, EMAIL_INDEX, CHANNEL_INDEX */
-];
-
-async function deleteIndices() {
+import { IS_DRY_RUN } from '../constants';
+import { checkIndexExists } from '../utils/check_index_exists';
+
+// Get indices and user_id from command line arguments
+const indicesArg = process.argv[2];
+
+if (!indicesArg) {
+ console.error('Missing required command line arguments:');
+ console.error('Usage: bun scripts/delete_indices.ts <indices>');
+ console.error(
+ ' indices: Comma-separated list of index names (e.g., "documents,chats,emails")'
+ );
+ console.error('\nExample:');
+ console.error('bun scripts/delete_indices.ts "documents,emails,chats"');
+ process.exit(1);
+}
+
+const INDICES_TO_PROCESS = indicesArg
+ .split(',')
+ .map((index: string) => index.trim());
+
+async function run(dryRun: boolean = true) {
const opensearchClient = client();
- console.log('Deleting indices...');
- try {
- for (const index of indices) {
- const indexExists = await opensearchClient.indices.exists({
+ console.log('\n' + '='.repeat(60));
+ console.log(
+ `Opensearch Indices Cleanup Script ${dryRun ? '(DRY-RUN MODE)' : '(LIVE MODE)'}`
+ );
+ console.log('='.repeat(60));
+ console.log(`\nThis script will delete ${INDICES_TO_PROCESS.join(', ')}`);
+ console.log('\n⚠️ WARNING: This will permanently delete data!');
+
+ if (dryRun) {
+ console.log('\n⚠️ DRY-RUN MODE: No changes will be made to the database');
+ } else {
+ console.log('\n🚨 LIVE MODE: Fields will be permanently removed!');
+ }
+
+ for (const index of INDICES_TO_PROCESS) {
+ console.log(`Processing index: ${index}`);
+
+ try {
+ const indexExists = await checkIndexExists(opensearchClient, index);
+
+ if (!indexExists) {
+ console.log(`Index ${index} does not exist, skipping...`);
+ continue;
+ }
+
+ console.log(`Index ${index} exists, deleting...`);
+
+ if (dryRun) {
+ console.log(`[DRY-RUN] Would delete index ${index}`);
+ continue;
+ }
+
+ const result = await opensearchClient.indices.delete({
index,
});
- if (indexExists) {
- console.log(`${index} index exists, deleting...`);
- const result = await opensearchClient.indices.delete({
- index,
- });
- console.log(`${index} index deleted`, result.body);
- } else {
- console.log(`${index} index does not exist`);
- }
+
+ console.log(`${index} index deleted`, result.body);
+ } catch (error) {
+ console.error(`Error deleting index ${index}:`, error);
+ process.exit(1);
}
- } catch (error) {
- console.error('Error', error);
}
}
-deleteIndices();
+run(IS_DRY_RUN);
diff --git a/infra/stacks/opensearch/helpers/utils/remove_field.ts b/infra/stacks/opensearch/helpers/utils/remove_field.ts
index df04323323..64a4111e4f 100644
--- a/infra/stacks/opensearch/helpers/utils/remove_field.ts
+++ b/infra/stacks/opensearch/helpers/utils/remove_field.ts
@@ -1,20 +1,5 @@
import type { Client } from '@opensearch-project/opensearch';
-
-/**
- * Checks if an index exists.
- * @param opensearchClient The OpenSearch client.
- * @param indexName The name of the index.
- * @returns Whether the index exists.
- */
-async function checkIndexExists(
- opensearchClient: Client,
- indexName: string
-): Promise<boolean> {
- const response = await opensearchClient.indices.exists({
- index: indexName,
- });
- return response.body;
-}
+import { checkIndexExists } from './check_index_exists';
/**
* Checks if a field exists in an index.
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-1c5f9c90a5ee11ffedc5c98b589c821898f04952a5a597c9796e152fa4c65bb1.json b/rust/cloud-storage/macro_db_client/.sqlx/query-1c5f9c90a5ee11ffedc5c98b589c821898f04952a5a597c9796e152fa4c65bb1.json
new file mode 100644
index 0000000000..dee1621260
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-1c5f9c90a5ee11ffedc5c98b589c821898f04952a5a597c9796e152fa4c65bb1.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n e.subject,\n l.macro_id\n FROM\n \"email_messages\" e\n JOIN email_links l ON l.id = e.link_id\n WHERE\n e.thread_id = $1\n LIMIT 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "subject",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "macro_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ false
+ ]
+ },
+ "hash": "1c5f9c90a5ee11ffedc5c98b589c821898f04952a5a597c9796e152fa4c65bb1"
+}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-34a1c3b64fdb985b5e78d815f8fac33efbd25ad5cb6854e9fe3949518f530570.json b/rust/cloud-storage/macro_db_client/.sqlx/query-34a1c3b64fdb985b5e78d815f8fac33efbd25ad5cb6854e9fe3949518f530570.json
deleted file mode 100644
index 32c5fe0adb..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-34a1c3b64fdb985b5e78d815f8fac33efbd25ad5cb6854e9fe3949518f530570.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n c.name as \"name?\"\n FROM\n \"comms_channels\" c\n WHERE\n c.id = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "name?",
- "type_info": "Varchar"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- true
- ]
- },
- "hash": "34a1c3b64fdb985b5e78d815f8fac33efbd25ad5cb6854e9fe3949518f530570"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-4330d1613cce138c4ad80ab77be5d5974752e684b9350a86e77098cff910849c.json b/rust/cloud-storage/macro_db_client/.sqlx/query-4330d1613cce138c4ad80ab77be5d5974752e684b9350a86e77098cff910849c.json
deleted file mode 100644
index 7f2b0568cb..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-4330d1613cce138c4ad80ab77be5d5974752e684b9350a86e77098cff910849c.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n c.name\n FROM\n \"Chat\" c\n WHERE\n c.id = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "name",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "4330d1613cce138c4ad80ab77be5d5974752e684b9350a86e77098cff910849c"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-b10688248a9a0a8577b81b6e5e3ac178ff73f084e219e6e77814400f7af45c70.json b/rust/cloud-storage/macro_db_client/.sqlx/query-b10688248a9a0a8577b81b6e5e3ac178ff73f084e219e6e77814400f7af45c70.json
deleted file mode 100644
index 9251692ab1..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-b10688248a9a0a8577b81b6e5e3ac178ff73f084e219e6e77814400f7af45c70.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n d.name\n FROM\n \"Document\" d\n WHERE\n d.id = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "name",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "b10688248a9a0a8577b81b6e5e3ac178ff73f084e219e6e77814400f7af45c70"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-b3766104e7a069d3c092841e0d5944beeff5f96be5d9a98eb1645202f585da81.json b/rust/cloud-storage/macro_db_client/.sqlx/query-b3766104e7a069d3c092841e0d5944beeff5f96be5d9a98eb1645202f585da81.json
deleted file mode 100644
index a46188620b..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-b3766104e7a069d3c092841e0d5944beeff5f96be5d9a98eb1645202f585da81.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n e.subject\n FROM\n \"email_messages\" e\n WHERE\n e.thread_id = $1\n LIMIT 1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "subject",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- true
- ]
- },
- "hash": "b3766104e7a069d3c092841e0d5944beeff5f96be5d9a98eb1645202f585da81"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-b905b3cbb97bbfc8262ffb91023db7f703b0994d6d519fbab0fdb67ea2df5877.json b/rust/cloud-storage/macro_db_client/.sqlx/query-b905b3cbb97bbfc8262ffb91023db7f703b0994d6d519fbab0fdb67ea2df5877.json
new file mode 100644
index 0000000000..1037b40923
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-b905b3cbb97bbfc8262ffb91023db7f703b0994d6d519fbab0fdb67ea2df5877.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n c.name,\n c.\"userId\" as user_id\n FROM\n \"Chat\" c\n WHERE\n c.id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "user_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "b905b3cbb97bbfc8262ffb91023db7f703b0994d6d519fbab0fdb67ea2df5877"
+}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-c18576c18caa0b7e23198ee55981cb8d7b9859dfe4272a92cbb29a4b64a1d695.json b/rust/cloud-storage/macro_db_client/.sqlx/query-c18576c18caa0b7e23198ee55981cb8d7b9859dfe4272a92cbb29a4b64a1d695.json
new file mode 100644
index 0000000000..f81d0eeecc
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-c18576c18caa0b7e23198ee55981cb8d7b9859dfe4272a92cbb29a4b64a1d695.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n d.name,\n d.owner\n FROM\n \"Document\" d\n WHERE\n d.id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "c18576c18caa0b7e23198ee55981cb8d7b9859dfe4272a92cbb29a4b64a1d695"
+}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-cf1161cb3ef4f573dd8e15092ebedc6cfa7e0f8b9f8b3c3a698991d37860c76f.json b/rust/cloud-storage/macro_db_client/.sqlx/query-cf1161cb3ef4f573dd8e15092ebedc6cfa7e0f8b9f8b3c3a698991d37860c76f.json
new file mode 100644
index 0000000000..ca358e80ad
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-cf1161cb3ef4f573dd8e15092ebedc6cfa7e0f8b9f8b3c3a698991d37860c76f.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n c.name as \"name?\",\n c.owner_id\n FROM\n \"comms_channels\" c\n WHERE\n c.id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "name?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 1,
+ "name": "owner_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ false
+ ]
+ },
+ "hash": "cf1161cb3ef4f573dd8e15092ebedc6cfa7e0f8b9f8b3c3a698991d37860c76f"
+}
diff --git a/rust/cloud-storage/macro_db_client/src/entity_name.rs b/rust/cloud-storage/macro_db_client/src/entity_name.rs
index 21e9404cd9..b0781c4367 100644
--- a/rust/cloud-storage/macro_db_client/src/entity_name.rs
+++ b/rust/cloud-storage/macro_db_client/src/entity_name.rs
@@ -1,18 +1,20 @@
use models_opensearch::SearchEntityType;
-/// Gets the entity name for the provided entity id and entity type
+/// Gets the entity name and macro user id of the owner for the provided
+/// entity id and entity type
#[tracing::instrument(skip(db), err)]
-pub async fn get_entity_name(
+pub async fn get_entity_name_and_owner(
db: &sqlx::PgPool,
entity_id: &uuid::Uuid,
entity_type: &SearchEntityType,
-) -> anyhow::Result<Option<String>> {
- let name: Option<String> = match entity_type {
+) -> anyhow::Result<(Option<String>, String)> {
+ let result: (Option<String>, String) = match entity_type {
SearchEntityType::Chats => {
sqlx::query!(
r#"
SELECT
- c.name
+ c.name,
+ c."userId" as user_id
FROM
"Chat" c
WHERE
@@ -20,7 +22,7 @@ pub async fn get_entity_name(
"#,
&entity_id.to_string(),
)
- .map(|row| Some(row.name))
+ .map(|row| (Some(row.name), row.user_id))
.fetch_one(db)
.await?
}
@@ -28,7 +30,8 @@ pub async fn get_entity_name(
sqlx::query!(
r#"
SELECT
- d.name
+ d.name,
+ d.owner
FROM
"Document" d
WHERE
@@ -36,7 +39,7 @@ pub async fn get_entity_name(
"#,
&entity_id.to_string(),
)
- .map(|row| Some(row.name))
+ .map(|row| (Some(row.name), row.owner))
.fetch_one(db)
.await?
}
@@ -44,16 +47,18 @@ pub async fn get_entity_name(
sqlx::query!(
r#"
SELECT
- e.subject
+ e.subject,
+ l.macro_id
FROM
"email_messages" e
+ JOIN email_links l ON l.id = e.link_id
WHERE
e.thread_id = $1
LIMIT 1
"#,
entity_id,
)
- .map(|row| row.subject)
+ .map(|row| (row.subject, row.macro_id))
.fetch_one(db)
.await?
}
@@ -61,7 +66,8 @@ pub async fn get_entity_name(
sqlx::query!(
r#"
SELECT
- c.name as "name?"
+ c.name as "name?",
+ c.owner_id
FROM
"comms_channels" c
WHERE
@@ -69,7 +75,7 @@ pub async fn get_entity_name(
"#,
entity_id,
)
- .map(|row| row.name)
+ .map(|row| (row.name, row.owner_id))
.fetch_one(db)
.await?
}
@@ -78,7 +84,7 @@ pub async fn get_entity_name(
}
};
- Ok(name)
+ Ok(result)
}
#[cfg(test)]
diff --git a/rust/cloud-storage/macro_db_client/src/entity_name/test.rs b/rust/cloud-storage/macro_db_client/src/entity_name/test.rs
index a61c26dc6b..2eff1acdb2 100644
--- a/rust/cloud-storage/macro_db_client/src/entity_name/test.rs
+++ b/rust/cloud-storage/macro_db_client/src/entity_name/test.rs
@@ -1,22 +1,40 @@
use super::*;
#[sqlx::test(fixtures(path = "../../fixtures", scripts("get_entity_name")))]
-async fn test_get_entity_name(pool: sqlx::Pool<sqlx::Postgres>) -> anyhow::Result<()> {
+async fn test_get_entity_name_and_owner(pool: sqlx::Pool<sqlx::Postgres>) -> anyhow::Result<()> {
let entity_id = uuid::Uuid::parse_str("11111111-1111-1111-1111-111111111111")?;
- let entity_name = get_entity_name(&pool, &entity_id, &SearchEntityType::Documents).await?;
- assert_eq!(entity_name, Some("document".to_string()));
+ let entity_name =
+ get_entity_name_and_owner(&pool, &entity_id, &SearchEntityType::Documents).await?;
+ assert_eq!(
+ entity_name,
+ (
+ Some("document".to_string()),
+ "macro|[email protected]".to_string()
+ )
+ );
- let entity_name = get_entity_name(&pool, &entity_id, &SearchEntityType::Chats).await?;
- assert_eq!(entity_name, Some("chat".to_string()));
+ let entity_name =
+ get_entity_name_and_owner(&pool, &entity_id, &SearchEntityType::Chats).await?;
+ assert_eq!(
+ entity_name,
+ (Some("chat".to_string()), "macro|[email protected]".to_string())
+ );
- let entity_name = get_entity_name(&pool, &entity_id, &SearchEntityType::Emails).await?;
- assert_eq!(entity_name, Some("subject".to_string()));
+ let entity_name =
+ get_entity_name_and_owner(&pool, &entity_id, &SearchEntityType::Emails).await?;
+ assert_eq!(
+ entity_name,
+ (
+ Some("subject".to_string()),
+ "macro|[email protected]".to_string()
+ )
+ );
- let result = get_entity_name(&pool, &entity_id, &SearchEntityType::Projects).await;
+ let result = get_entity_name_and_owner(&pool, &entity_id, &SearchEntityType::Projects).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "entity type not supported");
- let result = get_entity_name(
+ let result = get_entity_name_and_owner(
&pool,
&macro_uuid::generate_uuid_v7(),
&SearchEntityType::Documents,
diff --git a/rust/cloud-storage/opensearch_client/src/upsert/name.rs b/rust/cloud-storage/opensearch_client/src/upsert/name.rs
index d0a3760317..6b0ff0b0a8 100644
--- a/rust/cloud-storage/opensearch_client/src/upsert/name.rs
+++ b/rust/cloud-storage/opensearch_client/src/upsert/name.rs
@@ -9,6 +9,8 @@ use crate::{Result, error::ResponseExt};
pub struct UpsertEntityNameArgs {
/// The entity id
pub entity_id: String,
+ /// The macro user id who created the entity
+ pub user_id: String,
/// The entity name
pub name: String,
/// The entity type
diff --git a/rust/cloud-storage/search_processing_service/src/process/name.rs b/rust/cloud-storage/search_processing_service/src/process/name.rs
index 58149c9dce..180efa8c46 100644
--- a/rust/cloud-storage/search_processing_service/src/process/name.rs
+++ b/rust/cloud-storage/search_processing_service/src/process/name.rs
@@ -10,10 +10,12 @@ pub async fn upsert_name(
db: &sqlx::Pool<sqlx::Postgres>,
message: &UpdateEntityName,
) -> anyhow::Result<()> {
- // Get entity name from db
- let entity_name =
- macro_db_client::entity_name::get_entity_name(db, &message.entity_id, &message.entity_type)
- .await?;
+ let (entity_name, user_id) = macro_db_client::entity_name::get_entity_name_and_owner(
+ db,
+ &message.entity_id,
+ &message.entity_type,
+ )
+ .await?;
let name = if let Some(entity_name) = entity_name {
entity_name
@@ -30,6 +32,7 @@ pub async fn upsert_name(
.upsert_entity_name(&UpsertEntityNameArgs {
entity_id: message.entity_id.to_string(),
name,
+ user_id,
entity_type: message.entity_type.clone(),
})
.await?;