diff --git a/superset-frontend/src/components/DatabaseSelector/DatabaseSelector.test.tsx b/superset-frontend/src/components/DatabaseSelector/DatabaseSelector.test.tsx index 7635361d89..874d22ea6b 100644 --- a/superset-frontend/src/components/DatabaseSelector/DatabaseSelector.test.tsx +++ b/superset-frontend/src/components/DatabaseSelector/DatabaseSelector.test.tsx @@ -290,7 +290,13 @@ test('Sends the correct db when changing the database', async () => { test('Sends the correct schema when changing the schema', async () => { const props = createProps(); - render(, { useRedux: true, store }); + const { rerender } = render(, { + useRedux: true, + store, + }); + await waitFor(() => expect(fetchMock.calls(databaseApiRoute).length).toBe(1)); + rerender(); + expect(props.onSchemaChange).toBeCalledTimes(0); const select = screen.getByRole('combobox', { name: 'Select schema or type to search schemas', }); @@ -301,4 +307,5 @@ test('Sends the correct schema when changing the schema', async () => { await waitFor(() => expect(props.onSchemaChange).toHaveBeenCalledWith('information_schema'), ); + expect(props.onSchemaChange).toBeCalledTimes(1); }); diff --git a/superset-frontend/src/components/DatabaseSelector/index.tsx b/superset-frontend/src/components/DatabaseSelector/index.tsx index d17489a9c2..7b4afd9af0 100644 --- a/superset-frontend/src/components/DatabaseSelector/index.tsx +++ b/superset-frontend/src/components/DatabaseSelector/index.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import React, { ReactNode, useState, useMemo, useEffect } from 'react'; +import React, { ReactNode, useState, useMemo, useEffect, useRef } from 'react'; import { styled, SupersetClient, t } from '@superset-ui/core'; import rison from 'rison'; import { AsyncSelect, Select } from 'src/components'; @@ -133,6 +133,8 @@ export default function DatabaseSelector({ const [currentSchema, setCurrentSchema] = useState( schema ? { label: schema, value: schema, title: schema } : undefined, ); + const schemaRef = useRef(schema); + schemaRef.current = schema; const { addSuccessToast } = useToasts(); const loadDatabases = useMemo( @@ -215,7 +217,7 @@ export default function DatabaseSelector({ function changeSchema(schema: SchemaOption | undefined) { setCurrentSchema(schema); - if (onSchemaChange) { + if (onSchemaChange && schema?.value !== schemaRef.current) { onSchemaChange(schema?.value); } } @@ -229,7 +231,9 @@ export default function DatabaseSelector({ onSuccess: (schemas, isFetched) => { if (schemas.length === 1) { changeSchema(schemas[0]); - } else if (!schemas.find(schemaOption => schema === schemaOption.value)) { + } else if ( + !schemas.find(schemaOption => schemaRef.current === schemaOption.value) + ) { changeSchema(undefined); }