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: Showing errors for SQLA forms #15462

Merged
merged 6 commits into from
Jun 29, 2021
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 @@ -17,6 +17,7 @@
* under the License.
*/
import { DATABASE_LIST } from './helper';

describe('Add database', () => {
beforeEach(() => {
cy.login();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ const ModalHeader = ({
<CreateHeaderTitle>Enter Primary Credentials</CreateHeaderTitle>
<CreateHeaderSubtitle>
Need help? Learn how to connect your database{' '}
<a href={DOCUMENTATION_LINK} target="_blank" rel="noopener noreferrer">
<a
href={supersetTextDocs?.default || DOCUMENTATION_LINK}
target="_blank"
rel="noopener noreferrer"
>
here
</a>
.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
addDangerToast,
);

const isDynamic = (engine: string | undefined) =>
availableDbs?.databases.filter(
(DB: DatabaseObject) => DB.backend === engine || DB.engine === engine,
)[0].parameters !== undefined;
const showDBError = validationErrors || dbErrors;
const isEmpty = (data?: Object | null) =>
data && Object.keys(data).length === 0;
Expand Down Expand Up @@ -398,13 +402,13 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
// Clone DB object
const dbToUpdate = JSON.parse(JSON.stringify(update));

// Validate DB before saving
await getValidation(dbToUpdate, true);
if (validationErrors && !isEmpty(validationErrors)) {
return;
}

if (dbToUpdate.configuration_method === CONFIGURATION_METHOD.DYNAMIC_FORM) {
// Validate DB before saving
await getValidation(dbToUpdate, true);
if (validationErrors && !isEmpty(validationErrors)) {
return;
}

if (dbToUpdate?.parameters?.query) {
// convert query params into dictionary
dbToUpdate.parameters.query = JSON.parse(
Expand All @@ -426,27 +430,29 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
}
}

if (dbToUpdate?.extra_json) {
// convert extra_json to back to string
dbToUpdate.extra = JSON.stringify({
...dbToUpdate.extra_json,
metadata_params: JSON.parse(
(dbToUpdate?.extra_json?.metadata_params as string) || '{}',
),
engine_params: JSON.parse(
(dbToUpdate?.extra_json?.engine_params as string) || '{}',
),
schemas_allowed_for_csv_upload:
(dbToUpdate?.extra_json?.schemas_allowed_for_csv_upload as string) ||
'[]',
});
}
console.log(dbToUpdate.extra);

if (db?.id) {
if (dbToUpdate?.extra_json) {
// convert extra_json to back to string
dbToUpdate.extra = JSON.stringify({
...dbToUpdate.extra_json,
metadata_params: JSON.parse(
dbToUpdate?.extra_json?.metadata_params as string,
),
engine_params: JSON.parse(
dbToUpdate?.extra_json?.engine_params as string,
),
schemas_allowed_for_csv_upload: JSON.parse(
dbToUpdate?.extra_json?.schemas_allowed_for_csv_upload as string,
),
});
}
setLoading(true);
const result = await updateResource(
db.id as number,
dbToUpdate as DatabaseObject,
true,
dbToUpdate.configuration_method === CONFIGURATION_METHOD.DYNAMIC_FORM, // onShow toast on SQLA Forms
);
if (result) {
if (onDatabaseAdd) {
Expand All @@ -458,23 +464,11 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
}
} else if (db) {
// Create
if (dbToUpdate?.extra_json) {
// convert extra_json to back to string
dbToUpdate.extra = JSON.stringify({
...dbToUpdate.extra_json,
metadata_params: JSON.parse(
dbToUpdate?.extra_json?.metadata_params as string,
),
engine_params: JSON.parse(
dbToUpdate?.extra_json?.engine_params as string,
),
schemas_allowed_for_csv_upload: JSON.parse(
dbToUpdate?.extra_json?.schemas_allowed_for_csv_upload as string,
),
});
}
setLoading(true);
const dbId = await createResource(dbToUpdate as DatabaseObject, true);
const dbId = await createResource(
dbToUpdate as DatabaseObject,
dbToUpdate.configuration_method === CONFIGURATION_METHOD.DYNAMIC_FORM, // onShow toast on SQLA Forms
);
if (dbId) {
setHasConnectedDb(true);
if (onDatabaseAdd) {
Expand Down Expand Up @@ -804,11 +798,6 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
);
};

const isDynamic = (engine: string | undefined) =>
availableDbs?.databases.filter(
(DB: DatabaseObject) => DB.backend === engine || DB.engine === engine,
)[0].parameters !== undefined;

return useTabLayout ? (
<Modal
css={(theme: SupersetTheme) => [
Expand Down