Files
supabase/apps/studio/components/interfaces/App/CommandMenu/DatabaseEntitySearch.utils.test.ts
Joshen Lim 53ffdf75c9 Joshenlim/fe 4945 cmd k support searching for all table (#53262)
## Context

Probably the final piece for this series of changes towards resource
aware Cmd K. We currently only present results for tables when searching
in Cmd K, this PR adds in other entity types like views, materialized
views, foreign tables, and partitioned tables (only the parent)

Note that the results excludes entities created from database extensions
to reduce the chance of noise

<img width="500" alt="image"
src="https://github.com/user-attachments/assets/27bf5026-937a-4883-b365-75694d551a39"
/>

## Related changes
- Tweaked the entity icon for partitioned tables a little to be blue
(previously gray) and
- Added an entity icon for child partition tables in the table editor
that's slightly lighter than the partition table icon, currently child
partition tables just render with the same icon as tables which can be
confusing
<img width="286" height="128" alt="image"
src="https://github.com/user-attachments/assets/638af7c3-512d-4264-bfae-0a8827b3ef94"
/>
- Note that i'm planning to look into this separately RE how to better
render child partition tables so this might go away subsequently

## Performance

As always, ran a claude benchmarking session - this runs on debounced
keystrokes against a live catalog, so it's benchmarked at scale:
**45,653 `pg_class` rows**
- 12k tables
- 3,143 views
- 3,002 matviews
- 3,001 foreign tables)
Taking a median of 15 runs, all variants measured against the same
catalog in one process.

| scenario | query | master | this PR | worst |
|---|---|---|---|---|
| exact table name | `t_500` | 19.5ms | 24.3ms | 29.1ms |
| view name | `v_10` | 19.3ms | 23.7ms | 25.8ms |
| foreign table name | `ft_10` | 18.9ms | 24.1ms | 26.9ms |
| no match | `zzz_nothing` | 19.4ms | 24.0ms | 27.5ms |
| broad prefix (~3k matches) | `t_1` | 30.8ms | 38.7ms | 45.1ms |
| broad matview prefix | `mv_1` | 26.6ms | 37.5ms | 41.7ms |
2026-11-12 11:37:45 +08:00

125 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
formatFunctionSearchDetails,
formatTableSearchDetails,
formatTriggerSearchDetails,
matchesTableRelatedSearch,
matchesTriggerSearch,
} from './DatabaseEntitySearch.utils'
describe('matchesTableRelatedSearch', () => {
const policy = { name: 'Authors can edit', schema: 'public', table: 'blog_posts' }
it.each([
['the policy name', 'AUTHORS'],
['the table name', 'blog_p'],
['a schema-qualified table', 'pub.blog_posts'],
])('matches on %s', (_, query) => {
expect(matchesTableRelatedSearch(policy, query)).toBe(true)
})
it('does not match unrelated searches', () => {
expect(matchesTableRelatedSearch(policy, 'colors')).toBe(false)
})
})
describe('formatTableSearchDetails', () => {
const base = { id: 1, schema: 'public', name: 'blog_posts' }
it.each([
[{ ...base, type: 'r', rls_enabled: true, policy_count: 4 }, '4 policies • RLS enabled'],
[{ ...base, type: 'r', rls_enabled: false, policy_count: 1 }, '1 policy • RLS disabled'],
[{ ...base, type: 'p', rls_enabled: true, policy_count: 0 }, '0 policies • RLS enabled'],
[{ ...base, type: 'v', security_invoker: true }, 'Security invoker'],
[{ ...base, type: 'v', security_invoker: false }, 'Security definer'],
// Neither RLS nor security_invoker applies, and the icon already carries the kind
[{ ...base, type: 'm' }, ''],
[{ ...base, type: 'f' }, ''],
] as const)('formats %j', (table, expected) => {
expect(formatTableSearchDetails(table)).toBe(expected)
})
})
describe('formatFunctionSearchDetails', () => {
it.each([
[{ type: 'function', args: 'a integer, b text' }, '(a integer, b text)'],
[{ type: 'function', args: '' }, ''],
[{ type: 'procedure', args: 'id bigint' }, 'procedure (id bigint)'],
[{ type: 'procedure', args: '' }, 'procedure'],
] as const)('formats %j', (fn, expected) => {
expect(formatFunctionSearchDetails(fn)).toBe(expected)
})
})
describe('formatTriggerSearchDetails', () => {
const cases: Array<[Parameters<typeof formatTriggerSearchDetails>[0], string]> = [
[
{
activation: 'BEFORE',
events: ['INSERT'],
schema: 'public',
table: 'posts',
matched_function: null,
},
'BEFORE INSERT on public.posts',
],
[
{
activation: 'AFTER',
events: ['UPDATE', 'DELETE'],
schema: 'blog',
table: 'posts',
matched_function: null,
},
'AFTER UPDATE OR DELETE on blog.posts',
],
[
{
activation: 'INSTEAD OF',
events: ['INSERT'],
schema: 'public',
table: 'posts_view',
matched_function: null,
},
'INSTEAD OF INSERT on public.posts_view',
],
[
{
activation: 'AFTER',
events: ['INSERT'],
schema: 'public',
table: 'posts',
matched_function: 'handle_new_user',
},
'Runs handle_new_user',
],
]
it.each(cases)('formats %j', (trigger, expected) => {
expect(formatTriggerSearchDetails(trigger)).toBe(expected)
})
})
describe('matchesTriggerSearch', () => {
const trigger = {
name: 'on_author_created',
schema: 'public',
table: 'authors',
matched_function: 'handle_new_user',
}
it.each([
['its own name', 'author_created'],
['its table', 'authors'],
['the function it runs', 'HANDLE_NEW'],
])('matches on %s', (_, query) => {
expect(matchesTriggerSearch(trigger, query)).toBe(true)
})
it('does not match unrelated searches, or the function when it was not what matched', () => {
expect(matchesTriggerSearch(trigger, 'colors')).toBe(false)
expect(matchesTriggerSearch({ ...trigger, matched_function: null }, 'handle_new')).toBe(false)
})
})