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.

2.3 KB
Raw Blame
require('dotenv').config();

import { client } from '../client';
import {
  CHANNEL_INDEX,
  CHAT_INDEX,
  DOCUMENT_INDEX,
  EMAIL_INDEX,
} from '../constants';
import { type IndexMigration, migrateIndex } from '../utils/migrate_field';

const MIGRATIONS: IndexMigration[] = [
  {
    indexName: CHANNEL_INDEX,
    fields: [
      { oldField: 'created_at', newField: 'created_at_seconds' },
      { oldField: 'updated_at', newField: 'updated_at_seconds' },
    ],
  },
  {
    indexName: CHAT_INDEX,
    fields: [{ oldField: 'updated_at', newField: 'updated_at_seconds' }],
  },
  {
    indexName: DOCUMENT_INDEX,
    fields: [{ oldField: 'updated_at', newField: 'updated_at_seconds' }],
  },
  {
    indexName: EMAIL_INDEX,
    fields: [
      { oldField: 'updated_at', newField: 'updated_at_seconds' },
      { oldField: 'sent_at', newField: 'sent_at_seconds' },
    ],
  },
];

async function copyData(dryRun: boolean = true) {
  const opensearchClient = client();

  console.log('\n' + '='.repeat(60));
  console.log(
    `Copy Timestamp Data ${dryRun ? '(DRY-RUN MODE)' : '(LIVE MODE)'}`
  );
  console.log('='.repeat(60));
  console.log(
    '\nThis script copies timestamp data from old fields to *_seconds fields.'
  );
  console.log('It only updates documents where the new field does not exist.');
  console.log("\n💡 Safe to run multiple times - it's idempotent!");

  if (dryRun) {
    console.log('\n⚠️  DRY-RUN MODE: No changes will be made to the database');
  } else {
    console.log('\n🚨 LIVE MODE: Data will be copied');
  }

  try {
    for (const migration of MIGRATIONS) {
      await migrateIndex(opensearchClient, migration, dryRun);
    }

    console.log('\n' + '='.repeat(60));
    console.log('Data copy completed successfully!');
    console.log('='.repeat(60));

    if (dryRun) {
      console.log(
        '\nTo run for real, set DRY_RUN=false environment variable\n'
      );
    } else {
      console.log(
        '\n✓ All timestamp data has been copied to *_seconds fields.'
      );
      console.log(
        '💡 Run this script again after deploying new code to catch any documents'
      );
      console.log('   added during the migration.\n');
    }
  } catch (error) {
    console.error('\n❌ Data copy failed:', error);
    throw error;
  }
}

const isDryRun = process.env.DRY_RUN !== 'false';

copyData(isDryRun);