mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Context Building off from [here](https://github.com/supabase/supabase/pull/52751) to make Cmd+K resource-aware so finding and inspecting anything in a project doesn't depend on knowing where it lives in the dashboard. Specifically looking into database entities for now - end state would be that you could search for a table name for example, and Cmd K will pull up everything related to that table for you to skim (e.g Policies, Functions, Triggers, etc). Changes in this PR sets up searching for **database triggers**, and will probably be the last database entity that we'll add into context aware cmd K for now given that we'll have tables, policies, functions and triggers which are the primary building blocks for a project. <img width="500" alt="image" src="https://github.com/user-attachments/assets/242ec517-88cd-4f3d-96ce-741edaa8560d" /> Triggers will also match if it's function matches the search input, and its description will tweak slightly as well <img width="500" alt="image" src="https://github.com/user-attachments/assets/65f515df-b875-4b92-95b7-00389d8a070b" /> ## Performance Had Claude do some benchmarking against `getSearchEntitiesSql` (`EXPLAIN ANALYZE`, median of 5 runs, local Postgres, warm cache) on one synthetic catalog: - 12,000 tables (36,651 `pg_class`), 6,001 policies, 27,261 `pg_proc` (12,000 user + 12,000 extension-owned functions), 56,304 `pg_trigger` - 3,500 user triggers, 500 of which run one shared function; the rest are internal FK triggers - 297,921 `pg_depend`. Compared against master (tables + policies) and the functions PR (before triggers). | Search | What it exercises | master | functions PR | this PR | Δ this PR | |---|---|---|---|---|---| | `t_1` | broad table match | 12ms | 26ms | 30ms | +4ms | | `p_1` | broad policy-name match | 15ms | 27ms | 35ms | +8ms | | `fn_1` | broad function match | 10ms | 26ms | 33ms | +7ms | | `trg_1` | broad trigger-name match | 9ms | 23ms | 32ms | +9ms | | `trg_` | every user trigger matches by name | 10ms | 25ms | 40ms | +15ms | | `audit_row_fn` | function match; its 500 triggers match only through the function | 9ms | 24ms | 31ms | +7ms | | `ri_constraint` | only internal FK triggers match | 9ms | 24ms | 30ms | +6ms | | `stress.fn_1` | schema-qualified | 12ms | 33ms | 40ms | +7ms | | `zzz` | no match | 9ms | 22ms | 27ms | +5ms | - Triggers add +4 to +15ms over the functions PR. The worst case is a search matching every user trigger, at 40ms in total. - Matching triggers through their function shares the single `pg_trigger` pass the name search already does, with no extra scan. A separate pass measured about 30ms more on every search. ## To test - [ ] Search a table name: Tables first, then policies, and functions, and triggers - [ ] Search a trigger name: it appears under triggers and opens the editor panel in database/triggers - [ ] Search a function name: trigger results should appear if the trigger uses that function - [ ] Trigger outside public: schema filter switches and the trigger opens
140 lines
4.0 KiB
TypeScript
140 lines
4.0 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
formatFunctionSearchDetails,
|
|
formatTriggerSearchDetails,
|
|
getFunctionEditorRoute,
|
|
getPolicyEditorRoute,
|
|
getTriggerEditorRoute,
|
|
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('getPolicyEditorRoute', () => {
|
|
it('sets the schema so the policy is listed on the policies page', () => {
|
|
expect(getPolicyEditorRoute('abc', { id: 7, schema: 'public' })).toBe(
|
|
'/project/abc/database/policies?schema=public&edit=7'
|
|
)
|
|
})
|
|
|
|
it('encodes schema names', () => {
|
|
expect(getPolicyEditorRoute('abc', { id: 7, schema: 'my schema&x' })).toBe(
|
|
'/project/abc/database/policies?schema=my%20schema%26x&edit=7'
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('getFunctionEditorRoute', () => {
|
|
it('sets the schema so the function is listed on the functions page', () => {
|
|
expect(getFunctionEditorRoute('abc', { id: 9, schema: 'my schema' })).toBe(
|
|
'/project/abc/database/functions?schema=my%20schema&edit=9'
|
|
)
|
|
})
|
|
})
|
|
|
|
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('getTriggerEditorRoute', () => {
|
|
it('links to the list page directly, since the index route drops the query string', () => {
|
|
expect(getTriggerEditorRoute('abc', { id: 5, schema: 'my schema' })).toBe(
|
|
'/project/abc/database/triggers/data?schema=my%20schema&edit=5'
|
|
)
|
|
})
|
|
})
|
|
|
|
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)
|
|
})
|
|
})
|