: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(data-warehouse): say when a pasted connection string can't be read (#95858)
Tom Owers2f4e391d5784
diff --git a/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx b/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsxindex e30d0b6e9a2e..58ef9c18ee26 100644--- a/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx+++ b/products/data_warehouse/frontend/shared/components/forms/SourceForm.test.tsx@@ -1,4 +1,13 @@-import { SourceConfig, SourceFieldSelectConfig, SourceFieldSwitchGroupConfig } from '~/queries/schema/schema-general'+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'++import {+ SourceConfig,+ SourceFieldInputConfig,+ SourceFieldSelectConfig,+ SourceFieldSwitchGroupConfig,+} from '~/queries/schema/schema-general'import { sourceFieldToElement } from './SourceForm'@@ -47,7 +56,29 @@ const switchGroupState = (storedGroupValue: any, formValue?: any): { checked: boreturn { checked: toggle.props.checked, childrenVisible: !!children.find((child: any) => child?.props?.name) }}+const CONNECTION_STRING_FIELD: SourceFieldInputConfig = {+ type: 'text',+ name: 'connection_string',+ label: 'Connection string (optional)',+ required: false,+ placeholder: 'postgresql://user:password@localhost:5432/database',+ secret: true,+}++const POSTGRES_CONFIG = { name: 'Postgres', fields: [] } as unknown as SourceConfig++// Renders the connection string field at a given value and reports whether it tells the user the+// string didn't parse.+const connectionStringWarns = (value: string): boolean => {+ const element = sourceFieldToElement(CONNECTION_STRING_FIELD, POSTGRES_CONFIG)+ const lemonField = element.props.children.find((child: any) => child?.props?.name === 'connection_string')+ render(lemonField.props.children({ value, onChange: jest.fn() }))+ return screen.queryByText(/Couldn't read that connection string/) !== null+}+describe('sourceFieldToElement', () => {+ afterEach(cleanup)+it('renders a select field caption as field help text', () => {const element = sourceFieldToElement(SELECT_FIELD, SOURCE_CONFIG)expect(element.props.help).toBeTruthy()@@ -58,6 +89,18 @@ describe('sourceFieldToElement', () => {expect(element.props.help).toBeUndefined()})+ // A string the parser rejects used to prefill nothing and say nothing, so the user only found+ // out at the next step, as required-field errors on Host, Port, Database, User and Password.+ it.each([+ ['stays quiet for a string it can read', 'postgresql://alice:[email protected]:5432/analytics', false],+ ['warns when the database is missing', 'postgresql://alice:[email protected]:5432', true],+ ['warns when the scheme belongs to another source', 'mysql://alice:[email protected]:3306/analytics', true],+ ['stays quiet while the value is still half typed', 'postgresq', false],+ ['stays quiet when the field is empty', '', false],+ ])('%s', (_name, value, expected) => {+ expect(connectionStringWarns(value)).toBe(expected)+ })+// job_inputs cross an encrypted field that stringifies booleans, so the form prefills `enabled`// as "True"/"False". LemonSwitch checks `checked === true`, so an untranslated "True" rendered// the toggle off while the truthy string still expanded the children below it.diff --git a/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx b/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsxindex cfc7742e5d60..9cb2ca433bee 100644--- a/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx+++ b/products/data_warehouse/frontend/shared/components/forms/SourceForm.tsx@@ -161,35 +161,59 @@ export const sourceFieldToElement = (return (<React.Fragment key={field.name}><LemonField name={field.name} label={field.label}>- {({ onChange }) => (- <LemonInput- key={field.name}- className="ph-connection-string"- data-attr={field.name}- placeholder={field.placeholder}- type="text"- onChange={(updatedConnectionString) => {- onChange(updatedConnectionString)- const { isValid, fields } = parseConnectionStringForSource(- sourceConfig.name,- updatedConnectionString- )+ {({ value, onChange }) => {+ const typed = String(value ?? '')+ // A half-typed string parses as garbage, so only report a failure once the+ // value carries a scheme and reads as a whole connection string.+ const looksLikeConnectionString = typed.includes('://')+ const unparsed =+ looksLikeConnectionString &&+ !parseConnectionStringForSource(sourceConfig.name, typed).isValid+ return (+ <>+ <LemonInput+ key={field.name}+ className="ph-connection-string"+ data-attr={field.name}+ placeholder={field.placeholder}+ type="text"+ onChange={(updatedConnectionString) => {+ onChange(updatedConnectionString)+ const { isValid, fields } = parseConnectionStringForSource(+ sourceConfig.name,+ updatedConnectionString+ )- if (isValid) {- for (const { path, value } of fields) {- if (setSourceConnectionDetailsValue) {- setSourceConnectionDetailsValue(['payload', ...path], value)- } else {- sourceWizardLogic.actions.setSourceConnectionDetailsValue(- ['payload', ...path],- value- )+ if (isValid) {+ for (const { path, value } of fields) {+ if (setSourceConnectionDetailsValue) {+ setSourceConnectionDetailsValue(['payload', ...path], value)+ } else {+ sourceWizardLogic.actions.setSourceConnectionDetailsValue(+ ['payload', ...path],+ value+ )+ }+ }}- }- }- }}- />- )}+ }}+ />+ {unparsed && (+ <p className="m-0 mt-1 text-xs text-warning">+ Couldn't read that connection string, so the fields below are still empty.{' '}+ {field.placeholder ? (+ <>+ Check it looks like <code>{field.placeholder}</code>, or fill them in+ yourself.+ </>+ ) : (+ 'Fill them in yourself instead.'+ )}+ </p>+ )}+ </>+ )+ }}</LemonField><LemonDivider /></React.Fragment>