Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
chore(search): drop one-time alias migration docs and helpers (#3170)
gbirman764046ddb3fe
diff --git a/infra/stacks/opensearch/helpers/README.md b/infra/stacks/opensearch/helpers/README.mdindex 8135e1727f..e5a8af890b 100644--- a/infra/stacks/opensearch/helpers/README.md+++ b/infra/stacks/opensearch/helpers/README.md@@ -19,13 +19,13 @@ Then run `bun scripts/${OPERATION}.ts` to perform an operation.Application code (Rust) reads/writes via stable alias names defined in`SearchIndex` / `OpenSearchEntityType::index_name()`:-| Alias | Underlying index (current) |-| -------------- | -------------------------- |-| `channels` | `channels_v1` |-| `chats` | `chats_v1` |-| `documents` | `documents_v1` |-| `emails` | `emails_v1` |-| `call_records` | `call_records_v1` |+| Alias | Underlying index |+| -------------- | ---------------- |+| `channels` | `channels_v1` |+| `chats` | `chats_v1` |+| `documents` | `documents_v1` |+| `emails` | `emails_v1` |+| `call_records` | `call_records_v1`|The alias is the contract; the physical index is an implementation detailthat can be swapped without a code deploy.@@ -35,145 +35,61 @@ that can be swapped without a code deploy.| Script | Purpose || ---------------------------- | ---------------------------------------------------------------------- || `verify_aliases.ts` | Pre/post-flight check: alias exists and points at the expected index. |-| `add_alias.ts` | Idempotent additive alias (no reindex). Use to dual-alias an index. |+| `add_alias.ts` | Idempotent additive alias (no reindex). || `reindex_with_alias_swap.ts` | Reindex + atomic swap (handles `remove_index` for bare physical case). || `create_indices.ts` | Idempotent first-time creation of every versioned index + alias. |-All three migration scripts default to `DRY_RUN=true`; pass `DRY_RUN=false` to apply.+All migration scripts default to `DRY_RUN=true`; pass `DRY_RUN=false` to apply.-## Deploy ordering for this PR (read me first)--The Rust SearchIndex enum starts using alias names for every index when-this PR ships. Of the five aliases the new code references, four happen-to coincide with the existing physical index name in dev/prod-(`channels`, `chats`, `documents`, `call_records`), so OpenSearch-resolves the write correctly even before any alias is added — those-require **no** pre-merge action. The fifth, `emails`, does not exist-yet (the index lives at `emails_v2` under the legacy `emails_alias`),-so post-deploy email writes/reads would 404.--Required pre-merge step in each environment:--```sh-DRY_RUN=false bun scripts/add_alias.ts emails emails_v2-bun scripts/verify_aliases.ts # emails entry should now show -> emails_v2-```--This is purely additive — old code keeps writing through `emails_alias`,-new code writes through `emails`, both resolve to `emails_v2`. Drop-`emails_alias` after the new code is fully rolled out.--The bare-physical → versioned migrations (`channels` → `channels_v1`,-etc.) and the `emails_v2` → `emails_v1` standardisation are independent-follow-up operations that can be scheduled per index using the playbook-below; they are not deploy-blocking for this PR.--## Migration playbook: bringing an existing environment to alias-based access--Each environment starts in a different state. Before deploying code that-relies on the new alias names, run the playbook for whichever index is-not yet behind the expected alias. The order minimises the write window-where new code could land before its alias is in place.--Pre-flight: see what's missing.--```sh-bun scripts/verify_aliases.ts-```+## Runbook: reindex with new mapping (zero downtime)-The output flags each alias that's missing, points at the wrong index,-or is currently a physical index (which needs a reindex). Three states-show up in practice:--1. **Bare physical index** (`channels`, `chats`, `documents`,- `call_records` in pre-migration envs). The name is a physical index- with no alias of the same name. Resolution: reindex + swap.-2. **Already aliased under a legacy name** (`emails_v2` aliased as- `emails_alias`). The new code wants alias `emails`. Resolution: add- the new alias name additively, then drop the legacy alias once the- deploy is in.-3. **Mismatched physical index** (`emails` alias pointing at `emails_v2`- when the canonical name is `emails_v1`). Resolution: reindex + swap.--### State 1: bare physical index → versioned index behind alias--```sh-# 1. Create the new empty versioned physical index. Idempotent.-# create_indices.ts detects that the alias name (e.g. "channels") is-# currently a bare physical index and creates the new versioned index-# *without* attempting to add the alias — the alias has to be added-# atomically with the deletion of the conflicting physical index, and-# that's the swap script's job.-bun scripts/create_indices.ts--# 2. Dry run the swap. Confirm the actions list looks correct.-bun scripts/reindex_with_alias_swap.ts channels channels_v1--# 3. Apply. The script reindexes channels -> channels_v1, validates doc-# counts, then issues an atomic _aliases call:-# [{ remove_index: { index: "channels" } },-# { add: { index: "channels_v1", alias: "channels" } }]-DRY_RUN=false bun scripts/reindex_with_alias_swap.ts channels channels_v1--# 4. Replay writes that landed during the reindex window via the-# search_processing_service backfill endpoints. Bound the window with-# `since` set to just before step 3 started.--# 5. Re-run verify.-bun scripts/verify_aliases.ts-```+Use when you need to change a mapping that requires a full reindex (e.g.+field type change, analyzer change, breaking schema migration).-### State 2: legacy alias → add canonical alias additively, then drop legacy+1. **Create the new physical index** at the next version. Either bump the+ version in `constants.ts` and run `bun scripts/create_indices.ts`, or+ create directly via the OpenSearch API. Example: `documents_v2`.-For `emails_v2` aliased as `emails_alias`, the new code uses alias-`emails`. The safe order is:+2. **Reindex + swap (dry run first)**:-```sh-# 1. Add the canonical alias on top of the existing physical index.-# This is purely additive — emails_alias keeps working.-DRY_RUN=false bun scripts/add_alias.ts emails emails_v2+ ```sh+ bun scripts/reindex_with_alias_swap.ts documents documents_v2+ ```-# 2. Verify both aliases now point at emails_v2.-bun scripts/verify_aliases.ts+ This reads the current index behind the `documents` alias, reindexes+ into `documents_v2`, validates doc counts, and prints the `_aliases`+ actions it would apply. Nothing is changed.-# 3. Deploy the new application code. Old code still writes via-# emails_alias; new code writes via emails. Both resolve to emails_v2.+3. **Apply the swap**:-# 4. Once the new code is fully rolled out and stable, drop the legacy-# alias via the OpenSearch _aliases API:-# POST /_aliases { "actions": [{ "remove": { "index": "emails_v2", "alias": "emails_alias" }}] }-```+ ```sh+ DRY_RUN=false bun scripts/reindex_with_alias_swap.ts documents documents_v2+ ```-### State 3: standardise emails_v2 onto emails_v1 (optional cleanup)+ The script issues a single `_aliases` request that atomically removes+ the alias from the old index and adds it to the new one.-To bring the email physical index in line with the rest of the v1-naming, run the swap script after the alias is in place:+4. **Verify**: writes through the alias now land in `documents_v2`. Check+ doc counts continue to grow on the new index.-```sh-# Prereq: the canonical `emails` alias already points at emails_v2 (state 2).-# 1. Create emails_v1 with the same mapping. create_indices.ts notices-# that the `emails` alias already points at emails_v2 (a different-# index), so it creates emails_v1 *without* attempting to add the-# alias. The swap in step 2 handles that atomically.-bun scripts/create_indices.ts+5. **Replay** any writes that landed during the reindex window via the+ `search_processing_service` backfill endpoints (filter by `since` to+ bound work).-# 2. Dry run + apply.-bun scripts/reindex_with_alias_swap.ts emails emails_v1-DRY_RUN=false bun scripts/reindex_with_alias_swap.ts emails emails_v1+6. **Drop the old index** once you're confident:-# 3. Replay writes from the reindex window via backfill.-# 4. Drop the old physical index.-bun scripts/delete_indices.ts emails_v2-```+ ```sh+ bun scripts/delete_indices.ts documents_v1+ ```-### Avoiding downtime — write window strategy+### Write window strategyThe reindex script submits the reindex *async* (`wait_for_completion=false`)with `slices=auto` (one sub-task per primary shard) and polls the OpenSearchtask API every `REINDEX_POLL_SECONDS` (default 10s) until it completes,then issues the atomic alias swap. The async submission keeps a 60+ minute-documents reindex from being killed by the ALB / proxy idle timeout that a-synchronous request would hit.+reindex from being killed by the ALB / proxy idle timeout that a synchronous+request would hit.Tunable env vars:@@ -192,73 +108,13 @@ cut off when the alias swaps. Two options:arrived during reindex onto the new index. Idempotent on `_id`, sore-running is safe.- **Pause writers**: stop the producers (SQS consumers in- search_processing_service) before the reindex, drain the queue, then+ `search_processing_service`) before the reindex, drain the queue, thenreindex with no live writes. Lower risk, but causes a real pause infreshness rather than a backfill catch-up.The reindex script refuses the swap when the destination has fewer docsthan the source, so it won't silently complete a half-finished migration.-## Runbook: reindex with new mapping (zero downtime)--Use when you need to change a mapping that requires a full reindex (e.g.-field type change, analyzer change, breaking schema migration).--1. **Create the new physical index** at the next version. Either bump the- version in `constants.ts` and run `bun scripts/create_indices.ts`, or- create directly via the OpenSearch API. Example: `documents_v2`.--2. **Reindex + swap (dry run first)**:-- ```sh- bun scripts/reindex_with_alias_swap.ts documents documents_v2- ```-- This reads the current index behind the `documents` alias, reindexes- into `documents_v2`, validates doc counts, and prints the `_aliases`- actions it would apply. Nothing is changed.--3. **Pause writers** (or accept that writes during reindex go to the old- index and need to be replayed). Backfill jobs are the most common- replay path — see `search_processing_service` backfill endpoints.--4. **Apply the swap**:-- ```sh- DRY_RUN=false bun scripts/reindex_with_alias_swap.ts documents documents_v2- ```-- The script issues a single `_aliases` request that atomically removes- the alias from the old index and adds it to the new one (or- `remove_index` + `add` when the alias name was previously a physical- index).--5. **Verify**: writes through the alias now land in `documents_v2`. Check- doc counts continue to grow on the new index. Use the search API to- confirm reads return expected results.--6. **Replay** any writes that landed during the reindex window via the- backfill endpoints (filter by `since` to bound work).--7. **Drop the old index** once you're confident:-- ```sh- bun scripts/delete_indices.ts "documents_v1"- ```--### Promoting an existing physical index to live behind an alias--If an environment still has a raw physical index sharing the alias name-(e.g. an older `channels` index), the swap script handles this — it-detects the conflict and emits `remove_index` + `add` in the same atomic-actions list. Run the migration to a versioned name:--```sh-# 1. Create the new versioned index with the desired mapping (use create_indices.ts after bumping versions, or call the API directly).-# 2. Reindex + swap. The script will detect that "channels" is currently a physical index and handle removal atomically with the alias add.-DRY_RUN=false bun scripts/reindex_with_alias_swap.ts channels channels_v1-```-### Dry-run verificationAlways run the script with `DRY_RUN=true` (the default) first. The output@@ -269,8 +125,8 @@ this before applying. Example output:[DRY-RUN] Would run _aliases with actions:{"actions": [- { "remove_index": { "index": "documents" } },- { "add": { "index": "documents_v1", "alias": "documents" } }+ { "remove": { "index": "documents_v1", "alias": "documents" } },+ { "add": { "index": "documents_v2", "alias": "documents" } }]}```diff --git a/infra/stacks/opensearch/helpers/scripts/add_alias.ts b/infra/stacks/opensearch/helpers/scripts/add_alias.tsindex 7f939bf8b0..3a02d4992e 100644--- a/infra/stacks/opensearch/helpers/scripts/add_alias.ts+++ b/infra/stacks/opensearch/helpers/scripts/add_alias.ts@@ -3,10 +3,8 @@* existing physical index without disrupting any other alias already* pointing at it.*- * Use during the deploy-prep phase to introduce the new alias name (e.g.- * `emails`) alongside a legacy alias (`emails_alias`) on the same physical- * index, so the new code can deploy safely before the legacy alias is- * dropped.+ * Errors if the alias name is currently a physical index — use+ * reindex_with_alias_swap.ts for that case.** Usage:* bun scripts/add_alias.ts <alias> <index>diff --git a/infra/stacks/opensearch/helpers/scripts/create_emails_v2.ts b/infra/stacks/opensearch/helpers/scripts/create_emails_v2.tsdeleted file mode 100644index 1cdaf50d99..0000000000--- a/infra/stacks/opensearch/helpers/scripts/create_emails_v2.ts+++ /dev/null@@ -1,98 +0,0 @@-require('dotenv').config();--import { client } from '../client';-import { IS_DRY_RUN, SHARD_SETTINGS } from '../constants';--const EMAIL_INDEX_V2 = 'emails_v2';--const EMAIL_MAPPINGS = {- dynamic: 'false' as const,- properties: {- entity_id: { type: 'keyword' as const },- message_id: { type: 'keyword' as const },- thread_id: { type: 'keyword' as const },- user_id: { type: 'keyword' as const },- link_id: { type: 'keyword' as const },- sender: { type: 'keyword' as const },- recipients: { type: 'keyword' as const },- cc: { type: 'keyword' as const },- bcc: { type: 'keyword' as const },- reply_to: {- type: 'keyword' as const,- index: true,- doc_values: true,- },- labels: { type: 'keyword' as const, index: false },- subject: {- type: 'text' as const,- fields: { keyword: { type: 'keyword' as const, ignore_above: 50 } },- },- content: { type: 'text' as const, analyzer: 'standard' },- sender_name: { type: 'text' as const, analyzer: 'standard' },- recipient_names: { type: 'text' as const, analyzer: 'standard' },- cc_names: { type: 'text' as const, analyzer: 'standard' },- bcc_names: { type: 'text' as const, analyzer: 'standard' },- sent_at: { type: 'date' as const, index: false },- sent_at_seconds: {- type: 'date' as const,- index: false,- format: 'epoch_second',- },- updated_at: { type: 'date' as const, index: false },- updated_at_seconds: {- type: 'date' as const,- index: false,- format: 'epoch_second',- },- },-};--async function createEmailsV2(dryRun: boolean) {- const opensearchClient = client();-- console.log('\n' + '='.repeat(60));- console.log(- `Create ${EMAIL_INDEX_V2} index ${dryRun ? '(DRY-RUN MODE)' : '(LIVE MODE)'}`- );- console.log('='.repeat(60));-- const exists = (- await opensearchClient.indices.exists({ index: EMAIL_INDEX_V2 })- ).body;-- if (exists) {- console.log(`Index "${EMAIL_INDEX_V2}" already exists. Aborting.`);- return;- }-- if (dryRun) {- console.log('\n[DRY-RUN] Would create index with settings:');- console.log(JSON.stringify(SHARD_SETTINGS, null, 2));- console.log('\n[DRY-RUN] Would create index with mappings:');- console.log(JSON.stringify(EMAIL_MAPPINGS, null, 2));- console.log('\nTo run for real, set DRY_RUN=false');- return;- }-- await opensearchClient.indices.create({- index: EMAIL_INDEX_V2,- body: {- settings: SHARD_SETTINGS,- mappings: EMAIL_MAPPINGS,- },- });-- console.log(`\nCreated ${EMAIL_INDEX_V2}`);-- console.log('\n' + '='.repeat(60));- console.log('Done! Next steps:');- console.log(' 1. Deploy code that uses emails_alias');- console.log(- ' 2. Swap alias: POST /_aliases { "actions": [{ "remove": { "index": "emails", "alias": "emails_alias" }}, { "add": { "index": "emails_v2", "alias": "emails_alias" }}] }'- );- console.log(' 3. Backfill emails_v2');- console.log(' 4. Delete old emails index');- console.log('='.repeat(60));-}--createEmailsV2(IS_DRY_RUN);