mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 02:45:07 +03:00
Third of a stack. **Stacked on #49070** (which is stacked on #49069) — review those first. Base retargets automatically as each merges. Mechanical throughout; no behavior change. ## The problem Three types described where a query runs, and no two agreed: | | shape | |---|---| | `CellSource` (registry) | `{ id, type, parameters: { … } }` — `id` and `type` always held the same literal | | `QuerySource` (SQL editor) | `{ type: 'database' } \| { type: 'logs', dateRange }` | | notebook cells | flat per-backend fields, neither of the above | Anything crossing between them needed a translation that dropped fields on the way — which is how a notebook cell's replica selection had nowhere to go. ## What changed One `QuerySourceBinding`: a backend `_tag` with that backend's parameters spread flat beside it, borrowed from the wire schema (#49069) so the binding and the persisted cell agree by construction. - **`QuerySource` is deleted.** `useRunSource` returns the shared binding, so `runSource.type`/`dateRange` become `_tag`/`time_range` across the SQL editor — that is most of the file count here. - **`getQuerySourceBinding`** projects a notebook cell onto a binding; **`toQuerySourceBinding`** does the same for any backend-tagged carrier. Both overloaded so an already-narrowed caller gets the matching binding back rather than the union, which keeps the result spreadable without re-narrowing. - **`ExplorerQuerySourceMenu`** drops its inline copy of the custom-range and upgrade-prompt logic in favor of `useLogsCustomRange`, which the SQL editor menu already used. The registry keeps only what is genuinely runtime: endpoints, labels, icons, availability, defaults. What a query *is* stays in the wire schema. ## Verification Typecheck, Prettier, and the lint ratchet clean. 405 tests pass across the notebook schema, query sources, the logs components, the SQL editor, and the Explorer surfaces. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Updated query source handling across Explorer and SQL Editor for a more consistent selection experience. * Database and log sources now preserve identifiers and time ranges more reliably when switching or editing queries. * Source menus, labels, icons, validation, and query execution now reflect the selected source more accurately. * **Bug Fixes** * Invalid or outdated saved source settings now safely fall back to a database source. * Improved log-source detection and time-range handling throughout query editing and execution. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
116 lines
3.7 KiB
TypeScript
116 lines
3.7 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { FeatureFlagContext } from 'common'
|
|
import { mockAnimationsApi } from 'jsdom-testing-mocks'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { ExplorerQuerySourceMenu } from './ExplorerQuerySourceMenu'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
mockAnimationsApi()
|
|
|
|
beforeEach(() => {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
response: {
|
|
id: 1,
|
|
ref: 'default',
|
|
organization_id: 1,
|
|
name: 'Test Project',
|
|
status: 'ACTIVE_HEALTHY',
|
|
cloud_provider: 'AWS',
|
|
region: 'us-east-1',
|
|
db_host: 'db.default.supabase.co',
|
|
restUrl: 'https://default.supabase.co/rest/v1/',
|
|
inserted_at: '2024-01-01T00:00:00Z',
|
|
updated_at: '2024-01-01T00:00:00Z',
|
|
subscription_id: 'sub_123',
|
|
is_branch_enabled: false,
|
|
is_physical_backups_enabled: false,
|
|
high_availability: false,
|
|
integration_source: null,
|
|
connectionString: 'postgresql://postgres@localhost:5432/postgres',
|
|
is_hibernating: false,
|
|
},
|
|
})
|
|
})
|
|
|
|
describe('ExplorerQuerySourceMenu', () => {
|
|
const renderWithFlags = (
|
|
source: Parameters<typeof ExplorerQuerySourceMenu>[0]['source'],
|
|
flags: Record<string, boolean>
|
|
) =>
|
|
customRender(
|
|
<FeatureFlagContext.Provider value={{ configcat: flags, posthog: {}, hasLoaded: true }}>
|
|
<ExplorerQuerySourceMenu source={source} onSourceChange={vi.fn()} />
|
|
</FeatureFlagContext.Provider>
|
|
)
|
|
|
|
it('emits a complete default binding when the query changes source', async () => {
|
|
const onSourceChange = vi.fn()
|
|
|
|
customRender(
|
|
<ExplorerQuerySourceMenu
|
|
source={{
|
|
_tag: 'logs',
|
|
time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' },
|
|
}}
|
|
onSourceChange={onSourceChange}
|
|
/>
|
|
)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Query source: Logs' }))
|
|
await userEvent.click(screen.getByText('Database'))
|
|
|
|
expect(onSourceChange).toHaveBeenCalledWith({ _tag: 'database' })
|
|
})
|
|
|
|
it('emits the selected log time range as source parameters', async () => {
|
|
const onSourceChange = vi.fn()
|
|
|
|
customRender(
|
|
<ExplorerQuerySourceMenu
|
|
source={{
|
|
_tag: 'logs',
|
|
time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' },
|
|
}}
|
|
onSourceChange={onSourceChange}
|
|
/>
|
|
)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Query source: Logs' }))
|
|
await userEvent.hover(screen.getByText('Time range'))
|
|
await userEvent.click(await screen.findByText('Last 3 hours'))
|
|
|
|
expect(onSourceChange).toHaveBeenCalledWith({
|
|
_tag: 'logs',
|
|
time_range: { _tag: 'relative_time_range', amount: 3, unit: 'hour' },
|
|
})
|
|
})
|
|
|
|
it('does not offer logs when source flags are disabled for a database query', async () => {
|
|
renderWithFlags({ _tag: 'database' }, { sqlEditorLogsSource: false, otelLegacyLogs: false })
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Query source: Database' }))
|
|
|
|
expect(screen.queryByText('Logs')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('keeps logs available when an existing query already uses it', async () => {
|
|
renderWithFlags(
|
|
{
|
|
_tag: 'logs',
|
|
time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' },
|
|
},
|
|
{ sqlEditorLogsSource: false, otelLegacyLogs: false }
|
|
)
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Query source: Logs' }))
|
|
|
|
expect(screen.getAllByText('Logs')).toHaveLength(2)
|
|
expect(screen.getByText('Database')).toBeInTheDocument()
|
|
})
|
|
})
|