Skip to content

The open alternative to Salesforce, designed for AI.

11.4 KB
Raw Blame
import { Injectable, Logger } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { DataSource, type QueryRunner } from 'typeorm';

import { AgentHistoryMigrationDataService } from 'src/database/commands/agent-history/agent-history-migration-data.service';
import { AgentHistoryMigrationValidationService } from 'src/database/commands/agent-history/agent-history-migration-validation.service';
import {
  AGENT_HISTORY_TABLES,
  ACTIVE_AGENT_HISTORY_TABLES,
  isActiveAgentHistoryTable,
} from 'src/database/commands/agent-history/agent-history-tables.constant';
import { getAgentHistoryTable } from 'src/database/commands/agent-history/utils/get-agent-history-table.util';
import { AGENT_HISTORY_MIGRATION_STORAGE_KEY } from 'src/database/commands/agent-history/agent-history-migration-storage-key.constant';
import { AgentHistoryMigrationStateService } from 'src/database/commands/agent-history/agent-history-migration-state.service';
import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';

type Storage = AgentHistoryMigrationState['storage'];

@Injectable()
export class AgentHistoryMigrationService {
  private readonly logger = new Logger(AgentHistoryMigrationService.name);

  constructor(
    @InjectDataSource() private readonly dataSource: DataSource,
    private readonly migrationStateService: AgentHistoryMigrationStateService,
    private readonly dataService: AgentHistoryMigrationDataService,
    private readonly validationService: AgentHistoryMigrationValidationService,
  ) {}

  async migrate({
    workspaceId,
    target,
    batchSize = 1000,
  }: {
    workspaceId: string;
    target: Storage;
    batchSize?: number;
  }): Promise<void> {
    if (!Number.isInteger(batchSize) || batchSize < 1 || batchSize > 10000) {
      throw new Error('Batch size must be between 1 and 10000');
    }
    const runner = this.dataSource.createQueryRunner('master');
    let ownsRunnerLock = false;
    const runnerKey = `${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:runner:${workspaceId}`;
    try {
      await runner.connect();
      const [{ acquired }]: { acquired: boolean }[] = await runner.query(
        'SELECT pg_try_advisory_lock(hashtextextended($1, 0)) AS acquired',
        [runnerKey],
      );
      ownsRunnerLock = acquired;
      if (!acquired) {
        throw new Error(
          'An agent history migration is already running for this workspace',
        );
      }
      let state = await this.migrationStateService.readState(
        runner,
        workspaceId,
      );
      if (state.migration?.phase === 'aborting') {
        throw new Error(
          'Finish aborting the incomplete migration before starting another copy',
        );
      }
      if (state.storage === target && !isDefined(state.migration)) {
        return;
      }
      if (isDefined(state.migration) && state.migration.target !== target) {
        throw new Error(
          'Resume the in-progress migration or abort it before changing direction',
        );
      }
      await this.validationService.validateSchema({ runner, workspaceId });
      if (!isDefined(state.migration)) {
        state = await this.fenced({
          runner,
          workspaceId,
          work: async () => {
            const current = await this.migrationStateService.readState(
              runner,
              workspaceId,
            );
            await this.validationService.assertNoActiveStreams({
              runner,
              workspaceId,
              source: current.storage,
            });
            await this.validationService.validateReferences({
              runner,
              workspaceId,
              storage: current.storage,
            });
            if (target === 'core') {
              await this.validationService.assertNoThreadTargets({
                runner,
                workspaceId,
              });
              await this.validationService.assertNoCoreIdCollisions({
                runner,
                workspaceId,
              });
            }
            const next: AgentHistoryMigrationState = {
              ...current,
              migration: {
                phase: 'clearing',
                target,
                tableIndex: 0,
                lastId: null,
              },
            };
            await this.migrationStateService.writeState(
              runner,
              workspaceId,
              next,
            );
            return next;
          },
        });
      }
      if (state.migration?.phase === 'clearing') {
        await this.dataService.clearStore({
          runner,
          workspaceId,
          storage: target,
        });
        state = await this.fenced({
          runner,
          workspaceId,
          work: async () => {
            const next: AgentHistoryMigrationState = {
              ...state,
              migration: {
                phase: 'copying',
                target,
                tableIndex: 0,
                lastId: null,
              },
            };
            await this.migrationStateService.writeState(
              runner,
              workspaceId,
              next,
            );
            return next;
          },
        });
      }
      while (
        isDefined(state.migration) &&
        state.migration.tableIndex < AGENT_HISTORY_TABLES.length
      ) {
        state = await this.fenced({
          runner,
          workspaceId,
          work: async () => {
            const current = await this.migrationStateService.readState(
              runner,
              workspaceId,
            );
            const progress = current.migration;
            if (!isDefined(progress) || progress.target !== target) {
              throw new Error('Migration state changed unexpectedly');
            }
            const table = AGENT_HISTORY_TABLES[progress.tableIndex];
            const ids = isActiveAgentHistoryTable(table)
              ? await this.dataService.copyBatch({
                  runner,
                  workspaceId,
                  table,
                  source: current.storage,
                  target,
                  lastId: progress.lastId,
                  batchSize,
                })
              : [];
            const next: AgentHistoryMigrationState = {
              ...current,
              migration: {
                phase: 'copying',
                target,
                tableIndex:
                  ids.length < batchSize
                    ? progress.tableIndex + 1
                    : progress.tableIndex,
                lastId: ids.length < batchSize ? null : ids[ids.length - 1],
              },
            };
            await this.migrationStateService.writeState(
              runner,
              workspaceId,
              next,
            );
            this.logger.log(
              `${workspaceId}: copied ${ids.length} ${table.name} rows`,
            );
            return next;
          },
        });
      }
      await this.fenced({
        runner,
        workspaceId,
        work: async () => {
          await this.validationService.verify({ runner, workspaceId });
          await this.validationService.validateReferences({
            runner,
            workspaceId,
            storage: target,
          });
          await this.migrationStateService.writeState(runner, workspaceId, {
            storage: target,
            verifiedAt: new Date().toISOString(),
          });
        },
      });
      this.logger.log(`${workspaceId}: verified and switched to ${target}`);
    } finally {
      if (runner.isTransactionActive) {
        await runner.rollbackTransaction();
      }
      if (ownsRunnerLock) {
        await runner.query(
          'SELECT pg_advisory_unlock(hashtextextended($1, 0))',
          [runnerKey],
        );
      }
      await runner.release();
    }
  }

  async abort({
    workspaceId,
    dryRun,
  }: {
    workspaceId: string;
    dryRun: boolean;
  }): Promise<void> {
    const runner = this.dataSource.createQueryRunner('master');
    const key = `${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:runner:${workspaceId}`;
    let ownsLock = false;
    try {
      await runner.connect();
      const [{ acquired }]: { acquired: boolean }[] = await runner.query(
        'SELECT pg_try_advisory_lock(hashtextextended($1, 0)) AS acquired',
        [key],
      );
      ownsLock = acquired;
      if (!acquired) {
        throw new Error('Stop the running migration before aborting');
      }
      const state = await this.migrationStateService.readState(
        runner,
        workspaceId,
      );
      if (!isDefined(state.migration)) {
        return;
      }
      if (dryRun) {
        this.logger.log(
          `[DRY RUN] ${workspaceId}: would discard ${state.migration.target} destination; ${JSON.stringify(state.migration)}`,
        );
        return;
      }
      await this.fenced({
        runner,
        workspaceId,
        work: () =>
          this.migrationStateService.writeState(runner, workspaceId, {
            ...state,
            migration: { ...state.migration!, phase: 'aborting' },
          }),
      });
      await this.dataService.clearStore({
        runner,
        workspaceId,
        storage: state.migration.target,
      });
      await this.fenced({
        runner,
        workspaceId,
        work: () =>
          this.migrationStateService.writeState(runner, workspaceId, {
            ...state,
            migration: undefined,
          }),
      });
    } finally {
      if (runner.isTransactionActive) {
        await runner.rollbackTransaction();
      }
      if (ownsLock) {
        await runner.query(
          'SELECT pg_advisory_unlock(hashtextextended($1, 0))',
          [key],
        );
      }
      await runner.release();
    }
  }

  async inspect(workspaceId: string): Promise<void> {
    const runner = this.dataSource.createQueryRunner('master');
    try {
      await runner.connect();
      const state = await this.migrationStateService.readState(
        runner,
        workspaceId,
      );
      this.logger.log(`${workspaceId}: ${JSON.stringify(state)}`);
      await this.validationService.assertNoActiveStreams({
        runner,
        workspaceId,
        source: state.storage,
      });
      await this.validationService.validateReferences({
        runner,
        workspaceId,
        storage: state.storage,
      });
      for (const table of ACTIVE_AGENT_HISTORY_TABLES) {
        const [{ count }] = await runner.query(
          `SELECT count(*) FROM ${getAgentHistoryTable({ workspaceId, storage: state.storage, name: table.name })} ${state.storage === 'core' ? 'WHERE "workspaceId" = $1' : ''}`,
          state.storage === 'core' ? [workspaceId] : [],
        );
        this.logger.log(`${workspaceId}: ${table.name}: ${count} source rows`);
      }
    } finally {
      await runner.release();
    }
  }

  private async fenced<TResult>({
    runner,
    workspaceId,
    work,
  }: {
    runner: QueryRunner;
    workspaceId: string;
    work: () => Promise<TResult>;
  }): Promise<TResult> {
    await runner.startTransaction();
    try {
      await runner.query(
        'SELECT pg_advisory_xact_lock(hashtextextended($1, 0))',
        [`${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:${workspaceId}`],
      );
      const result = await work();
      await runner.commitTransaction();
      return result;
    } catch (error) {
      await runner.rollbackTransaction();
      throw error;
    }
  }
}