Files
supabase/apps/studio/lib/ai/tools/cap-read-query.test.ts
Anthony LioandJoshen Lim c3fd259904 fix(ai): assistant sql row cap (#51608)
## Problem

attempt to [fix this
ticket](https://supabase.frontapp.com/inboxes/teams/folders/27650590/unassigned/116845868830):
when the studio ai assistant runs a query with previously set database
opt-in, `execute_sql` returns every row while the query cell's footer
says "Limit 100 rows" causing in case of large amount of returned rows
browser to freeze

+ the chat fails with "Sorry, I'm having trouble responding right now"
until the user clears it

## Solution

- `execute_sql` now keeps the first 100 rows using the cell's existing
`DEFAULT_CELL_ROW_LIMIT`
- uses in history sanitizer the same `toModelRows` helper
- chats saved before this change have no `totalRows` so the helper falls
back to `rows.length` and cuts them to 100 too

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

1. In a test project, run this in the SQL editor:
   ```sql
create table "emailSubscriber" (id bigint generated always as identity
primary key, email text);
create table "stripeCustomer" (id bigint generated always as identity
primary key, email text);
insert into "emailSubscriber" (email) select md5(g::text) ||
'@example.com' from generate_series(1, 50000) g;
insert into "stripeCustomer" (email) select md5(g::text) ||
'@example.com' from generate_series(1, 1000) g;
2. on the live dashboard, open the assistant in a new chat and send "i
want all the emails in emailSubscriber where the email is not in the
stripeCustomer table"
3. in the opt in requested settings choose "Schema, Logs & Database
Data"
4. then click run query, the cell should show 100 rows (vs 50000 rows in
prod)

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-10-15 12:22:16 +03:00

74 lines
2.6 KiB
TypeScript

import { safeSql } from '@supabase/pg-meta'
import { describe, expect, it } from 'vitest'
import { capReadQuery } from './cap-read-query'
describe('capReadQuery', () => {
it('appends a limit when applyAutoLimit can', () => {
expect(capReadQuery(safeSql`select * from users`, 101)).toEqual({
sql: 'select * from users limit 101;',
isCappedInDatabase: true,
})
})
it('wraps queries with an explicit large LIMIT', () => {
const { sql, isCappedInDatabase } = capReadQuery(
safeSql`select * from users limit 1000000;`,
101
)
expect(isCappedInDatabase).toBe(true)
expect(sql).toBe(
'select * from (\nselect * from users limit 1000000\n) as capped_result limit 101;'
)
})
it('wraps queries with LIMIT ALL instead of appending a second limit', () => {
const { sql, isCappedInDatabase } = capReadQuery(safeSql`select * from users limit all`, 101)
expect(isCappedInDatabase).toBe(true)
expect(sql).toBe(
'select * from (\nselect * from users limit all\n) as capped_result limit 101;'
)
})
it('wraps FETCH FIRST and CTE queries', () => {
expect(
capReadQuery(safeSql`select * from users fetch first 5000 rows only`, 101).isCappedInDatabase
).toBe(true)
expect(
capReadQuery(safeSql`with t as (select 1) select * from t`, 101).isCappedInDatabase
).toBe(true)
})
it('keeps a trailing line comment from swallowing the closing paren', () => {
const { sql } = capReadQuery(safeSql`select * from users limit 5000 -- all`, 101)
expect(sql).toContain('-- all\n)')
})
it('ignores keywords and semicolons inside literals and comments', () => {
for (const q of [
safeSql`select * from t where note = 'please update; into' limit 5000`,
safeSql`select "update", $$delete;$$ from t limit 5000 -- insert`,
safeSql`select * from t /* for update */ limit 5000`,
]) {
expect(capReadQuery(q, 101).isCappedInDatabase).toBe(true)
}
})
it('still rejects writes that follow a literal', () => {
const q = safeSql`with d as (delete from t where a = 'x' returning *) select * from d`
expect(capReadQuery(q, 101)).toEqual({ sql: q, isCappedInDatabase: false })
})
it('leaves multiple statements and writes untouched', () => {
for (const q of [
safeSql`select 1; select 2`,
safeSql`with d as (delete from t returning *) select * from d`,
safeSql`with t as (select 1) select * into new_t from t`,
safeSql`with t as (select 1) select * from t for update`,
safeSql`insert into t values (1)`,
]) {
expect(capReadQuery(q, 101)).toEqual({ sql: q, isCappedInDatabase: false })
}
})
})