The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Keep 2.46 instance commands safe for a rolling deploy and large tables (#27540)
Félix Malfait05e6c9828af0
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791215192958-add-upgrade-migration-workspace-id-created-at-index.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791215192958-add-upgrade-migration-workspace-id-created-at-index.tsindex d1e621553b2..83cf1b15a3a 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791215192958-add-upgrade-migration-workspace-id-created-at-index.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791215192958-add-upgrade-migration-workspace-id-created-at-index.ts@@ -3,17 +3,10 @@ import { type QueryRunner } from 'typeorm';import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';import { type FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';+// No-op, built concurrently by a slow command: kept so recorded upgrade cursors still resolve@RegisteredInstanceCommand('2.46.0', 1791215192958)export class AddUpgradeMigrationWorkspaceIdCreatedAtIndexFastInstanceCommand implements FastInstanceCommand {- public async up(queryRunner: QueryRunner): Promise<void> {- await queryRunner.query(- 'CREATE INDEX IF NOT EXISTS "IDX_UPGRADE_MIGRATION_WORKSPACE_ID_CREATED_AT" ON "core"."upgradeMigration" ("workspaceId", "createdAt") WHERE "workspaceId" IS NOT NULL',- );- }+ public async up(_queryRunner: QueryRunner): Promise<void> {}- public async down(queryRunner: QueryRunner): Promise<void> {- await queryRunner.query(- 'DROP INDEX IF EXISTS "core"."IDX_UPGRADE_MIGRATION_WORKSPACE_ID_CREATED_AT"',- );- }+ public async down(_queryRunner: QueryRunner): Promise<void> {}}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791216099453-drop-agent-evaluation-inputs.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791216099453-drop-agent-evaluation-inputs.tsindex 8f9729b1472..47050dfc485 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791216099453-drop-agent-evaluation-inputs.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791216099453-drop-agent-evaluation-inputs.ts@@ -1,19 +1,12 @@-import { QueryRunner } from 'typeorm';+import { type QueryRunner } from 'typeorm';import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';-import { FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';+import { type FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';+// No-op, the drop moved to 2.47 for 2.45 pods: kept so recorded upgrade cursors still resolve@RegisteredInstanceCommand('2.46.0', 1791216099453)export class DropAgentEvaluationInputsFastInstanceCommand implements FastInstanceCommand {- public async up(queryRunner: QueryRunner): Promise<void> {- await queryRunner.query(- `ALTER TABLE "core"."agent" DROP COLUMN IF EXISTS "evaluationInputs"`,- );- }+ public async up(_queryRunner: QueryRunner): Promise<void> {}- public async down(queryRunner: QueryRunner): Promise<void> {- await queryRunner.query(- `ALTER TABLE "core"."agent" ADD COLUMN IF NOT EXISTS "evaluationInputs" text array NOT NULL DEFAULT '{}'`,- );- }+ public async down(_queryRunner: QueryRunner): Promise<void> {}}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791375726691-add-two-factor-authentication-recovery-code-index.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791375726691-add-two-factor-authentication-recovery-code-index.tsindex feac478f503..95b05b3e548 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791375726691-add-two-factor-authentication-recovery-code-index.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791375726691-add-two-factor-authentication-recovery-code-index.ts@@ -1,19 +1,12 @@-import { QueryRunner } from 'typeorm';+import { type QueryRunner } from 'typeorm';import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';-import { FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';+import { type FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';+// No-op, built concurrently by a slow command: kept so recorded upgrade cursors still resolve@RegisteredInstanceCommand('2.46.0', 1791375726691)export class AddTwoFactorAuthenticationRecoveryCodeIndexFastInstanceCommand implements FastInstanceCommand {- public async up(queryRunner: QueryRunner): Promise<void> {- await queryRunner.query(- 'CREATE UNIQUE INDEX "IDX_APP_TOKEN_RECOVERY_CODE_PENDING_UNIQUE" ON "core"."appToken" ("userId", "workspaceId") WHERE "type" = \'TWO_FACTOR_AUTHENTICATION_RECOVERY_CODE\' AND "deletedAt" IS NULL AND "revokedAt" IS NULL',- );- }+ public async up(_queryRunner: QueryRunner): Promise<void> {}- public async down(queryRunner: QueryRunner): Promise<void> {- await queryRunner.query(- 'DROP INDEX "core"."IDX_APP_TOKEN_RECOVERY_CODE_PENDING_UNIQUE"',- );- }+ public async down(_queryRunner: QueryRunner): Promise<void> {}}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524804-add-two-factor-authentication-recovery-code-index-concurrently.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524804-add-two-factor-authentication-recovery-code-index-concurrently.tsnew file mode 100644index 00000000000..60be01ab3ee--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524804-add-two-factor-authentication-recovery-code-index-concurrently.ts@@ -0,0 +1,30 @@+import { type DataSource, type QueryRunner } from 'typeorm';++import { createCoreIndexConcurrently } from 'src/database/commands/upgrade-version-command/2-46/utils/create-core-index-concurrently.util';+import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';+import { type SlowInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/slow-instance-command.interface';++const INDEX_NAME = 'IDX_APP_TOKEN_RECOVERY_CODE_PENDING_UNIQUE';++const CREATE_INDEX_QUERY = `CREATE UNIQUE INDEX IF NOT EXISTS "${INDEX_NAME}" ON "core"."appToken" ("userId", "workspaceId") WHERE "type" = 'TWO_FACTOR_AUTHENTICATION_RECOVERY_CODE' AND "deletedAt" IS NULL AND "revokedAt" IS NULL`;++// appToken holds every refresh token, so building this index under a lock+// would block logins and token refreshes for the whole build+@RegisteredInstanceCommand('2.46.0', 1791401524804, { type: 'slow' })+export class AddTwoFactorAuthenticationRecoveryCodeIndexConcurrentlySlowInstanceCommand implements SlowInstanceCommand {+ async runDataMigration(dataSource: DataSource): Promise<void> {+ await createCoreIndexConcurrently({+ dataSource,+ indexName: INDEX_NAME,+ createIndexQuery: CREATE_INDEX_QUERY,+ });+ }++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(CREATE_INDEX_QUERY);+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`DROP INDEX IF EXISTS "core"."${INDEX_NAME}"`);+ }+}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524805-add-upgrade-migration-workspace-id-created-at-index-concurrently.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524805-add-upgrade-migration-workspace-id-created-at-index-concurrently.tsnew file mode 100644index 00000000000..443a9d27f6a--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524805-add-upgrade-migration-workspace-id-created-at-index-concurrently.ts@@ -0,0 +1,30 @@+import { type DataSource, type QueryRunner } from 'typeorm';++import { createCoreIndexConcurrently } from 'src/database/commands/upgrade-version-command/2-46/utils/create-core-index-concurrently.util';+import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';+import { type SlowInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/slow-instance-command.interface';++const INDEX_NAME = 'IDX_UPGRADE_MIGRATION_WORKSPACE_ID_CREATED_AT';++const CREATE_INDEX_QUERY = `CREATE INDEX IF NOT EXISTS "${INDEX_NAME}" ON "core"."upgradeMigration" ("workspaceId", "createdAt") WHERE "workspaceId" IS NOT NULL`;++// upgradeMigration holds a row per workspace for every command, so building+// this index under a lock would block every workspace sign-up meanwhile+@RegisteredInstanceCommand('2.46.0', 1791401524805, { type: 'slow' })+export class AddUpgradeMigrationWorkspaceIdCreatedAtIndexConcurrentlySlowInstanceCommand implements SlowInstanceCommand {+ async runDataMigration(dataSource: DataSource): Promise<void> {+ await createCoreIndexConcurrently({+ dataSource,+ indexName: INDEX_NAME,+ createIndexQuery: CREATE_INDEX_QUERY,+ });+ }++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(CREATE_INDEX_QUERY);+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`DROP INDEX IF EXISTS "core"."${INDEX_NAME}"`);+ }+}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/utils/create-core-index-concurrently.util.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/utils/create-core-index-concurrently.util.tsnew file mode 100644index 00000000000..3a62268d1e5--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/utils/create-core-index-concurrently.util.ts@@ -0,0 +1,60 @@+import { DataSource } from 'typeorm';++// A concurrent build can outlast the core pool's request query_timeout, so it+// runs on its own connection without one. A failed build leaves an invalid+// index that IF NOT EXISTS would then keep, so it is dropped first.+export const createCoreIndexConcurrently = async ({+ dataSource,+ indexName,+ createIndexQuery,+}: {+ dataSource: DataSource;+ indexName: string;+ createIndexQuery: string;+}): Promise<void> => {+ const coreDataSourceOptions = dataSource.options;++ if (coreDataSourceOptions.type !== 'postgres') {+ throw new Error(+ `Concurrent index builds need a postgres datasource, got ${coreDataSourceOptions.type}`,+ );+ }++ const indexBuildDataSource = await new DataSource({+ ...coreDataSourceOptions,+ entities: [],+ migrations: [],+ subscribers: [],+ poolSize: 1,+ extra: {+ ...coreDataSourceOptions.extra,+ query_timeout: undefined,+ },+ }).initialize();++ try {+ const invalidIndexes: { name: string }[] =+ await indexBuildDataSource.query(+ `SELECT index_class.relname AS name+ FROM pg_index+ JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid+ JOIN pg_namespace namespace ON namespace.oid = index_class.relnamespace+ WHERE namespace.nspname = 'core'+ AND index_class.relname = $1+ AND NOT pg_index.indisvalid`,+ [indexName],+ );++ if (invalidIndexes.length > 0) {+ await indexBuildDataSource.query(+ `DROP INDEX CONCURRENTLY IF EXISTS "core"."${indexName}"`,+ );+ }++ await indexBuildDataSource.query(+ createIndexQuery.replace(' INDEX ', ' INDEX CONCURRENTLY '),+ );+ } finally {+ await indexBuildDataSource.destroy();+ }+};diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791401524803-drop-agent-evaluation-inputs.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791401524803-drop-agent-evaluation-inputs.tsnew file mode 100644index 00000000000..a1388f1ddd6--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791401524803-drop-agent-evaluation-inputs.ts@@ -0,0 +1,21 @@+import { type QueryRunner } from 'typeorm';++import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';+import { type FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';++// 2.46 stopped reading evaluationInputs, and 2.45 servers still map it while+// 2.46 rolls out, so it is dropped one release later+@RegisteredInstanceCommand('2.47.0', 1791401524803)+export class DropAgentEvaluationInputsDeferredFastInstanceCommand implements FastInstanceCommand {+ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(+ `ALTER TABLE "core"."agent" DROP COLUMN IF EXISTS "evaluationInputs"`,+ );+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(+ `ALTER TABLE "core"."agent" ADD COLUMN IF NOT EXISTS "evaluationInputs" text array NOT NULL DEFAULT '{}'`,+ );+ }+}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.tsindex 4de89244b33..2f7cadc2080 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts@@ -230,6 +230,9 @@ import { CreatePendingWakeUpTableFastInstanceCommand } from 'src/database/commanimport { CreateAgentRunSuspensionTableFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791301683545-create-agent-run-suspension-table';import { AddUserApplicationVariableValueFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791328043361-add-user-application-variable-value';import { AddTwoFactorAuthenticationRecoveryCodeIndexFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791375726691-add-two-factor-authentication-recovery-code-index';+import { DropAgentEvaluationInputsDeferredFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791401524803-drop-agent-evaluation-inputs';+import { AddTwoFactorAuthenticationRecoveryCodeIndexConcurrentlySlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524804-add-two-factor-authentication-recovery-code-index-concurrently';+import { AddUpgradeMigrationWorkspaceIdCreatedAtIndexConcurrentlySlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524805-add-upgrade-migration-workspace-id-created-at-index-concurrently';export const INSTANCE_COMMANDS = [AddViewFieldGroupIdIndexOnViewFieldFastInstanceCommand,@@ -462,4 +465,7 @@ export const INSTANCE_COMMANDS = [CreateAgentRunSuspensionTableFastInstanceCommand,AddUserApplicationVariableValueFastInstanceCommand,AddTwoFactorAuthenticationRecoveryCodeIndexFastInstanceCommand,+ DropAgentEvaluationInputsDeferredFastInstanceCommand,+ AddTwoFactorAuthenticationRecoveryCodeIndexConcurrentlySlowInstanceCommand,+ AddUpgradeMigrationWorkspaceIdCreatedAtIndexConcurrentlySlowInstanceCommand,];diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/entities/agent.entity.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/entities/agent.entity.tsindex 6a084b576ef..298087ca6b3 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/entities/agent.entity.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/entities/agent.entity.ts@@ -11,6 +11,7 @@ import {import { ADD_IS_SYSTEM_TO_AGENT_AND_WORKFLOW_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-46/add-is-system-to-agent-and-workflow-upgrade-command-name.constant';import { ADD_TRIGGERS_TO_AGENT_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-46/add-triggers-to-agent-upgrade-command-name.constant';import { WasIntroducedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator';+import { type WasRemovedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-removed-in-upgrade.decorator';import { AgentResponseFormat } from 'src/engine/metadata-modules/ai/ai-agent/types/agent-response-format.type';import { ModelConfiguration } from 'src/engine/metadata-modules/ai/ai-agent/types/model-configuration.type';import { AUTO_SELECT_WORKSPACE_DEFAULT_MODEL_ID } from 'twenty-shared/ai';@@ -85,4 +86,8 @@ export class AgentEntity})@Column({ nullable: false, type: 'jsonb', default: [] })triggers: JsonbProperty<AgentTrigger[]>;++ // dropped in 2.47; no @WasRemovedInUpgrade yet: 2.45 pods still map it mid rolling deploy+ @Column({ type: 'text', array: true, default: '{}' })+ evaluationInputs: WasRemovedInUpgrade<string[]>;}