chore: import upstream snapshot with attribution
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { hybridAuthMockFns } from '@sim/testing'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { TableDefinition } from '@/lib/table'
|
||||
|
||||
const {
|
||||
mockCheckAccess,
|
||||
mockMarkTableJobRunning,
|
||||
mockReleaseJobClaim,
|
||||
mockRunTableDelete,
|
||||
mockTableFilterError,
|
||||
mockTasksTrigger,
|
||||
flags,
|
||||
} = vi.hoisted(() => ({
|
||||
mockCheckAccess: vi.fn(),
|
||||
mockMarkTableJobRunning: vi.fn(),
|
||||
mockReleaseJobClaim: vi.fn(),
|
||||
mockRunTableDelete: vi.fn(),
|
||||
mockTableFilterError: vi.fn(),
|
||||
mockTasksTrigger: vi.fn(),
|
||||
flags: { triggerDev: false },
|
||||
}))
|
||||
|
||||
vi.mock('@sim/utils/id', () => ({
|
||||
generateId: vi.fn().mockReturnValue('job-id-xyz'),
|
||||
generateShortId: vi.fn().mockReturnValue('short-id'),
|
||||
}))
|
||||
vi.mock('@/lib/table/jobs/service', () => ({
|
||||
markTableJobRunning: mockMarkTableJobRunning,
|
||||
releaseJobClaim: mockReleaseJobClaim,
|
||||
}))
|
||||
vi.mock('@/lib/table/delete-runner', () => ({ runTableDelete: mockRunTableDelete }))
|
||||
vi.mock('@/lib/core/config/env-flags', () => ({
|
||||
get isTriggerDevEnabled() {
|
||||
return flags.triggerDev
|
||||
},
|
||||
}))
|
||||
vi.mock('@/background/table-delete', () => ({ tableDeleteTask: { id: 'table-delete' } }))
|
||||
vi.mock('@/lib/core/async-jobs/region', () => ({
|
||||
resolveTriggerRegion: vi.fn().mockResolvedValue('us-east-1'),
|
||||
}))
|
||||
vi.mock('@trigger.dev/sdk', () => ({
|
||||
tasks: { trigger: mockTasksTrigger },
|
||||
task: (config: unknown) => config,
|
||||
}))
|
||||
vi.mock('@/lib/core/utils/background', () => ({
|
||||
runDetached: (_label: string, work: () => Promise<unknown>) => {
|
||||
void work()
|
||||
},
|
||||
}))
|
||||
vi.mock('@/app/api/table/utils', async () => {
|
||||
const { NextResponse } = await import('next/server')
|
||||
return {
|
||||
checkAccess: mockCheckAccess,
|
||||
accessError: (result: { status: number }) =>
|
||||
NextResponse.json({ error: 'denied' }, { status: result.status }),
|
||||
tableFilterError: mockTableFilterError,
|
||||
}
|
||||
})
|
||||
|
||||
import { POST } from '@/app/api/table/[tableId]/delete-async/route'
|
||||
|
||||
function buildTable(overrides: Partial<TableDefinition> = {}): TableDefinition {
|
||||
return {
|
||||
id: 'tbl_1',
|
||||
name: 'People',
|
||||
description: null,
|
||||
schema: { columns: [{ name: 'status', type: 'string' }] },
|
||||
metadata: null,
|
||||
rowCount: 1000,
|
||||
maxRows: 1_000_000,
|
||||
workspaceId: 'workspace-1',
|
||||
createdBy: 'user-1',
|
||||
archivedAt: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function makeRequest(body: unknown, tableId = 'tbl_1') {
|
||||
const req = new NextRequest(`http://localhost:3000/api/table/${tableId}/delete-async`, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
return POST(req, { params: Promise.resolve({ tableId }) })
|
||||
}
|
||||
|
||||
const validBody = {
|
||||
workspaceId: 'workspace-1',
|
||||
filter: { status: 'archived' },
|
||||
excludeRowIds: ['row_keep'],
|
||||
}
|
||||
|
||||
describe('POST /api/table/[tableId]/delete-async', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'user-1',
|
||||
authType: 'session',
|
||||
})
|
||||
mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() })
|
||||
mockMarkTableJobRunning.mockResolvedValue(true)
|
||||
mockRunTableDelete.mockResolvedValue(undefined)
|
||||
mockTableFilterError.mockReturnValue(null)
|
||||
mockTasksTrigger.mockResolvedValue({ id: 'run_1' })
|
||||
flags.triggerDev = false
|
||||
})
|
||||
|
||||
it('claims the job slot and kicks off the delete worker with filter + exclusions', async () => {
|
||||
const response = await makeRequest(validBody)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.data).toEqual({ tableId: 'tbl_1', jobId: 'job-id-xyz' })
|
||||
expect(mockMarkTableJobRunning).toHaveBeenCalledWith('tbl_1', 'job-id-xyz', 'delete', {
|
||||
filter: { status: 'archived' },
|
||||
excludeRowIds: ['row_keep'],
|
||||
cutoff: expect.any(String),
|
||||
})
|
||||
expect(mockRunTableDelete).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
jobId: 'job-id-xyz',
|
||||
tableId: 'tbl_1',
|
||||
workspaceId: 'workspace-1',
|
||||
filter: { status: 'archived' },
|
||||
excludeRowIds: ['row_keep'],
|
||||
cutoff: expect.any(Date),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('allows a whole-table delete with no filter', async () => {
|
||||
const response = await makeRequest({ workspaceId: 'workspace-1' })
|
||||
expect(response.status).toBe(200)
|
||||
expect(mockRunTableDelete).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ filter: undefined, cutoff: expect.any(Date) })
|
||||
)
|
||||
})
|
||||
|
||||
it('returns 409 when a job is already in progress (claim lost)', async () => {
|
||||
mockMarkTableJobRunning.mockResolvedValue(false)
|
||||
const response = await makeRequest(validBody)
|
||||
expect(response.status).toBe(409)
|
||||
expect(mockRunTableDelete).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns 400 on an invalid filter without claiming the slot', async () => {
|
||||
mockTableFilterError.mockReturnValue(NextResponse.json({ error: 'bad field' }, { status: 400 }))
|
||||
const response = await makeRequest(validBody)
|
||||
expect(response.status).toBe(400)
|
||||
expect(mockMarkTableJobRunning).not.toHaveBeenCalled()
|
||||
expect(mockRunTableDelete).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns 401 when unauthenticated', async () => {
|
||||
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ success: false })
|
||||
const response = await makeRequest(validBody)
|
||||
expect(response.status).toBe(401)
|
||||
expect(mockMarkTableJobRunning).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns the access error status when access is denied', async () => {
|
||||
mockCheckAccess.mockResolvedValue({ ok: false, status: 403 })
|
||||
const response = await makeRequest(validBody)
|
||||
expect(response.status).toBe(403)
|
||||
expect(mockRunTableDelete).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns 400 when the table is archived', async () => {
|
||||
mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable({ archivedAt: new Date() }) })
|
||||
const response = await makeRequest(validBody)
|
||||
expect(response.status).toBe(400)
|
||||
expect(mockRunTableDelete).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns 400 on workspace mismatch', async () => {
|
||||
const response = await makeRequest({ ...validBody, workspaceId: 'other-ws' })
|
||||
expect(response.status).toBe(400)
|
||||
})
|
||||
|
||||
it('routes through trigger.dev (ISO cutoff, tagged) when the flag is on', async () => {
|
||||
flags.triggerDev = true
|
||||
const response = await makeRequest(validBody)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(mockRunTableDelete).not.toHaveBeenCalled()
|
||||
expect(mockTasksTrigger).toHaveBeenCalledWith(
|
||||
'table-delete',
|
||||
expect.objectContaining({
|
||||
jobId: 'job-id-xyz',
|
||||
tableId: 'tbl_1',
|
||||
filter: { status: 'archived' },
|
||||
excludeRowIds: ['row_keep'],
|
||||
cutoff: expect.any(String),
|
||||
}),
|
||||
{ tags: ['tableId:tbl_1', 'jobId:job-id-xyz'], region: 'us-east-1' }
|
||||
)
|
||||
})
|
||||
|
||||
it('releases the job claim when the trigger.dev dispatch fails (no ghost running job)', async () => {
|
||||
flags.triggerDev = true
|
||||
mockTasksTrigger.mockRejectedValueOnce(new Error('trigger.dev unreachable'))
|
||||
|
||||
const response = await makeRequest(validBody)
|
||||
|
||||
expect(response.status).toBe(500)
|
||||
expect(mockReleaseJobClaim).toHaveBeenCalledWith('tbl_1', 'job-id-xyz')
|
||||
expect(mockRunTableDelete).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,129 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { generateId } from '@sim/utils/id'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { deleteTableRowsAsyncContract } from '@/lib/api/contracts/tables'
|
||||
import { parseRequest } from '@/lib/api/server'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { isTriggerDevEnabled } from '@/lib/core/config/env-flags'
|
||||
import { runDetached } from '@/lib/core/utils/background'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { markTableDeleteFailed, runTableDelete } from '@/lib/table/delete-runner'
|
||||
import { markTableJobRunning, releaseJobClaim } from '@/lib/table/jobs/service'
|
||||
import type { TableDeleteJobPayload } from '@/lib/table/types'
|
||||
import { accessError, checkAccess, tableFilterError } from '@/app/api/table/utils'
|
||||
|
||||
const logger = createLogger('TableDeleteAsync')
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ tableId: string }>
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/table/[tableId]/delete-async
|
||||
*
|
||||
* Kicks off a background "select all" delete: the client sends the active filter (and an optional
|
||||
* exclusion set) instead of every row id. Claims the table's single job slot (mutually exclusive
|
||||
* with imports), captures a `created_at` cutoff so rows inserted while the job runs survive, then
|
||||
* runs the paginated delete worker detached.
|
||||
*/
|
||||
export const POST = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
||||
}
|
||||
const userId = authResult.userId
|
||||
|
||||
const parsed = await parseRequest(deleteTableRowsAsyncContract, request, { params })
|
||||
if (!parsed.success) return parsed.response
|
||||
const { tableId } = parsed.data.params
|
||||
const { workspaceId, filter, excludeRowIds, estimatedCount } = parsed.data.body
|
||||
|
||||
const access = await checkAccess(tableId, userId, 'write')
|
||||
if (!access.ok) return accessError(access, requestId, tableId)
|
||||
const { table } = access
|
||||
|
||||
if (table.workspaceId !== workspaceId) {
|
||||
return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
|
||||
}
|
||||
if (table.archivedAt) {
|
||||
return NextResponse.json({ error: 'Cannot delete from an archived table' }, { status: 400 })
|
||||
}
|
||||
|
||||
// Validate the filter up front so the caller gets immediate feedback (the worker reuses it).
|
||||
const filterError = tableFilterError(filter, table.schema.columns)
|
||||
if (filterError) return filterError
|
||||
|
||||
// Rows inserted after this instant are spared (created_at <= cutoff in the worker).
|
||||
const cutoff = new Date()
|
||||
|
||||
// Atomically claim the job slot — one background job per table, so this also blocks while an
|
||||
// import is in flight (and vice versa). The scope is persisted to the job's payload so read
|
||||
// paths can mask the doomed rows while the job runs (see `pendingDeleteMask`).
|
||||
const jobId = generateId()
|
||||
const payload: TableDeleteJobPayload = {
|
||||
filter,
|
||||
excludeRowIds,
|
||||
cutoff: cutoff.toISOString(),
|
||||
// Clamp the client's display estimate to reality so a stale/bogus value
|
||||
// can't drive counts negative or hide more than the table holds.
|
||||
...(estimatedCount != null ? { doomedCount: Math.min(estimatedCount, table.rowCount) } : {}),
|
||||
}
|
||||
const claimed = await markTableJobRunning(tableId, jobId, 'delete', payload)
|
||||
if (!claimed) {
|
||||
return NextResponse.json(
|
||||
{ error: 'A job is already in progress for this table' },
|
||||
{ status: 409 }
|
||||
)
|
||||
}
|
||||
|
||||
if (isTriggerDevEnabled) {
|
||||
// Trigger.dev runs the delete outside the web container (survives deploys) and retries —
|
||||
// safe: the keyset + cutoff walk just deletes whatever remains.
|
||||
try {
|
||||
const [{ tableDeleteTask }, { tasks }, { resolveTriggerRegion }] = await Promise.all([
|
||||
import('@/background/table-delete'),
|
||||
import('@trigger.dev/sdk'),
|
||||
import('@/lib/core/async-jobs/region'),
|
||||
])
|
||||
await tasks.trigger<typeof tableDeleteTask>(
|
||||
'table-delete',
|
||||
{ jobId, tableId, workspaceId, filter, excludeRowIds, cutoff: cutoff.toISOString() },
|
||||
{ tags: [`tableId:${tableId}`, `jobId:${jobId}`], region: await resolveTriggerRegion() }
|
||||
)
|
||||
} catch (error) {
|
||||
// A failed dispatch must not leave a ghost `running` job holding the
|
||||
// table's one-write-job slot until the stale-job janitor fires.
|
||||
await releaseJobClaim(tableId, jobId).catch(() => {})
|
||||
throw error
|
||||
}
|
||||
} else {
|
||||
runDetached('table-delete', () =>
|
||||
runTableDelete({
|
||||
jobId,
|
||||
tableId,
|
||||
workspaceId,
|
||||
filter,
|
||||
excludeRowIds,
|
||||
cutoff,
|
||||
}).catch(async (error) => {
|
||||
// No retry machinery on the detached path — fail the job immediately.
|
||||
await markTableDeleteFailed(tableId, jobId, error)
|
||||
throw error
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
logger.info(`[${requestId}] Async row delete started`, {
|
||||
tableId,
|
||||
jobId,
|
||||
hasFilter: Boolean(filter),
|
||||
excluded: excludeRowIds?.length ?? 0,
|
||||
})
|
||||
return NextResponse.json({ success: true, data: { tableId, jobId } })
|
||||
})
|
||||
Reference in New Issue
Block a user