@@ -16,7 +16,7 @@ import {
1616 workspaceFiles ,
1717} from '@sim/db/schema'
1818import { generateId } from '@sim/utils/id'
19- import { and , eq , inArray , isNull , sql } from 'drizzle-orm'
19+ import { and , eq , inArray , isNull , or , sql } from 'drizzle-orm'
2020import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest'
2121
2222const fixtureStorage = vi . hoisted ( ( ) => ( { root : '' } ) )
@@ -239,9 +239,9 @@ describe('workspace file names in PostgreSQL', () => {
239239 } ) )
240240 for ( const file of files ) {
241241 await db . execute ( sql `
242- INSERT INTO ${ workspaceFiles } (id, key, user_id, workspace_id, folder_id, context, original_name, content_type)
242+ INSERT INTO ${ workspaceFiles } (id, key, user_id, workspace_id, folder_id, context, original_name, content_type, size_bytes )
243243 VALUES (${ file . id } , ${ `legacy/${ file . id } ` } , ${ fixture . aliceId } , ${ fixture . workspaceId } ,
244- ${ file . folderId } , 'workspace', ${ file . name } , 'text/plain')` )
244+ ${ file . folderId } , 'workspace', ${ file . name } , 'text/plain', 1 )` )
245245 }
246246 await db . execute ( sql `
247247 INSERT INTO ${ workspaceFiles } (id, key, user_id, workspace_id, context, original_name, content_type)
@@ -264,17 +264,22 @@ describe('workspace file names in PostgreSQL', () => {
264264 }
265265 expect ( await resolveWorkspaceFileReference ( fixture . workspaceId , 'brand-new.txt' ) ) . toBeNull ( )
266266
267+ // The resolver's candidate query for a miss: the id arm, the display-name arm, its order.
267268 const plan = await db . execute (
268- sql `EXPLAIN (FORMAT JSON) SELECT id FROM ${ workspaceFiles } WHERE ${ and (
269- eq ( workspaceFiles . workspaceId , fixture . workspaceId ) ,
270- eq ( workspaceFiles . context , 'workspace' ) ,
271- isNull ( workspaceFiles . deletedAt ) ,
272- eq ( displaySegmentKey ( workspaceFiles . originalName ) , 'brand-new.txt' )
273- ) } `
274- )
275- expect ( JSON . stringify ( plan [ 0 ] [ 'QUERY PLAN' ] ) ) . toContain (
276- 'workspace_files_workspace_display_name_idx'
269+ sql `EXPLAIN (FORMAT JSON) SELECT id, original_name, folder_id, uploaded_at
270+ FROM ${ workspaceFiles } WHERE ${ and (
271+ eq ( workspaceFiles . workspaceId , fixture . workspaceId ) ,
272+ eq ( workspaceFiles . context , 'workspace' ) ,
273+ isNull ( workspaceFiles . deletedAt ) ,
274+ or (
275+ inArray ( workspaceFiles . id , [ 'brand-new.txt' ] ) ,
276+ eq ( displaySegmentKey ( workspaceFiles . originalName ) , 'brand-new.txt' )
277+ )
278+ ) } ORDER BY ${ workspaceFiles . uploadedAt } `
277279 )
280+ const scan = JSON . stringify ( plan [ 0 ] [ 'QUERY PLAN' ] )
281+ expect ( scan ) . toContain ( 'workspace_files_workspace_display_name_idx' )
282+ expect ( scan ) . not . toContain ( 'workspace_files_workspace_active_keyset_idx' )
278283 } )
279284
280285 it ( 'falls back to a short-id suffix after 20 numbered copies, including under concurrency' , async ( ) => {
0 commit comments