diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-drop-workspace-messaging-fks.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-drop-workspace-messaging-fks.command.ts new file mode 100644 index 00000000000..a63c57a04f2 --- /dev/null +++ b/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-drop-workspace-messaging-fks.command.ts @@ -0,0 +1,94 @@ +import { Command } from 'nest-commander'; + +import { ActiveOrSuspendedWorkspaceCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspace.command-runner'; +import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service'; +import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner'; +import { WorkspaceSchemaManagerService } from 'src/engine/twenty-orm/workspace-schema-manager/workspace-schema-manager.service'; +import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util'; + +const FK_COLUMNS_TO_DROP = [ + { + tableName: 'messageChannelMessageAssociation', + columnName: 'messageChannelId', + }, + { + tableName: 'calendarChannelEventAssociation', + columnName: 'calendarChannelId', + }, + { tableName: 'messageFolder', columnName: 'messageChannelId' }, + { + tableName: 'messageChannelMessageAssociationMessageFolder', + columnName: 'messageFolderId', + }, +]; + +@Command({ + name: 'upgrade:1-21:drop-workspace-messaging-fks', + description: + 'Drop FK constraints from workspace messaging/calendar tables that now reference core schema entities', +}) +export class DropWorkspaceMessagingFksCommand extends ActiveOrSuspendedWorkspaceCommandRunner { + constructor( + protected readonly workspaceIteratorService: WorkspaceIteratorService, + private readonly workspaceSchemaManagerService: WorkspaceSchemaManagerService, + ) { + super(workspaceIteratorService); + } + + override async runOnWorkspace({ + workspaceId, + dataSource, + options, + }: RunOnWorkspaceArgs): Promise { + if (!dataSource) { + this.logger.log(`No data source for workspace ${workspaceId}, skipping`); + + return; + } + + const schemaName = getWorkspaceSchemaName(workspaceId); + const queryRunner = dataSource.createQueryRunner(); + + await queryRunner.connect(); + + try { + for (const { tableName, columnName } of FK_COLUMNS_TO_DROP) { + const foreignKeyName = + await this.workspaceSchemaManagerService.foreignKeyManager.getForeignKeyName( + { + queryRunner, + schemaName, + tableName, + columnName, + }, + ); + + if (!foreignKeyName) { + continue; + } + + if (options.dryRun) { + this.logger.log( + `[DRY RUN] Would drop FK ${foreignKeyName} from ${schemaName}.${tableName}`, + ); + continue; + } + + await this.workspaceSchemaManagerService.foreignKeyManager.dropForeignKey( + { + queryRunner, + schemaName, + tableName, + foreignKeyName, + }, + ); + + this.logger.log( + `Dropped FK ${foreignKeyName} from ${schemaName}.${tableName}`, + ); + } + } finally { + await queryRunner.release(); + } + } +} diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-upgrade-version-command.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-upgrade-version-command.module.ts index 42c23ba9a55..171663308b3 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-upgrade-version-command.module.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version-command/1-21/1-21-upgrade-version-command.module.ts @@ -6,6 +6,7 @@ import { AddGlobalKeyValuePairUniqueIndexCommand } from 'src/database/commands/u import { BackfillDatasourceToWorkspaceCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-backfill-datasource-to-workspace.command'; import { BackfillPageLayoutsAndFieldsWidgetViewFieldsCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-backfill-page-layouts-and-fields-widget-view-fields.command'; import { DeduplicateEngineCommandsCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-deduplicate-engine-commands.command'; +import { DropWorkspaceMessagingFksCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-drop-workspace-messaging-fks.command'; import { FixSelectAllCommandMenuItemsCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-fix-select-all-command-menu-items.command'; import { MigrateAiAgentTextToJsonResponseFormatCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-migrate-ai-agent-text-to-json-response-format.command'; import { UpdateEditLayoutCommandMenuItemLabelCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-update-edit-layout-command-menu-item-label.command'; @@ -14,6 +15,7 @@ import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature- import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module'; +import { WorkspaceSchemaManagerModule } from 'src/engine/twenty-orm/workspace-schema-manager/workspace-schema-manager.module'; import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module'; import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace-migration/workspace-migration.module'; @@ -26,6 +28,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace WorkspaceMigrationModule, FeatureFlagModule, WorkspaceIteratorModule, + WorkspaceSchemaManagerModule, ], providers: [ AddGlobalKeyValuePairUniqueIndexCommand, @@ -35,6 +38,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace FixSelectAllCommandMenuItemsCommand, MigrateAiAgentTextToJsonResponseFormatCommand, UpdateEditLayoutCommandMenuItemLabelCommand, + DropWorkspaceMessagingFksCommand, ], exports: [ AddGlobalKeyValuePairUniqueIndexCommand, @@ -44,6 +48,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace FixSelectAllCommandMenuItemsCommand, MigrateAiAgentTextToJsonResponseFormatCommand, UpdateEditLayoutCommandMenuItemLabelCommand, + DropWorkspaceMessagingFksCommand, ], }) export class V1_21_UpgradeVersionCommandModule {} diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts index 069120e91ef..24dc7582194 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version-command/upgrade.command.ts @@ -35,6 +35,7 @@ import { UpdateEditLayoutCommandMenuItemLabelCommand } from 'src/database/comman import { CoreEngineVersionService } from 'src/engine/core-engine-version/services/core-engine-version.service'; import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; import { WorkspaceVersionService } from 'src/engine/workspace-manager/workspace-version/services/workspace-version.service'; +import { DropWorkspaceMessagingFksCommand } from 'src/database/commands/upgrade-version-command/1-21/1-21-drop-workspace-messaging-fks.command'; @Command({ name: 'upgrade', @@ -76,6 +77,7 @@ export class UpgradeCommand extends UpgradeCommandRunner { private readonly fixSelectAllCommandMenuItemsCommand: FixSelectAllCommandMenuItemsCommand, private readonly migrateAiAgentTextToJsonResponseFormatCommand: MigrateAiAgentTextToJsonResponseFormatCommand, private readonly updateEditLayoutCommandMenuItemLabelCommand: UpdateEditLayoutCommandMenuItemLabelCommand, + private readonly dropWorkspaceMessagingFksCommand: DropWorkspaceMessagingFksCommand, ) { super( workspaceRepository, @@ -114,6 +116,7 @@ export class UpgradeCommand extends UpgradeCommandRunner { this.fixSelectAllCommandMenuItemsCommand, this.migrateAiAgentTextToJsonResponseFormatCommand, this.updateEditLayoutCommandMenuItemLabelCommand, + this.dropWorkspaceMessagingFksCommand, ]; this.allCommands = { diff --git a/packages/twenty-server/src/database/typeorm/core/migrations/common/1775165049548-migrate-messaging-calendar-to-core.ts b/packages/twenty-server/src/database/typeorm/core/migrations/common/1775165049548-migrate-messaging-calendar-to-core.ts new file mode 100644 index 00000000000..ccf0e4d2928 --- /dev/null +++ b/packages/twenty-server/src/database/typeorm/core/migrations/common/1775165049548-migrate-messaging-calendar-to-core.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class MigrateMessagingCalendarToCore1775165049548 + implements MigrationInterface +{ + name = 'MigrateMessagingCalendarToCore1775165049548'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "core"."messageFolder" ALTER COLUMN "parentFolderId" TYPE character varying`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "core"."messageFolder" ALTER COLUMN "parentFolderId" TYPE uuid USING "parentFolderId"::uuid`, + ); + } +} diff --git a/packages/twenty-server/src/engine/metadata-modules/message-folder/entities/message-folder.entity.ts b/packages/twenty-server/src/engine/metadata-modules/message-folder/entities/message-folder.entity.ts index 879790afa71..3588048ac81 100644 --- a/packages/twenty-server/src/engine/metadata-modules/message-folder/entities/message-folder.entity.ts +++ b/packages/twenty-server/src/engine/metadata-modules/message-folder/entities/message-folder.entity.ts @@ -31,7 +31,7 @@ export class MessageFolderEntity extends WorkspaceRelatedEntity { @Column({ type: 'boolean', nullable: false }) isSynced: boolean; - @Column({ type: 'uuid', nullable: true }) + @Column({ type: 'varchar', nullable: true }) parentFolderId: string | null; @Column({ type: 'varchar', nullable: true })