Skip to content

Commit

Permalink
fix(sqllab): table preview has gone (apache#25977)
Browse files Browse the repository at this point in the history
(cherry picked from commit cdbbd83)
  • Loading branch information
justinpark authored and josedev-union committed Jan 22, 2024
1 parent 1e5c295 commit b166fe2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(<DatabaseSelector {...props} />, { useRedux: true, store });
const { rerender } = render(<DatabaseSelector {...props} db={null} />, {
useRedux: true,
store,
});
await waitFor(() => expect(fetchMock.calls(databaseApiRoute).length).toBe(1));
rerender(<DatabaseSelector {...props} />);
expect(props.onSchemaChange).toBeCalledTimes(0);
const select = screen.getByRole('combobox', {
name: 'Select schema or type to search schemas',
});
Expand All @@ -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);
});
10 changes: 7 additions & 3 deletions superset-frontend/src/components/DatabaseSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -133,6 +133,8 @@ export default function DatabaseSelector({
const [currentSchema, setCurrentSchema] = useState<SchemaOption | undefined>(
schema ? { label: schema, value: schema, title: schema } : undefined,
);
const schemaRef = useRef(schema);
schemaRef.current = schema;
const { addSuccessToast } = useToasts();

const loadDatabases = useMemo(
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}

Expand Down

0 comments on commit b166fe2

Please sign in to comment.