Files
supabase/apps/studio/components/interfaces/SQLEditor/querySource.ts
Saxon Fletcher cc6fe2100a refactor(studio): centralize query sources (#49027)
## Summary

- define application-owned database and logs source contracts, defaults,
validation, labels, and execution endpoints
- extract controlled database and logs parameter controls for reuse
outside SQL snippets
- adapt the SQL editor to the shared source model without changing
snippet behavior
- standardize source icons at 16px with a 2px stroke
- keep relative logs ranges aligned with the existing date picker units

## To test

1. Open an existing query in the SQL Editor and run it against the
database.
2. Switch the query source to Logs, change the time range, and confirm
the query still runs as expected.

## Why

Explorer queries and notebook query cells need to select an execution
source without coupling that source to SQL snippets. This provides the
shared registry and controlled UI foundation for those consumers.

## Impact

Existing SQL snippets retain their current database/logs routing and
session behavior. The registry documents the SQL editor legacy
database-selector adapter while new consumers own their identifier
inline. The shared Logs date picker remains unchanged; query ranges
support its existing minute, hour, and day units. This PR does not add
the Explorer query tab itself.

## Validation

- pnpm --filter studio typecheck
- focused Vitest coverage for the registry, canonical log-range
utilities, SQL execution adapters, source filtering, retention locking,
custom ranges, and preset selection
- pnpm --filter studio run lint:ratchet

Component and state tests cover this change per the Studio testing
guidance; no E2E test is added.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Added a unified query-source menu for database queries and logs.
* Added custom log time-range selection with calendar support and
retention-aware upgrade prompts.
* Added consistent source icons and improved database selection
handling.
  * Added support for relative and absolute log time ranges.

* **Bug Fixes**
  * Improved log-range validation, defaults, and current-time handling.
* Updated query execution to use the correct source-specific endpoints.

* **Tests**
* Expanded coverage for query sources, log ranges, menus, and retention
behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-13 16:51:06 +07:00

65 lines
2.7 KiB
TypeScript

import type { Snippet } from '@/data/content/sql-folders-query'
import { type LogTimeRange, type QuerySourceId } from '@/data/query-sources/query-source-registry'
/**
* Domain view of where a snippet's query runs. Derived from the content TYPE:
* a `log_sql` snippet always targets the logs backend and a `sql` (or `report`)
* snippet always targets the user's Postgres database. A snippet's source is
* immutable — switching backends means creating a new snippet, not toggling this
* value.
*/
export type SqlSnippetSource = QuerySourceId
/**
* The single reader every surface (AI, reports, tabs, nav, execution) uses to
* decide where a snippet runs. `'log_sql'` → `'logs'`; everything else (`'sql'`,
* `'report'`) → `'database'`. Accepts the raw `Snippet['type']` so a snippet of
* any content type maps to a source without narrowing first.
*/
export function getSnippetSource(snippet: Pick<Snippet, 'type'>): SqlSnippetSource {
return snippet.type === 'log_sql' ? 'logs' : 'database'
}
export function isLogsSource(source: SqlSnippetSource | undefined): boolean {
return source === 'logs'
}
/**
* The markdown fence language a source's SQL is written into a prompt with, so the model
* can tell a ClickHouse logs query from Postgres SQL. */
export function sqlSourceToFenceLanguage(
source: SqlSnippetSource | undefined
): 'sql' | 'clickhouse' {
return isLogsSource(source) ? 'clickhouse' : 'sql'
}
/**
* Parse a raw `source` value (e.g. the `?source=` query param a creation entry
* threads through `/sql/new`) into a `SqlSnippetSource`. Only the explicit
* `'logs'` opts a new snippet into the logs backend; anything else — including an
* absent param — is a database snippet, keeping database the safe default.
*/
export function parseSqlSnippetSource(raw: string | undefined): SqlSnippetSource {
return raw === 'logs' ? 'logs' : 'database'
}
/**
* Resolve where an open snippet's query runs, falling back to the `?source=` URL param
* when the snippet isn't in the store yet — a fresh `/sql/new` tab is materialized
* lazily on the first keystroke, and until then the param is the only signal.
*/
export function resolveSnippetSource(
snippet: Pick<Snippet, 'type'> | undefined,
sourceParam: string | undefined
): SqlSnippetSource {
return snippet !== undefined ? getSnippetSource(snippet) : parseSqlSnippetSource(sourceParam)
}
/**
* The runtime query source for a snippet, pairing the database/logs discriminant
* with the extra state each backend needs to run. A logs run carries the active
* time range (session state, re-resolved at every run); a database run needs
* nothing beyond the connection the execution pipeline already resolves.
*/
export type QuerySource = { type: 'database' } | { type: 'logs'; dateRange: LogTimeRange }