Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure that gsheets db connection form loads properly #22361

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, { FormEvent, useEffect } from 'react';
import React, { FormEvent } from 'react';
import { SupersetTheme, JsonObject } from '@superset-ui/core';
import { InputProps } from 'antd/lib/input';
import { Form } from 'src/components/Form';
Expand Down Expand Up @@ -103,24 +103,7 @@ const FORM_FIELD_MAP = {
account: validatedInputField,
};

const DatabaseConnectionForm = ({
dbModel: { parameters, default_driver },
db,
editNewDb,
getPlaceholder,
getValidation,
isEditMode = false,
onAddTableCatalog,
onChange,
onExtraInputChange,
onParametersChange,
onParametersUploadFileChange,
onQueryChange,
onRemoveTableCatalog,
setDatabaseDriver,
sslForced,
validationErrors,
}: {
interface DatabaseConnectionFormProps {
isEditMode?: boolean;
sslForced: boolean;
editNewDb?: boolean;
Expand All @@ -146,52 +129,64 @@ const DatabaseConnectionForm = ({
validationErrors: JsonObject | null;
getValidation: () => void;
getPlaceholder?: (field: string) => string | undefined;
setDatabaseDriver: (driver: string) => void;
}) => {
useEffect(() => {
setDatabaseDriver(default_driver);
}, [default_driver]);
return (
<Form>
<div
// @ts-ignore
css={(theme: SupersetTheme) => [
formScrollableStyles,
validatedFormStyles(theme),
]}
>
{parameters &&
FormFieldOrder.filter(
(key: string) =>
Object.keys(parameters.properties).includes(key) ||
key === 'database_name',
).map(field =>
FORM_FIELD_MAP[field]({
required: parameters.required?.includes(field),
changeMethods: {
onParametersChange,
onChange,
onQueryChange,
onParametersUploadFileChange,
onAddTableCatalog,
onRemoveTableCatalog,
onExtraInputChange,
},
validationErrors,
getValidation,
db,
key: field,
field,
isEditMode,
sslForced,
editNewDb,
placeholder: getPlaceholder ? getPlaceholder(field) : undefined,
}),
)}
</div>
</Form>
);
};
}

const DatabaseConnectionForm = ({
dbModel: { parameters },
db,
editNewDb,
getPlaceholder,
getValidation,
isEditMode = false,
onAddTableCatalog,
onChange,
onExtraInputChange,
onParametersChange,
onParametersUploadFileChange,
onQueryChange,
onRemoveTableCatalog,
sslForced,
validationErrors,
}: DatabaseConnectionFormProps) => (
<Form>
<div
// @ts-ignore
css={(theme: SupersetTheme) => [
formScrollableStyles,
validatedFormStyles(theme),
]}
>
{parameters &&
FormFieldOrder.filter(
(key: string) =>
Object.keys(parameters.properties).includes(key) ||
key === 'database_name',
).map(field =>
FORM_FIELD_MAP[field]({
required: parameters.required?.includes(field),
changeMethods: {
onParametersChange,
onChange,
onQueryChange,
onParametersUploadFileChange,
onAddTableCatalog,
onRemoveTableCatalog,
onExtraInputChange,
},
validationErrors,
getValidation,
db,
key: field,
field,
isEditMode,
sslForced,
editNewDb,
placeholder: getPlaceholder ? getPlaceholder(field) : undefined,
}),
)}
</div>
</Form>
);
export const FormFieldMap = FORM_FIELD_MAP;

export default DatabaseConnectionForm;
Original file line number Diff line number Diff line change
Expand Up @@ -1786,4 +1786,34 @@ describe('dbReducer', () => {
catalog: [],
});
});

test('it will add db information when one is selected', () => {
const { backend, ...db } = databaseFixture;
const action: DBReducerActionType = {
type: ActionType.dbSelected,
payload: {
engine_information: {
supports_file_upload: true,
},
...db,
driver: db.driver,
engine: backend,
},
};
const currentState = dbReducer({}, action);

expect(currentState).toEqual({
database_name: db.database_name,
engine: backend,
configuration_method: db.configuration_method,
engine_information: {
supports_file_upload: true,
},
driver: db.driver,
expose_in_sqllab: true,
extra: '{"allows_virtual_table_explore":true}',
is_managed_externally: false,
name: 'PostgresDB',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export enum ActionType {
addTableCatalogSheet,
configMethodChange,
dbSelected,
driverChange,
editorChange,
extraEditorChange,
extraInputChange,
Expand Down Expand Up @@ -180,6 +179,7 @@ export type DBReducerActionType =
engine?: string;
configuration_method: CONFIGURATION_METHOD;
engine_information?: {};
driver?: string;
};
}
| {
Expand All @@ -198,10 +198,6 @@ export type DBReducerActionType =
engine?: string;
configuration_method: CONFIGURATION_METHOD;
};
}
| {
type: ActionType.driverChange;
payload: string;
};

const StyledBtns = styled.div`
Expand Down Expand Up @@ -426,12 +422,6 @@ export function dbReducer(
...action.payload,
};

case ActionType.driverChange:
return {
...trimmedState,
driver: action.payload,
};

case ActionType.reset:
default:
return null;
Expand Down Expand Up @@ -753,7 +743,8 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const selectedDbModel = availableDbs?.databases.filter(
(db: DatabaseObject) => db.name === database_name,
)[0];
const { engine, parameters, engine_information } = selectedDbModel;
const { engine, parameters, engine_information, default_driver } =
selectedDbModel;
const isDynamic = parameters !== undefined;
setDB({
type: ActionType.dbSelected,
Expand All @@ -764,6 +755,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
? CONFIGURATION_METHOD.DYNAMIC_FORM
: CONFIGURATION_METHOD.SQLALCHEMY_URI,
engine_information,
driver: default_driver,
},
});

Expand Down Expand Up @@ -1292,9 +1284,6 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
sslForced={sslForced}
dbModel={dbModel}
db={db as DatabaseObject}
setDatabaseDriver={(driver: string) => {
onChange(ActionType.driverChange, driver);
}}
onParametersChange={({ target }: { target: HTMLInputElement }) =>
onChange(ActionType.parametersChange, {
type: target.type,
Expand Down Expand Up @@ -1466,9 +1455,6 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
sslForced={sslForced}
dbModel={dbModel}
db={db as DatabaseObject}
setDatabaseDriver={(driver: string) => {
onChange(ActionType.driverChange, driver);
}}
onParametersChange={({ target }: { target: HTMLInputElement }) =>
onChange(ActionType.parametersChange, {
type: target.type,
Expand Down Expand Up @@ -1660,9 +1646,6 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
db={db}
sslForced={sslForced}
dbModel={dbModel}
setDatabaseDriver={(driver: string) => {
onChange(ActionType.driverChange, driver);
}}
onAddTableCatalog={() => {
setDB({ type: ActionType.addTableCatalogSheet });
}}
Expand Down