mirror of
https://github.com/supabase/supabase.git
synced 2026-09-07 02:20:52 +08:00
Stacked on #48305. ## What PR 3 of the stacked SQL-editor query-source series (Database vs Logs). Stacked on the PR 2 branch `charislam/log-sql-content-shape`. Turns `SnippetWithContent` into a discriminated union on `type` and makes all snippet writes source-aware: - `data/content/sql-folders-query.ts`: `SnippetWithContent` is now `{ type: 'sql'; content?: SqlSnippets.Content } | { type: 'log_sql'; content?: LogSqlSnippets.Content } | { type: 'report'; content?: never }`. `report` is kept (the content endpoints' wire type carries it) but has no SQL content — its body is `Dashboards.Content`, loaded through the separate `Content` union. - `setSql` brands per type (`untrustedLogSql` vs `untrustedSql`). - `buildUpsertPayload` persists `snippet.type` (no longer hardcoded `'sql'`). - `createSqlSnippetSkeletonV2({ source })` emits the matching type + content shape with the `as any` cast removed. - New `components/interfaces/SQLEditor/querySource.ts`: `SqlSnippetSource` + `getSnippetSource`. - `seedSnippet` test helper gains a `source` arg. - New `remapWireSnippet` boundary helper in `content-remap.ts` concentrates the single wire->domain assertion, so `content-id-query` / `content-upsert-mutation` call sites are cast-free (no `as unknown as`). - Collateral: query result types aligned to the union; `updateSnippet` no longer accepts `type` (source is immutable); db-only editor read paths narrow away `log_sql`. ## Why Impossible-states-impossible typing: a snippet's brand follows its content type, so logs SQL and database SQL can never cross execution paths. No behavior change for existing database snippets. ## Testing - \`pnpm typecheck\` — clean - \`pnpm --filter studio run lint:ratchet\` — no new warnings - \`pnpm test:studio\` (data/content, SQLEditor, state/sql-editor) — passing, including new tests for \`getSnippetSource\`, source-aware \`setSql\`, type-aware \`buildUpsertPayload\`, and both skeleton shapes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added source-aware creation for SQL editor snippets, including log-based SQL snippets. * Introduced backend source mapping so log snippets are treated as log_sql. * **Bug Fixes** * Improved SQL retrieval/prettification so log snippets no longer use the wrong fallback content. * Ensured log snippets are sanitized and preserve correct type, content, identifiers, and statuses during save/upsert flows. * **Tests** * Expanded unit and integration coverage for log snippet creation, source mapping, editing, prettification, and upsert payloads. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
219 lines
7.3 KiB
TypeScript
219 lines
7.3 KiB
TypeScript
import { untrustedSql } from '@supabase/pg-meta'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
buildUpsertPayload,
|
|
canEditSnippet,
|
|
isLoadedSnippet,
|
|
isSnippetOwner,
|
|
validateMoveToFolder,
|
|
type LoadedSnippet,
|
|
} from './sql-editor-rules'
|
|
import type { SnippetWithContent } from '@/data/content/sql-folders-query'
|
|
import { untrustedLogSql } from '@/data/logs/safe-analytics-sql'
|
|
|
|
function makeLogSnippet(): LoadedSnippet {
|
|
return {
|
|
id: 'log-snippet-1',
|
|
name: 'My Logs Query',
|
|
description: '',
|
|
visibility: 'user',
|
|
project_id: 42,
|
|
owner_id: 7,
|
|
folder_id: null,
|
|
favorite: false,
|
|
status: 'saved',
|
|
inserted_at: '2024-01-01T00:00:00.000Z',
|
|
updated_at: '2024-01-01T00:00:00.000Z',
|
|
type: 'log_sql',
|
|
content: {
|
|
content_id: 'log-snippet-1',
|
|
schema_version: '1',
|
|
unchecked_sql: untrustedLogSql('select * from logs'),
|
|
},
|
|
}
|
|
}
|
|
|
|
function makeSnippet(
|
|
overrides: Omit<Partial<SnippetWithContent>, 'content' | 'type'> = {}
|
|
): LoadedSnippet {
|
|
return {
|
|
id: 'snippet-1',
|
|
name: 'My Query',
|
|
description: 'A description',
|
|
visibility: 'user',
|
|
project_id: 42,
|
|
owner_id: 7,
|
|
folder_id: null,
|
|
favorite: false,
|
|
status: 'saved',
|
|
inserted_at: '2024-01-01T00:00:00.000Z',
|
|
updated_at: '2024-01-01T00:00:00.000Z',
|
|
...overrides,
|
|
type: 'sql',
|
|
content: {
|
|
content_id: 'snippet-1',
|
|
schema_version: '1',
|
|
unchecked_sql: untrustedSql('SELECT * FROM users;'),
|
|
},
|
|
}
|
|
}
|
|
|
|
describe('canEditSnippet', () => {
|
|
it('returns true for owner of a project-visibility snippet', () => {
|
|
const snippet = makeSnippet({ visibility: 'project', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, 7)).toBe(true)
|
|
})
|
|
|
|
it('returns false for non-owner of a project-visibility snippet', () => {
|
|
const snippet = makeSnippet({ visibility: 'project', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, 99)).toBe(false)
|
|
})
|
|
|
|
it('returns true for non-owner of a user-visibility snippet', () => {
|
|
const snippet = makeSnippet({ visibility: 'user', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, 99)).toBe(true)
|
|
})
|
|
|
|
it('returns false when profileId is undefined and snippet is project-visibility', () => {
|
|
const snippet = makeSnippet({ visibility: 'project', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, undefined)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('isSnippetOwner', () => {
|
|
it('returns true when profileId matches owner_id', () => {
|
|
const snippet = makeSnippet({ owner_id: 7 })
|
|
expect(isSnippetOwner(snippet, 7)).toBe(true)
|
|
})
|
|
|
|
it('returns false when profileId does not match owner_id', () => {
|
|
const snippet = makeSnippet({ owner_id: 7 })
|
|
expect(isSnippetOwner(snippet, 99)).toBe(false)
|
|
})
|
|
|
|
it('returns false when profileId is undefined', () => {
|
|
const snippet = makeSnippet({ owner_id: 7 })
|
|
expect(isSnippetOwner(snippet, undefined)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('validateMoveToFolder', () => {
|
|
it('returns { ok: false } when visibility is project and folderId is set', () => {
|
|
const result = validateMoveToFolder({ visibility: 'project', folderId: 'folder-abc' })
|
|
expect(result).toEqual({ ok: false, error: 'Shared snippet cannot be within a folder' })
|
|
})
|
|
|
|
it('returns { ok: true } when visibility is project and folderId is null', () => {
|
|
const result = validateMoveToFolder({ visibility: 'project', folderId: null })
|
|
expect(result).toEqual({ ok: true })
|
|
})
|
|
|
|
it('returns { ok: true } when visibility is project and folderId is undefined', () => {
|
|
const result = validateMoveToFolder({ visibility: 'project', folderId: undefined })
|
|
expect(result).toEqual({ ok: true })
|
|
})
|
|
|
|
it('returns { ok: true } when visibility is user and folderId is set', () => {
|
|
const result = validateMoveToFolder({ visibility: 'user', folderId: 'folder-abc' })
|
|
expect(result).toEqual({ ok: true })
|
|
})
|
|
})
|
|
|
|
describe('buildUpsertPayload', () => {
|
|
it('emits the snippet type — sql for a database snippet', () => {
|
|
const payload = buildUpsertPayload(makeSnippet(), 'snippet-1')
|
|
expect(payload.type).toBe('sql')
|
|
})
|
|
|
|
it('emits the snippet type — log_sql for a logs snippet (not silently rewritten to sql)', () => {
|
|
const payload = buildUpsertPayload(makeLogSnippet(), 'log-snippet-1')
|
|
expect(payload.type).toBe('log_sql')
|
|
expect((payload.content as { content_id: string }).content_id).toBe('log-snippet-1')
|
|
})
|
|
|
|
it('passes through provided values', () => {
|
|
const snippet = makeSnippet()
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
|
|
expect(payload.id).toBe('snippet-1')
|
|
expect(payload.type).toBe('sql')
|
|
expect(payload.name).toBe('My Query')
|
|
expect(payload.description).toBe('A description')
|
|
expect(payload.visibility).toBe('user')
|
|
expect(payload.project_id).toBe(42)
|
|
expect(payload.owner_id).toBe(7)
|
|
expect(payload.favorite).toBe(false)
|
|
})
|
|
|
|
it('applies Untitled default when name is undefined', () => {
|
|
const snippet = makeSnippet({ name: undefined })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.name).toBe('Untitled')
|
|
})
|
|
|
|
it('applies empty-string default when description is undefined', () => {
|
|
const snippet = makeSnippet({ description: undefined })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.description).toBe('')
|
|
})
|
|
|
|
it('applies user default when visibility is undefined', () => {
|
|
const snippet = makeSnippet({ visibility: undefined })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.visibility).toBe('user')
|
|
})
|
|
|
|
it('applies 0 default when project_id is undefined', () => {
|
|
const snippet = makeSnippet({ project_id: undefined })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.project_id).toBe(0)
|
|
})
|
|
|
|
it('applies false default when favorite is undefined', () => {
|
|
const snippet = makeSnippet({ favorite: undefined })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.favorite).toBe(false)
|
|
})
|
|
|
|
it('sets content.content_id to the supplied id', () => {
|
|
const snippet = makeSnippet()
|
|
const payload = buildUpsertPayload(snippet, 'my-id')
|
|
expect((payload.content as any).content_id).toBe('my-id')
|
|
})
|
|
|
|
it('converts folder_id null to undefined in the payload', () => {
|
|
const snippet = makeSnippet({ folder_id: null })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.folder_id).toBeUndefined()
|
|
})
|
|
|
|
it('passes through a non-null folder_id', () => {
|
|
const snippet = makeSnippet({ folder_id: 'folder-xyz' })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.folder_id).toBe('folder-xyz')
|
|
})
|
|
})
|
|
|
|
describe('isLoadedSnippet', () => {
|
|
it('returns true when content is present', () => {
|
|
const snippet = makeSnippet()
|
|
expect(isLoadedSnippet(snippet)).toBe(true)
|
|
})
|
|
|
|
it('returns false when content is undefined', () => {
|
|
const snippet = { ...makeSnippet(), content: undefined }
|
|
expect(isLoadedSnippet(snippet)).toBe(false)
|
|
})
|
|
|
|
it('narrows the snippet so buildUpsertPayload accepts it', () => {
|
|
const snippet: SnippetWithContent = makeSnippet()
|
|
if (isLoadedSnippet(snippet)) {
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.id).toBe('snippet-1')
|
|
} else {
|
|
throw new Error('expected snippet to be loaded')
|
|
}
|
|
})
|
|
})
|