@@ -11,7 +11,7 @@ import { explainCaptured, type PlanNode } from '@sim/db/testing/explain'
1111import { assertDisposableTestDatabaseUrl } from '@sim/db/testing/test-infrastructure'
1212import { deleteWorkspaceFixture , insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
1313import { generateId } from '@sim/utils/id'
14- import { eq , like , sql } from 'drizzle-orm'
14+ import { eq , inArray , like , sql } from 'drizzle-orm'
1515import { afterAll , beforeAll , describe , expect , it } from 'vitest'
1616import {
1717 type AuditLogFilterParams ,
@@ -21,6 +21,7 @@ import {
2121
2222const organizationId = generateId ( )
2323const memberId = generateId ( )
24+ const departedId = generateId ( )
2425const workspaceId = generateId ( )
2526const idPrefix = `audit_plan_${ generateId ( ) } _`
2627const workspaceIndex = 'audit_log_workspace_created_at_id_idx'
@@ -82,27 +83,31 @@ const NARROWED_CASES: [string, AuditLogFilterParams][] = [
8283describe ( 'organization audit-log listing plans in PostgreSQL' , ( ) => {
8384 beforeAll ( async ( ) => {
8485 assertDisposableTestDatabaseUrl ( process . env . DATABASE_URL ?? '' )
85- await db . insert ( user ) . values ( {
86- id : memberId ,
87- name : 'Audit plan fixture' ,
88- email : `${ memberId } @audit-plan.test` ,
89- emailVerified : true ,
90- createdAt : new Date ( ) ,
91- updatedAt : new Date ( ) ,
92- } )
86+ await db . insert ( user ) . values (
87+ [ memberId , departedId ] . map ( ( id ) => ( {
88+ id,
89+ name : 'Audit plan fixture' ,
90+ email : `${ id } @audit-plan.test` ,
91+ emailVerified : true ,
92+ createdAt : new Date ( ) ,
93+ updatedAt : new Date ( ) ,
94+ } ) )
95+ )
9396 await insertWorkspaceFixture ( db , {
9497 id : workspaceId ,
9598 name : 'Audit plan fixture' ,
9699 ownerId : memberId ,
97100 billedAccountUserId : memberId ,
98101 } )
99- // A workspace with a long history, and workspace-less rows of many other organizations, so
100- // collecting the scope and sorting it costs far more than a keyset scan that stops at a page.
102+ // A workspace with a long history written mostly by a departed member, and workspace-less
103+ // rows of many other organizations, so collecting the scope and sorting it costs far more than
104+ // a keyset scan that stops at a page, even one that passes over departed rows to fill it.
101105 await db . execute ( sql `
102106 INSERT INTO ${ auditLog } (id, workspace_id, actor_id, action, resource_type, resource_id,
103107 metadata, created_at)
104108 SELECT ${ idPrefix } || n, CASE WHEN n % 4 = 1 THEN ${ workspaceId } END,
105- CASE WHEN n % 2 = 0 THEN ${ memberId } END, 'organization.updated',
109+ CASE WHEN n % 20 = 1 OR n % 2 = 0 THEN ${ memberId } WHEN n % 4 = 1 THEN ${ departedId } END,
110+ 'organization.updated',
106111 CASE WHEN n % 997 = 0 THEN 'organization' ELSE 'workflow' END,
107112 CASE WHEN n % 997 = 0 THEN ${ organizationId } ELSE 'resource-' || n END,
108113 jsonb_build_object('organizationId',
@@ -115,7 +120,7 @@ describe('organization audit-log listing plans in PostgreSQL', () => {
115120 afterAll ( async ( ) => {
116121 await db . delete ( auditLog ) . where ( like ( auditLog . id , `${ idPrefix } %` ) )
117122 await deleteWorkspaceFixture ( db , eq ( workspace . id , workspaceId ) )
118- await db . delete ( user ) . where ( eq ( user . id , memberId ) )
123+ await db . delete ( user ) . where ( inArray ( user . id , [ memberId , departedId ] ) )
119124 } )
120125
121126 it . each ( KEYSET_CASES ) (
0 commit comments