-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(tables): paginated background row-delete jobs via table_jobs #4915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheodoreSpeaks
wants to merge
11
commits into
staging
Choose a base branch
from
improvement/table-row-deletes
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f865ed5
feat(tables): paginated background row-delete jobs via table_jobs
TheodoreSpeaks 3866cad
fix(tables): address review on async row-delete (filtered count, scop…
TheodoreSpeaks b324ac0
Merge remote-tracking branch 'origin/staging' into improvement/table-…
TheodoreSpeaks 03c98b0
improvement(tables): filter-aware select-all runs, delete-job read ma…
TheodoreSpeaks 3f8a67a
feat(tables): run import/delete/export/backfill jobs on trigger.dev w…
TheodoreSpeaks e46c5b5
improvement(tables): raise delete page to 10k and export batch to 5k
TheodoreSpeaks e2dafbc
improvement(tables): raise CSV import batch to 5k rows (param-cap bou…
TheodoreSpeaks f8a2aee
feat(tables): surface export jobs in the header tray with progress, c…
TheodoreSpeaks 1ea5871
improvement(tables): surface exports as derived tables-scoped toasts …
TheodoreSpeaks cdbf43f
Revert "improvement(tables): surface exports as derived tables-scoped…
TheodoreSpeaks a1465d7
fix(tables): preserve export storage key (NoSuchKey) and unify jobs i…
TheodoreSpeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
apps/sim/app/api/table/[tableId]/delete-async/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /** | ||
| * @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, | ||
| mockRunTableDelete, | ||
| mockTableFilterError, | ||
| mockTasksTrigger, | ||
| flags, | ||
| } = vi.hoisted(() => ({ | ||
| mockCheckAccess: vi.fn(), | ||
| mockMarkTableJobRunning: 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/service', () => ({ markTableJobRunning: mockMarkTableJobRunning })) | ||
| vi.mock('@/lib/table/delete-runner', () => ({ runTableDelete: mockRunTableDelete })) | ||
| vi.mock('@/lib/core/config/feature-flags', () => ({ | ||
| get isTriggerDevEnabled() { | ||
| return flags.triggerDev | ||
| }, | ||
| })) | ||
| vi.mock('@/background/table-delete', () => ({ tableDeleteTask: { id: 'table-delete' } })) | ||
| 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'] } | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stale janitor fails export jobs
Medium Severity
The stale cleanup now marks every
table_jobsrow withstatus='running'as failed, including newexportjobs. Exports can run for a long time and only bumpupdated_atbetween paginated batches, so a slow or blocked batch can look “stalled” and be terminated even though the worker is still healthy.Reviewed by Cursor Bugbot for commit f8a2aee. Configure here.