:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(sql-editor): open save view dialog before incremental checks (#125459)
Jovan Sakovicee7b28cb93d9
diff --git a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsxindex bb481101bd0a..8f028de82584 100644--- a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx+++ b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.test.tsx@@ -2,6 +2,7 @@ import '@testing-library/jest-dom'import { act, cleanup, render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'import userEvent from '@testing-library/user-event'+import { useValues } from 'kea'import posthog from 'posthog-js'import { ApiError } from 'lib/api-error'@@ -11,6 +12,12 @@ import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'import { initKeaTests } from '~/test/init'import { LemonDialog, LemonFormDialog } from './LemonDialog'+import { lemonDialogLogic } from './lemonDialogLogic'++function DialogName(): JSX.Element {+ const { form } = useValues(lemonDialogLogic)+ return <span>{form.name}</span>+}describe('LemonFormDialog', () => {let captureException: jest.SpyInstance@@ -101,12 +108,14 @@ describe('LemonFormDialog', () => {act(() => {LemonDialog.openForm({title: 'Test dialog',- initialValues: {},+ initialValues: { name: 'a name' },+ content: <DialogName />,onSubmit: jest.fn(),onAfterClose,})})+ expect(await screen.findByText('a name')).toBeInTheDocument()await userEvent.click(screen.getByText(button))await waitFor(() => expect(onAfterClose).toHaveBeenCalledTimes(1))diff --git a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsxindex 93926c04b317..edc88a09fe41 100644--- a/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx+++ b/frontend/src/lib/lemon-ui/LemonDialog/LemonDialog.tsx@@ -1,4 +1,4 @@-import { useActions, useValues } from 'kea'+import { BindLogic, useActions, useValues } from 'kea'import { Form } from 'kea-forms'import { router } from 'kea-router'import posthog from 'posthog-js'@@ -253,14 +253,16 @@ export const LemonFormDialog = ({}}>- <LemonDialog- ref={ref}- {...props}- content={resolvedContent}- primaryButton={primaryButton}- secondaryButton={secondaryButton}- isLoadingCallback={setIsLoading}- />+ <BindLogic logic={lemonDialogLogic} props={logicProps}>+ <LemonDialog+ ref={ref}+ {...props}+ content={resolvedContent}+ primaryButton={primaryButton}+ secondaryButton={secondaryButton}+ isLoadingCallback={setIsLoading}+ />+ </BindLogic></Form>)}diff --git a/frontend/src/scenes/data-warehouse/editor/IncrementalConfigFields.tsx b/frontend/src/scenes/data-warehouse/editor/IncrementalConfigFields.tsxindex ee8e8c6ed73a..270d86f1fca9 100644--- a/frontend/src/scenes/data-warehouse/editor/IncrementalConfigFields.tsx+++ b/frontend/src/scenes/data-warehouse/editor/IncrementalConfigFields.tsx@@ -1,5 +1,8 @@+import { useMountedLogic, useValues } from 'kea'+import { LemonBanner } from '@posthog/lemon-ui'+import { lemonDialogLogic } from 'lib/lemon-ui/LemonDialog/lemonDialogLogic'import { LemonField } from 'lib/lemon-ui/LemonField'import { LemonInputSelect } from 'lib/lemon-ui/LemonInputSelect'import { LemonRadio, LemonRadioOption } from 'lib/lemon-ui/LemonRadio'@@ -9,6 +12,8 @@ import { LemonTag } from 'lib/lemon-ui/LemonTag'import { DataWarehouseSavedQueryIncrementalCheck } from '~/types'+import { saveViewIncrementalLogic } from 'products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic'+export const LOOKBACK_OPTIONS = [{ value: 0, label: 'No lookback' },{ value: 60 * 60, label: '1 hour' },@@ -40,8 +45,7 @@ const UNIQUE_KEY_HELP =const LOOKBACK_HELP = 'Include this much previously processed data in each run to catch late arrivals and updates.'interface IncrementalConfigFieldsProps {- /** Result of the backend eligibility check for the query being saved. */- check: DataWarehouseSavedQueryIncrementalCheck | null+ queries: string[]}/** What the user has picked so far, before it becomes a saved incremental config. */@@ -285,11 +289,37 @@ export function IncrementalConfigOptions({* Rendered only when the view is being materialized, since incremental describes how a* materialized table is refreshed and means nothing without one.*/-export function IncrementalConfigFields({ check }: IncrementalConfigFieldsProps): JSX.Element | null {- if (!check) {+export function IncrementalConfigFields({ queries }: IncrementalConfigFieldsProps): JSX.Element | null {+ const formLogic = useMountedLogic(lemonDialogLogic)+ const { form } = useValues(formLogic)+ const { check, checkLoading } = useValues(+ saveViewIncrementalLogic({+ query: queries[Number(form.saveTarget)] ?? '',+ dialogKey: String(formLogic.key),+ enabled: Boolean(form.materializeAfterSave),+ })+ )++ if (!form.materializeAfterSave) {return null}+ if (checkLoading) {+ return (+ <div className="mt-4 deprecated-space-y-2" data-attr="sql-editor-save-view-incremental-loading">+ <LemonSkeleton className="h-8 w-full" repeat={2} />+ </div>+ )+ }++ if (!check) {+ return (+ <LemonBanner type="info" className="mt-2">+ Incremental options are unavailable. You can still save this view with a full refresh.+ </LemonBanner>+ )+ }+if (!check.eligible || check.key_candidates.length === 0) {return <IneligibleBanner check={check} />}diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.tsindex dcd3b71d1f81..51eaef4ea955 100644--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts@@ -2080,6 +2080,29 @@ describe('sqlEditorLogic', () => {})describe('save dialog selection guard', () => {+ it('opens the view dialog without checking incremental materialization', async () => {+ const openForm = jest.spyOn(LemonDialog, 'openForm').mockImplementation(() => {})+ const checkIncremental = jest.fn(() => [500, { detail: 'Check unavailable' }])+ useMocks({+ post: {+ '/api/environments/:team_id/warehouse_saved_queries/check_incremental/': checkIncremental,+ },+ })+ logic = sqlEditorLogic({+ tabId: TAB_ID,+ monaco: createMockMonaco(),+ editor: createMockEditor(),+ })+ logic.mount()+ logic.actions.setQueryInput('SELECT 1 AS value')++ await logic.asyncActions.saveAsView()++ expect(openForm).toHaveBeenCalledWith(expect.objectContaining({ title: 'Save as view' }))+ expect(checkIncremental).not.toHaveBeenCalled()+ openForm.mockRestore()+ })+// Monaco reports a non-empty selection after a double-click, which is the editor state// that let a single identifier reach the API as the query to save.function createEditorWithSelection(selected: string): any {diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsxindex fc7be2ad8e1b..ca1b8051eee5 100644--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx@@ -92,7 +92,6 @@ import {DataWarehouseSavedQuery,DataWarehouseSavedQueryDraft,DataWarehouseSavedQueryIncremental,- DataWarehouseSavedQueryIncrementalCheck,ExportContext,InsightModel,} from '~/types'@@ -2527,18 +2526,6 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([}const selectionProblem = await findSelectionProblem(candidates, values.sendRawQueryEnabled)- // Checked once as the dialog opens rather than on every keystroke: it only depends- // on the SQL being saved, which cannot change while the dialog is up. A failure- // leaves the incremental fields hidden, so the view saves as a normal full refresh.- let incrementalCheck: DataWarehouseSavedQueryIncrementalCheck | null = null- try {- incrementalCheck = await api.dataWarehouseSavedQueries.checkIncremental({- query: selectedRef.current ?? values.queryInput ?? '',- })- } catch {- incrementalCheck = null- }-const folderOptions: { value: string | null; label: string }[] = [{ value: null, label: 'No folder' },...values.dataWarehouseSavedQueryFolders.map((folder) => ({@@ -2582,7 +2569,7 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([isTest: false,materializeAfterSave,incrementalEnabled: false,- incrementalKey: incrementalCheck?.key_candidates[0] ?? null,+ incrementalKey: null,incrementalUniqueKey: [],incrementalLookbackSeconds: 0,},@@ -2674,7 +2661,7 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([<span className="text-muted cursor-pointer">ⓘ</span></Tooltip></div>- {value && <IncrementalConfigFields check={incrementalCheck} />}+ <IncrementalConfigFields queries={candidates.queries} /></>)}</LemonField>diff --git a/products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic.test.ts b/products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic.test.tsnew file mode 100644index 000000000000..142b9520a9d8--- /dev/null+++ b/products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic.test.ts@@ -0,0 +1,166 @@+import { waitFor } from '@testing-library/react'+import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'++import { lemonDialogLogic } from 'lib/lemon-ui/LemonDialog/lemonDialogLogic'+import { teamLogic } from 'scenes/teamLogic'++import { initKeaTests } from '~/test/init'++import * as api from '../generated/api'+import type { IncrementalEligibilityApi } from '../generated/api.schemas'+import { saveViewIncrementalLogic } from './saveViewIncrementalLogic'++const ELIGIBLE_CHECK: IncrementalEligibilityApi = {+ eligible: true,+ key_candidates: ['timestamp'],+ unique_key_candidates: ['id', 'timestamp'],+ key_candidate_types: { timestamp: 'datetime', id: 'integer' },+ blockers: [],+ warnings: [],+}++describe('saveViewIncrementalLogic', () => {+ beforeEach(() => {+ initKeaTests()+ })++ afterEach(() => {+ jest.restoreAllMocks()+ })++ it('keeps the name editable during the check and sets the first incremental column after it completes', async () => {+ let finishCheck!: (check: IncrementalEligibilityApi) => void+ const checkRequest = jest+ .spyOn(api, 'warehouseSavedQueriesCheckIncrementalCreate')+ .mockImplementation(() => new Promise((resolve) => (finishCheck = resolve)))+ const form = lemonDialogLogic({ dialogKey: 'save-view' })+ form.mount()+ form.actions.setFormValue('viewName', 'daily_events')+ const logic = saveViewIncrementalLogic({+ dialogKey: 'save-view',+ query: 'SELECT timestamp, id FROM events',+ enabled: true,+ })+ logic.mount()++ expect(logic.values.checkLoading).toBe(true)+ expect(form.values.form).not.toHaveProperty('incrementalEnabled')+ await waitFor(() => expect(checkRequest).toHaveBeenCalledTimes(1))+ expect(form.values.isFormValid).toBe(true)+ form.actions.setFormValue('viewName', 'edited_name')+ finishCheck(ELIGIBLE_CHECK)+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.check).toEqual(ELIGIBLE_CHECK)+ expect(form.values.form).toMatchObject({ viewName: 'edited_name', incrementalKey: 'timestamp' })+ logic.unmount()+ form.unmount()+ })++ it('discards an old response when the selected SQL changes', async () => {+ let finishOldCheck!: (check: IncrementalEligibilityApi) => void+ const checkRequest = jest+ .spyOn(api, 'warehouseSavedQueriesCheckIncrementalCreate')+ .mockImplementationOnce(() => new Promise((resolve) => (finishOldCheck = resolve)))+ .mockResolvedValueOnce({ ...ELIGIBLE_CHECK, key_candidates: ['created_at'] })+ const form = lemonDialogLogic({ dialogKey: 'save-view' })+ form.mount()+ const oldLogic = saveViewIncrementalLogic({+ dialogKey: 'save-view',+ query: 'SELECT timestamp FROM events',+ enabled: true,+ })+ oldLogic.mount()+ await waitFor(() => expect(checkRequest).toHaveBeenCalledTimes(1))+ form.actions.setFormValue('incrementalEnabled', true)+ form.actions.setFormValue('incrementalUniqueKey', ['id'])+ oldLogic.unmount()++ const newLogic = saveViewIncrementalLogic({+ dialogKey: 'save-view',+ query: 'SELECT created_at FROM persons',+ enabled: true,+ })+ newLogic.mount()+ await expectLogic(newLogic).toDispatchActions(['loadCheckSuccess'])+ finishOldCheck(ELIGIBLE_CHECK)+ await expectLogic(oldLogic).toFinishAllListeners()++ expect(form.values.form).toMatchObject({+ incrementalEnabled: false,+ incrementalKey: 'created_at',+ incrementalUniqueKey: [],+ })+ newLogic.unmount()+ form.unmount()+ })++ it('keeps the form usable with a full refresh when the check fails', async () => {+ const error = new Error('Check unavailable')+ jest.spyOn(api, 'warehouseSavedQueriesCheckIncrementalCreate').mockRejectedValue(error)+ const captureException = jest.spyOn(posthog, 'captureException').mockImplementation(() => undefined)+ const form = lemonDialogLogic({ dialogKey: 'save-view' })+ form.mount()+ form.actions.setFormValue('viewName', 'daily_events')+ const logic = saveViewIncrementalLogic({+ dialogKey: 'save-view',+ query: 'SELECT timestamp FROM events',+ enabled: true,+ })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.checkLoading).toBe(false)+ expect(logic.values.check).toBeNull()+ expect(form.values.isFormValid).toBe(true)+ expect(form.values.form).toMatchObject({ viewName: 'daily_events', incrementalEnabled: false })+ expect(captureException).toHaveBeenCalledWith(error)+ logic.unmount()+ form.unmount()+ })++ it('loads only after materialization is selected and preserves settings when it is toggled', async () => {+ const checkRequest = jest+ .spyOn(api, 'warehouseSavedQueriesCheckIncrementalCreate')+ .mockResolvedValue(ELIGIBLE_CHECK)+ const form = lemonDialogLogic({ dialogKey: 'save-view' })+ form.mount()+ const props = { dialogKey: 'save-view', query: 'SELECT timestamp, id FROM events', enabled: false }+ const logic = saveViewIncrementalLogic(props)+ logic.mount()+ expect(checkRequest).not.toHaveBeenCalled()++ saveViewIncrementalLogic({ ...props, enabled: true })+ await expectLogic(logic).toFinishAllListeners()+ form.actions.setFormValue('incrementalEnabled', true)+ form.actions.setFormValue('incrementalUniqueKey', ['id'])+ form.actions.setFormValue('incrementalLookbackSeconds', 3600)+ saveViewIncrementalLogic({ ...props, enabled: false })+ saveViewIncrementalLogic({ ...props, enabled: true })+ await expectLogic(logic).toFinishAllListeners()++ expect(checkRequest).toHaveBeenCalledTimes(1)+ expect(form.values.form).toMatchObject({+ incrementalEnabled: true,+ incrementalKey: 'timestamp',+ incrementalUniqueKey: ['id'],+ incrementalLookbackSeconds: 3600,+ })+ logic.unmount()+ form.unmount()+ })++ it('does not request an incremental check without a project', async () => {+ const checkRequest = jest.spyOn(api, 'warehouseSavedQueriesCheckIncrementalCreate')+ teamLogic.actions.loadCurrentTeamSuccess(null)+ const logic = saveViewIncrementalLogic({ dialogKey: 'save-view', query: 'SELECT 1', enabled: true })+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ expect(checkRequest).not.toHaveBeenCalled()+ expect(logic.values.checkLoading).toBe(false)+ expect(logic.values.check).toBeNull()+ logic.unmount()+ })+})diff --git a/products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic.ts b/products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic.tsnew file mode 100644index 000000000000..0b84edd11b66--- /dev/null+++ b/products/data_warehouse/frontend/sql_editor/saveViewIncrementalLogic.ts@@ -0,0 +1,117 @@+import { LogicWrapper, MakeLogicType, afterMount, connect, kea, key, listeners, path, props, propsChanged } from 'kea'+import type { FieldName } from 'kea-forms'+import { loaders } from 'kea-loaders'+import posthog from 'posthog-js'++import { ApiError } from 'lib/api-error'+import { lemonDialogLogic } from 'lib/lemon-ui/LemonDialog/lemonDialogLogic'+import { teamLogic } from 'scenes/teamLogic'++import * as api from '../generated/api'+import type { IncrementalEligibilityApi } from '../generated/api.schemas'++export interface SaveViewIncrementalLogicProps {+ query: string+ dialogKey: string+ enabled: boolean+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface saveViewIncrementalLogicValues {+ currentTeamId: number | null // teamLogic+ check: IncrementalEligibilityApi | null+ checkLoading: boolean+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface saveViewIncrementalLogicActions {+ setFormValue: (+ key: FieldName,+ value: any+ ) => {+ name: FieldName+ value: any+ } // lemonDialogLogic+ loadCheck: (_: void) => void+ loadCheckFailure: (+ error: string,+ errorObject?: any+ ) => {+ error: string+ errorObject?: any+ }+ loadCheckSuccess: (+ check: IncrementalEligibilityApi | null,+ payload?: void+ ) => {+ check: IncrementalEligibilityApi | null+ payload?: void+ }+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface saveViewIncrementalLogicMeta {+ key: string+}++export type saveViewIncrementalLogicType = MakeLogicType<+ saveViewIncrementalLogicValues,+ saveViewIncrementalLogicActions,+ SaveViewIncrementalLogicProps,+ saveViewIncrementalLogicMeta+>++export const saveViewIncrementalLogic: LogicWrapper<saveViewIncrementalLogicType> = kea<saveViewIncrementalLogicType>([+ props({} as SaveViewIncrementalLogicProps),+ key(({ dialogKey, query }) => `${dialogKey}:${query}`),+ path((key) => ['products', 'data_warehouse', 'sql_editor', 'saveViewIncrementalLogic', key]),+ connect((props: SaveViewIncrementalLogicProps) => ({+ values: [teamLogic, ['currentTeamId']],+ actions: [lemonDialogLogic({ dialogKey: props.dialogKey }), ['setFormValue']],+ })),+ loaders(({ actions, props, values }) => ({+ check: [+ null as IncrementalEligibilityApi | null,+ {+ loadCheck: async (_: void, breakpoint) => {+ // Form actions update the parent dialog, so wait for the mounting render to finish.+ await breakpoint(0)+ actions.setFormValue('incrementalEnabled', false)+ actions.setFormValue('incrementalKey', null)+ actions.setFormValue('incrementalUniqueKey', [])+ const teamId = values.currentTeamId+ if (teamId === null) {+ return null+ }+ let check: IncrementalEligibilityApi | null = null+ try {+ check = await api.warehouseSavedQueriesCheckIncrementalCreate(String(teamId), {+ query: props.query,+ })+ } catch (error) {+ if (!(error instanceof ApiError && error.status && error.status >= 400 && error.status < 500)) {+ posthog.captureException(error)+ }+ }+ breakpoint()+ return check+ },+ },+ ],+ })),+ listeners(({ actions }) => ({+ loadCheckSuccess: ({ check }) => {+ actions.setFormValue('incrementalKey', check?.key_candidates[0] ?? null)+ },+ })),+ afterMount(({ actions, props }) => {+ if (props.enabled) {+ actions.loadCheck()+ }+ }),+ propsChanged(({ actions, props, values }, oldProps) => {+ if (props.enabled && !oldProps.enabled && !values.check && !values.checkLoading) {+ actions.loadCheck()+ }+ }),+])