Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(connections): replacing a connection keeps draft edits and leaves disabled flows disabled (#15812)

Bartosz Majewski1d1deaad773e
diff --git a/packages/server/api/src/app/app-connection/app-connection-service/app-connection.handler.ts b/packages/server/api/src/app/app-connection/app-connection-service/app-connection.handler.ts
index 43e89646ecb..89e840dfdc4 100644
--- a/packages/server/api/src/app/app-connection/app-connection-service/app-connection.handler.ts
+++ b/packages/server/api/src/app/app-connection/app-connection-service/app-connection.handler.ts
@@ -1,4 +1,4 @@
-import { ActivepiecesError, assertNotNullOrUndefined, ErrorCode, isNil, PlatformId, ProjectId, tryCatch, UserId } from '@activepieces/core-utils'
+import { ActivepiecesError, assertNotNullOrUndefined, ErrorCode, FlowId, isNil, PlatformId, ProjectId, tryCatch, unique, UserId } from '@activepieces/core-utils'
import { PropertyType } from '@activepieces/pieces-framework'
import { AppConnection, AppConnectionStatus, AppConnectionType, AppConnectionValue, AppConnectionWithoutSensitiveData, EngineResponse, EngineResponseStatus, ExecuteRefreshTokenAuthResponse, Flow, FlowOperationType, flowStructureUtil, FlowVersion, FlowVersionState, PopulatedFlow, WorkerJobType } from '@activepieces/shared'
import dayjs from 'dayjs'
@@ -21,15 +21,29 @@ import { oauth2Util } from './oauth2/oauth2-util'
export const appConnectionHandler = (log: FastifyBaseLogger) => ({
async updateFlowsWithAppConnection(flows: PopulatedFlow[], params: UpdateFlowsWithAppConnectionParams): Promise<void> {
const { appConnection, newAppConnection, userId, applyToPublishedVersions } = params
+ const flowIdsToRepublish = applyToPublishedVersions
+ ? await findFlowIdsToRepublish({ flows, externalId: appConnection.externalId })
+ : new Set<FlowId>()
await Promise.all(flows.map(async (flow) => {
const project = await projectService(log).getOneOrThrow(flow.projectId)
- // Don't change the order: republish first (when opted in), then make sure the
- // draft also points to the new connection.
- if (applyToPublishedVersions) {
- await handleLockedVersion(flow, userId, flow.projectId, project.platformId, appConnection, newAppConnection, log)
+ const latestVersion = await flowVersionService(log).getFlowVersionOrThrow({
+ flowId: flow.id,
+ versionId: undefined,
+ })
+ const draftParams = { flow, latestVersion, userId, platformId: project.platformId, appConnection, newAppConnection, log }
+ if (!flowIdsToRepublish.has(flow.id)) {
+ await handleDraftVersion(draftParams)
+ return
+ }
+ const { error } = await tryCatch(() => handleLockedVersion(flow, userId, flow.projectId, project.platformId, appConnection, newAppConnection, log))
+ const republishedLatestVersion = isNil(error) && !isNil(flow.publishedVersionId) && latestVersion.state === FlowVersionState.LOCKED
+ if (!republishedLatestVersion) {
+ await handleDraftVersion(draftParams)
+ }
+ if (!isNil(error)) {
+ throw error
}
- await handleDraftVersion(flow, userId, flow.projectId, project.platformId, appConnection, newAppConnection, log)
}))
},
@@ -342,6 +356,33 @@ class CustomAuthRefreshError extends Error {
}
}
+async function findFlowIdsToRepublish({ flows, externalId }: FindFlowIdsToRepublishParams): Promise<Set<FlowId>> {
+ const publishedFlows = flows.filter((flow) => !isNil(flow.publishedVersionId))
+ if (publishedFlows.length === 0) {
+ return new Set()
+ }
+
+ const newestLockedConnectionIds = flowVersionRepo()
+ .createQueryBuilder('fv_locked')
+ .select('fv_locked."connectionIds"')
+ .where('fv_locked."flowId" = flow.id')
+ .andWhere('fv_locked.state = :lockedState')
+ .orderBy('fv_locked.created', 'DESC')
+ .limit(1)
+ const rows = await flowVersionRepo()
+ .createQueryBuilder('flow_version')
+ .select('flow.id', 'flowId')
+ .innerJoin('flow', 'flow', 'flow."publishedVersionId" = flow_version.id')
+ .where('flow.id IN (:...flowIds)', { flowIds: publishedFlows.map((flow) => flow.id) })
+ .andWhere('flow."projectId" IN (:...projectIds)', { projectIds: unique(publishedFlows.map((flow) => flow.projectId)) })
+ .andWhere(`(flow_version."connectionIds" && :externalIds OR (${newestLockedConnectionIds.getQuery()}) && :externalIds)`, {
+ externalIds: [externalId],
+ lockedState: FlowVersionState.LOCKED,
+ })
+ .getRawMany<{ flowId: FlowId }>()
+ return new Set(rows.map((row) => row.flowId))
+}
+
async function handleLockedVersion(flow: PopulatedFlow, userId: UserId, projectId: ProjectId, platformId: PlatformId, appConnection: AppConnectionWithoutSensitiveData, newAppConnection: AppConnectionWithoutSensitiveData, log: FastifyBaseLogger) {
if (isNil(flow.publishedVersionId)) {
return
@@ -362,6 +403,7 @@ async function handleLockedVersion(flow: PopulatedFlow, userId: UserId, projectI
},
})
+ const { status } = await flowService(log).getOneOrThrow({ id: flow.id, projectId })
await flowService(log).update({
id: flow.id,
projectId,
@@ -369,28 +411,19 @@ async function handleLockedVersion(flow: PopulatedFlow, userId: UserId, projectI
userId,
operation: {
type: FlowOperationType.LOCK_AND_PUBLISH,
- request: {},
+ request: { status },
},
})
}
-async function handleDraftVersion(flow: Flow, userId: UserId, projectId: ProjectId, platformId: PlatformId, appConnection: AppConnectionWithoutSensitiveData, newAppConnection: AppConnectionWithoutSensitiveData, log: FastifyBaseLogger) {
- const latestVersion = await flowVersionService(log).getFlowVersionOrThrow({
- flowId: flow.id,
- versionId: undefined,
- })
-
- // Nothing to do if the latest version no longer references the old connection
- // (e.g. it was just republished onto the new one). Otherwise IMPORT_FLOW will
- // transparently create a draft from a published version and rewrite it, so the
- // draft always ends up on the new connection even for never-edited published flows.
+async function handleDraftVersion({ flow, latestVersion, userId, platformId, appConnection, newAppConnection, log }: HandleDraftVersionParams): Promise<void> {
if (!latestVersion.connectionIds.includes(appConnection.externalId)) {
return
}
await flowService(log).update({
id: flow.id,
- projectId,
+ projectId: flow.projectId,
platformId,
userId,
operation: {
@@ -431,6 +464,21 @@ type UpdateFlowsWithAppConnectionParams = {
applyToPublishedVersions: boolean
}
+type FindFlowIdsToRepublishParams = {
+ flows: PopulatedFlow[]
+ externalId: string
+}
+
+type HandleDraftVersionParams = {
+ flow: Flow
+ latestVersion: FlowVersion
+ userId: UserId
+ platformId: PlatformId
+ appConnection: AppConnectionWithoutSensitiveData
+ newAppConnection: AppConnectionWithoutSensitiveData
+ log: FastifyBaseLogger
+}
+
type CountPublishedFlowsParams = {
projectId: ProjectId
externalId: string
diff --git a/packages/server/api/test/integration/ce/app-connection/app-connection-replace.test.ts b/packages/server/api/test/integration/ce/app-connection/app-connection-replace.test.ts
index 6a27106fa23..0f832321f65 100644
--- a/packages/server/api/test/integration/ce/app-connection/app-connection-replace.test.ts
+++ b/packages/server/api/test/integration/ce/app-connection/app-connection-replace.test.ts
@@ -1,6 +1,9 @@
-import { AppConnection, AppConnectionScope, FlowStatus, FlowVersionState } from '@activepieces/shared'
+import { isNil } from '@activepieces/core-utils'
+import { AppConnection, AppConnectionScope, Flow, FlowStatus, FlowTrigger, FlowTriggerType, FlowVersion, FlowVersionState } from '@activepieces/shared'
+import dayjs from 'dayjs'
import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
+import { databaseConnection } from '../../../../src/app/database/database-connection'
import { db } from '../../../helpers/db'
import {
createMockConnection,
@@ -21,6 +24,7 @@ afterAll(async () => {
})
const PIECE_NAME = '@activepieces/piece-slack'
+const MISSING_PIECE_NAME = '@activepieces/piece-does-not-exist'
describe('POST /v1/app-connections/replace', () => {
it('keeps the source connection when deleteSourceConnection is not set', async () => {
@@ -402,4 +406,319 @@ describe('POST /v1/app-connections/replace', () => {
const stillThere = await db.findOneBy<AppConnection>('app_connection', { id: source.id })
expect(stillThere?.id).toBe(source.id)
})
+
+ it('keeps unpublished draft edits and a disabled status on a draft-and-published replace', async () => {
+ const ctx = await createTestContext(app!)
+ const { source, target } = await saveSourceAndTarget(ctx)
+
+ const flow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.DISABLED,
+ })
+ await db.save('flow', flow)
+ const publishedVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-01-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'published', sampleDataFileId: 'published-sample-file' }),
+ })
+ const editedDraftVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.DRAFT,
+ created: '2020-06-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'draft-edit', sampleDataFileId: 'draft-sample-file' }),
+ })
+ await db.save('flow_version', [publishedVersion, editedDraftVersion])
+ flow.publishedVersionId = publishedVersion.id
+ await db.save('flow', flow)
+
+ const response = await ctx.post('/v1/app-connections/replace', {
+ sourceAppConnectionId: source.id,
+ targetAppConnectionId: target.id,
+ projectId: ctx.project.id,
+ applyToPublishedVersions: true,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.NO_CONTENT)
+ const updatedFlow = await db.findOneByOrFail<Flow>('flow', { id: flow.id })
+ expect(updatedFlow.status).toBe(FlowStatus.DISABLED)
+ const versions = await versionsNewestFirst(flow.id)
+ const republished = versions.find((v) => v.id === updatedFlow.publishedVersionId)
+ expect(republished?.trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'published' })
+ expect(versions[0].state).toBe(FlowVersionState.DRAFT)
+ expect(versions[0].trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'draft-edit' })
+ expect(versions[0].trigger.settings.sampleData?.sampleDataFileId).toBe('draft-sample-file')
+ })
+
+ it('restores unpublished draft edits when the republish fails', async () => {
+ const ctx = await createTestContext(app!)
+ const { source, target } = await saveSourceAndTarget(ctx)
+
+ const flow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.ENABLED,
+ })
+ await db.save('flow', flow)
+ const publishedVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-01-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'published', pieceName: MISSING_PIECE_NAME }),
+ })
+ const editedDraftVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.DRAFT,
+ created: '2020-06-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'draft-edit' }),
+ })
+ await db.save('flow_version', [publishedVersion, editedDraftVersion])
+ flow.publishedVersionId = publishedVersion.id
+ await db.save('flow', flow)
+
+ const response = await ctx.post('/v1/app-connections/replace', {
+ sourceAppConnectionId: source.id,
+ targetAppConnectionId: target.id,
+ projectId: ctx.project.id,
+ applyToPublishedVersions: true,
+ })
+
+ expect(response?.statusCode).not.toBe(StatusCodes.NO_CONTENT)
+ const versions = await versionsNewestFirst(flow.id)
+ expect(versions[0].state).toBe(FlowVersionState.DRAFT)
+ expect(versions[0].trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'draft-edit' })
+ })
+
+ it('does not leave a new draft behind when a published flow had no draft edits', async () => {
+ const ctx = await createTestContext(app!)
+ const { source, target } = await saveSourceAndTarget(ctx)
+
+ const flow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.DISABLED,
+ })
+ await db.save('flow', flow)
+ const publishedVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.LOCKED,
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'published' }),
+ })
+ await db.save('flow_version', publishedVersion)
+ flow.publishedVersionId = publishedVersion.id
+ await db.save('flow', flow)
+
+ const response = await ctx.post('/v1/app-connections/replace', {
+ sourceAppConnectionId: source.id,
+ targetAppConnectionId: target.id,
+ projectId: ctx.project.id,
+ applyToPublishedVersions: true,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.NO_CONTENT)
+ const updatedFlow = await db.findOneByOrFail<Flow>('flow', { id: flow.id })
+ expect(updatedFlow.status).toBe(FlowStatus.DISABLED)
+ const versions = await versionsNewestFirst(flow.id)
+ expect(versions[0].id).toBe(updatedFlow.publishedVersionId)
+ expect(versions[0].state).toBe(FlowVersionState.LOCKED)
+ expect(versions[0].trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'published' })
+ })
+
+ it('does not republish a flow when only its draft uses the source connection', async () => {
+ const ctx = await createTestContext(app!)
+ const { source, target } = await saveSourceAndTarget(ctx)
+
+ const flow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.ENABLED,
+ })
+ await db.save('flow', flow)
+ const publishedVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-01-01T00:00:00.000Z',
+ connectionIds: [target.externalId],
+ trigger: pieceTrigger({ externalId: target.externalId, marker: 'published' }),
+ })
+ const editedDraftVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.DRAFT,
+ created: '2020-06-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'draft-edit' }),
+ })
+ await db.save('flow_version', [publishedVersion, editedDraftVersion])
+ flow.publishedVersionId = publishedVersion.id
+ await db.save('flow', flow)
+
+ const response = await ctx.post('/v1/app-connections/replace', {
+ sourceAppConnectionId: source.id,
+ targetAppConnectionId: target.id,
+ projectId: ctx.project.id,
+ applyToPublishedVersions: true,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.NO_CONTENT)
+ const updatedFlow = await db.findOneByOrFail<Flow>('flow', { id: flow.id })
+ expect(updatedFlow.status).toBe(FlowStatus.ENABLED)
+ expect(updatedFlow.publishedVersionId).toBe(publishedVersion.id)
+ const versions = await versionsNewestFirst(flow.id)
+ expect(versions).toHaveLength(2)
+ expect(versions[0].id).toBe(editedDraftVersion.id)
+ expect(versions[0].state).toBe(FlowVersionState.DRAFT)
+ expect(versions[0].trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'draft-edit' })
+ })
+
+ it('decides the republish per flow when one replace covers several flows', async () => {
+ const ctx = await createTestContext(app!)
+ const { source, target } = await saveSourceAndTarget(ctx)
+
+ const draftOnlyFlow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.ENABLED,
+ })
+ const publishedFlow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.DISABLED,
+ })
+ await db.save('flow', [draftOnlyFlow, publishedFlow])
+ const draftOnlyLiveVersion = createMockFlowVersion({
+ flowId: draftOnlyFlow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-01-01T00:00:00.000Z',
+ connectionIds: [target.externalId],
+ trigger: pieceTrigger({ externalId: target.externalId, marker: 'published' }),
+ })
+ const draftOnlyDraftVersion = createMockFlowVersion({
+ flowId: draftOnlyFlow.id,
+ state: FlowVersionState.DRAFT,
+ created: '2020-06-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'draft-edit' }),
+ })
+ const publishedLiveVersion = createMockFlowVersion({
+ flowId: publishedFlow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-01-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'published' }),
+ })
+ const publishedDraftVersion = createMockFlowVersion({
+ flowId: publishedFlow.id,
+ state: FlowVersionState.DRAFT,
+ created: '2020-06-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'draft-edit' }),
+ })
+ await db.save('flow_version', [draftOnlyLiveVersion, draftOnlyDraftVersion, publishedLiveVersion, publishedDraftVersion])
+ draftOnlyFlow.publishedVersionId = draftOnlyLiveVersion.id
+ publishedFlow.publishedVersionId = publishedLiveVersion.id
+ await db.save('flow', [draftOnlyFlow, publishedFlow])
+
+ const response = await ctx.post('/v1/app-connections/replace', {
+ sourceAppConnectionId: source.id,
+ targetAppConnectionId: target.id,
+ projectId: ctx.project.id,
+ applyToPublishedVersions: true,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.NO_CONTENT)
+ const updatedDraftOnlyFlow = await db.findOneByOrFail<Flow>('flow', { id: draftOnlyFlow.id })
+ expect(updatedDraftOnlyFlow.publishedVersionId).toBe(draftOnlyLiveVersion.id)
+ expect(updatedDraftOnlyFlow.status).toBe(FlowStatus.ENABLED)
+ const updatedPublishedFlow = await db.findOneByOrFail<Flow>('flow', { id: publishedFlow.id })
+ expect(updatedPublishedFlow.publishedVersionId).not.toBe(publishedLiveVersion.id)
+ const publishedFlowVersions = await versionsNewestFirst(publishedFlow.id)
+ const republished = publishedFlowVersions.find((v) => v.id === updatedPublishedFlow.publishedVersionId)
+ expect(republished?.trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'published' })
+ expect(publishedFlowVersions[0].state).toBe(FlowVersionState.DRAFT)
+ expect(publishedFlowVersions[0].trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'draft-edit' })
+ })
+
+ it('rewires a newer locked version that uses the source connection even when the live version does not', async () => {
+ const ctx = await createTestContext(app!)
+ const { source, target } = await saveSourceAndTarget(ctx)
+
+ const flow = createMockFlow({
+ projectId: ctx.project.id,
+ status: FlowStatus.DISABLED,
+ })
+ await db.save('flow', flow)
+ const liveVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-01-01T00:00:00.000Z',
+ connectionIds: [target.externalId],
+ trigger: pieceTrigger({ externalId: target.externalId, marker: 'published' }),
+ })
+ const submittedVersion = createMockFlowVersion({
+ flowId: flow.id,
+ state: FlowVersionState.LOCKED,
+ created: '2020-06-01T00:00:00.000Z',
+ connectionIds: [source.externalId],
+ trigger: pieceTrigger({ externalId: source.externalId, marker: 'submitted' }),
+ })
+ await db.save('flow_version', [liveVersion, submittedVersion])
+ flow.publishedVersionId = liveVersion.id
+ await db.save('flow', flow)
+
+ const response = await ctx.post('/v1/app-connections/replace', {
+ sourceAppConnectionId: source.id,
+ targetAppConnectionId: target.id,
+ projectId: ctx.project.id,
+ applyToPublishedVersions: true,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.NO_CONTENT)
+ const versions = await versionsNewestFirst(flow.id)
+ const newestLocked = versions.find((v) => v.state === FlowVersionState.LOCKED)
+ expect(newestLocked?.trigger.settings.input).toEqual({ auth: connectionRef(target.externalId), marker: 'submitted' })
+ })
})
+
+async function saveSourceAndTarget(ctx: Awaited<ReturnType<typeof createTestContext>>): Promise<{ source: AppConnection, target: AppConnection }> {
+ const source = createMockConnection({
+ platformId: ctx.platform.id,
+ projectIds: [ctx.project.id],
+ pieceName: PIECE_NAME,
+ }, ctx.user.id)
+ const target = createMockConnection({
+ platformId: ctx.platform.id,
+ projectIds: [ctx.project.id],
+ pieceName: PIECE_NAME,
+ }, ctx.user.id)
+ await db.save('app_connection', [source, target])
+ return { source, target }
+}
+
+function connectionRef(externalId: string): string {
+ return `{{connections['${externalId}']}}`
+}
+
+function pieceTrigger({ externalId, marker, pieceName = PIECE_NAME, sampleDataFileId }: { externalId: string, marker: string, pieceName?: string, sampleDataFileId?: string }): FlowTrigger {
+ return {
+ type: FlowTriggerType.PIECE,
+ name: 'trigger',
+ displayName: 'Trigger',
+ valid: true,
+ lastUpdatedDate: dayjs().toISOString(),
+ settings: {
+ pieceName,
+ pieceVersion: '0.1.0',
+ triggerName: 'new_message',
+ input: { auth: connectionRef(externalId), marker },
+ propertySettings: {},
+ sampleData: isNil(sampleDataFileId) ? undefined : { sampleDataFileId },
+ },
+ }
+}
+
+async function versionsNewestFirst(flowId: string): Promise<FlowVersion[]> {
+ return databaseConnection().getRepository<FlowVersion>('flow_version').find({
+ where: { flowId },
+ order: { created: 'DESC' },
+ })
+}