d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
194 lines
5.0 KiB
TypeScript
194 lines
5.0 KiB
TypeScript
import { db } from '@sim/db'
|
|
import {
|
|
apiKey,
|
|
document,
|
|
invitation,
|
|
invitationWorkspaceGrant,
|
|
knowledgeBase,
|
|
knowledgeConnector,
|
|
mcpServers,
|
|
userTableDefinitions,
|
|
workflowMcpServer,
|
|
workflowSchedule,
|
|
workspace,
|
|
workspaceFiles,
|
|
} from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { and, eq, inArray, isNull, sql } from 'drizzle-orm'
|
|
import { mcpPubSub } from '@/lib/mcp/pubsub'
|
|
import { mcpService } from '@/lib/mcp/service'
|
|
import { archiveWorkflowsForWorkspace } from '@/lib/workflows/lifecycle'
|
|
import { getWorkspaceWithOwner } from '@/lib/workspaces/permissions/utils'
|
|
|
|
const logger = createLogger('WorkspaceLifecycle')
|
|
|
|
interface ArchiveWorkspaceOptions {
|
|
requestId: string
|
|
}
|
|
|
|
export async function archiveWorkspace(
|
|
workspaceId: string,
|
|
options: ArchiveWorkspaceOptions
|
|
): Promise<{ archived: boolean; workspaceName?: string }> {
|
|
const workspaceRecord = await getWorkspaceWithOwner(workspaceId, { includeArchived: true })
|
|
|
|
if (!workspaceRecord) {
|
|
return { archived: false }
|
|
}
|
|
|
|
if (workspaceRecord.archivedAt) {
|
|
await archiveWorkflowsForWorkspace(workspaceId, options)
|
|
return { archived: false, workspaceName: workspaceRecord.name }
|
|
}
|
|
|
|
const now = new Date()
|
|
const workflowMcpServerIds = await db
|
|
.select({ id: workflowMcpServer.id })
|
|
.from(workflowMcpServer)
|
|
.where(eq(workflowMcpServer.workspaceId, workspaceId))
|
|
|
|
await db.transaction(async (tx) => {
|
|
await tx
|
|
.update(knowledgeBase)
|
|
.set({
|
|
deletedAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.where(and(eq(knowledgeBase.workspaceId, workspaceId), isNull(knowledgeBase.deletedAt)))
|
|
|
|
const workspaceKbIds = await tx
|
|
.select({ id: knowledgeBase.id })
|
|
.from(knowledgeBase)
|
|
.where(eq(knowledgeBase.workspaceId, workspaceId))
|
|
|
|
const knowledgeBaseIds = workspaceKbIds.map((entry) => entry.id)
|
|
if (knowledgeBaseIds.length > 0) {
|
|
await tx
|
|
.update(document)
|
|
.set({ archivedAt: now })
|
|
.where(
|
|
and(
|
|
inArray(document.knowledgeBaseId, knowledgeBaseIds),
|
|
isNull(document.archivedAt),
|
|
isNull(document.deletedAt)
|
|
)
|
|
)
|
|
|
|
await tx
|
|
.update(knowledgeConnector)
|
|
.set({ archivedAt: now, status: 'paused', updatedAt: now })
|
|
.where(
|
|
and(
|
|
inArray(knowledgeConnector.knowledgeBaseId, knowledgeBaseIds),
|
|
isNull(knowledgeConnector.archivedAt),
|
|
isNull(knowledgeConnector.deletedAt)
|
|
)
|
|
)
|
|
}
|
|
|
|
await tx
|
|
.update(userTableDefinitions)
|
|
.set({
|
|
archivedAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.where(
|
|
and(
|
|
eq(userTableDefinitions.workspaceId, workspaceId),
|
|
isNull(userTableDefinitions.archivedAt)
|
|
)
|
|
)
|
|
|
|
await tx
|
|
.update(workspaceFiles)
|
|
.set({
|
|
deletedAt: now,
|
|
})
|
|
.where(and(eq(workspaceFiles.workspaceId, workspaceId), isNull(workspaceFiles.deletedAt)))
|
|
|
|
await tx
|
|
.update(invitation)
|
|
.set({
|
|
status: 'cancelled',
|
|
updatedAt: now,
|
|
})
|
|
.where(
|
|
and(
|
|
eq(invitation.status, 'pending'),
|
|
sql`${invitation.id} IN (
|
|
SELECT ${invitationWorkspaceGrant.invitationId}
|
|
FROM ${invitationWorkspaceGrant}
|
|
WHERE ${invitationWorkspaceGrant.workspaceId} = ${workspaceId}
|
|
)`
|
|
)
|
|
)
|
|
|
|
await tx
|
|
.delete(apiKey)
|
|
.where(and(eq(apiKey.workspaceId, workspaceId), eq(apiKey.type, 'workspace')))
|
|
|
|
await tx
|
|
.update(workflowMcpServer)
|
|
.set({
|
|
deletedAt: now,
|
|
isPublic: false,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(workflowMcpServer.workspaceId, workspaceId))
|
|
|
|
await tx
|
|
.update(mcpServers)
|
|
.set({
|
|
deletedAt: now,
|
|
enabled: false,
|
|
updatedAt: now,
|
|
})
|
|
.where(and(eq(mcpServers.workspaceId, workspaceId), isNull(mcpServers.deletedAt)))
|
|
|
|
await tx
|
|
.update(workflowSchedule)
|
|
.set({
|
|
archivedAt: now,
|
|
updatedAt: now,
|
|
status: 'disabled',
|
|
nextRunAt: null,
|
|
lastQueuedAt: null,
|
|
})
|
|
.where(
|
|
and(
|
|
eq(workflowSchedule.sourceWorkspaceId, workspaceId),
|
|
eq(workflowSchedule.sourceType, 'job'),
|
|
isNull(workflowSchedule.archivedAt)
|
|
)
|
|
)
|
|
|
|
await tx
|
|
.update(workspace)
|
|
.set({
|
|
archivedAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.where(and(eq(workspace.id, workspaceId), isNull(workspace.archivedAt)))
|
|
})
|
|
|
|
await archiveWorkflowsForWorkspace(workspaceId, options)
|
|
|
|
logger.info(`[${options.requestId}] Archived workspace ${workspaceId}`)
|
|
|
|
await mcpService.clearCache(workspaceId).catch(() => undefined)
|
|
|
|
if (mcpPubSub && workflowMcpServerIds.length > 0) {
|
|
for (const server of workflowMcpServerIds) {
|
|
mcpPubSub.publishWorkflowToolsChanged({
|
|
serverId: server.id,
|
|
workspaceId,
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
archived: true,
|
|
workspaceName: workspaceRecord.name,
|
|
}
|
|
}
|