Skip to content

Commit

Permalink
refactor update call to datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh committed Sep 28, 2021
1 parent 08d2a94 commit 0fc7454
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 80 deletions.
55 changes: 39 additions & 16 deletions superset-frontend/src/SqlLab/components/ResultSet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ import { RadioChangeEvent } from 'antd/lib/radio';
import Button from 'src/components/Button';
import shortid from 'shortid';
import rison from 'rison';
import { styled, t, makeApi } from '@superset-ui/core';
import {
styled,
t,
makeApi,
SupersetClient,
JsonResponse,
} from '@superset-ui/core';
import { debounce } from 'lodash';
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
import { SaveDatasetModal } from 'src/SqlLab/components/SaveDatasetModal';
import { put as updateDatset } from 'src/api/dataset';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
import Loading from '../../components/Loading';
import ExploreCtasResultsButton from './ExploreCtasResultsButton';
Expand Down Expand Up @@ -133,6 +138,27 @@ const ResultSetErrorMessage = styled.div`
padding-top: ${({ theme }) => 4 * theme.gridUnit}px;
`;

const updateDataset = async (
datasetId: number,
sql: string,
columns: Array<Record<string, any>>,
overrideColumns: boolean,
) => {
const endpoint = `api/v1/dataset/${datasetId}?override_columns=${overrideColumns}`;
const headers = { 'Content-Type': 'application/json' };
const body = JSON.stringify({
sql,
columns,
});

const data: JsonResponse = await SupersetClient.put({
endpoint,
headers,
body,
});
return data.json.result;
};

export default class ResultSet extends React.PureComponent<
ResultSetProps,
ResultSetState
Expand Down Expand Up @@ -236,12 +262,11 @@ export default class ResultSet extends React.PureComponent<
};

handleOverwriteDataset = async () => {
const { sql, results, dbId } = this.props.query;
const { sql, results } = this.props.query;
const { datasetToOverwrite } = this.state;

await updateDatset(
await updateDataset(
datasetToOverwrite.datasetId,
dbId,
sql,
results.selected_columns.map(d => ({ column_name: d.name })),
true,
Expand Down Expand Up @@ -487,17 +512,15 @@ export default class ResultSet extends React.PureComponent<
onChangeAutoComplete={this.handleOnChangeAutoComplete}
/>
<ResultSetButtons>
{this.props.visualize &&
this.props.database &&
this.props.database.allows_virtual_table_explore && (
<ExploreResultsButton
// @ts-ignore Redux types are difficult to work with, ignoring for now
query={this.props.query}
database={this.props.database}
actions={this.props.actions}
onClick={this.handleExploreBtnClick}
/>
)}
{this.props.visualize && this.props.database && (
<ExploreResultsButton
// @ts-ignore Redux types are difficult to work with, ignoring for now
query={this.props.query}
database={this.props.database}
actions={this.props.actions}
onClick={this.handleExploreBtnClick}
/>
)}
{this.props.csv && (
<Button
buttonSize="small"
Expand Down
63 changes: 0 additions & 63 deletions superset-frontend/src/api/dataset.ts

This file was deleted.

2 changes: 1 addition & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ def before_update(

# Check whether the relevant attributes have changed.
state = db.inspect(target) # pylint: disable=no-member
for attr in ["schema", "table_name"]:
for attr in ["database_id", "schema", "table_name"]:
history = state.get_history(attr, True)

if history.has_changes():
Expand Down

0 comments on commit 0fc7454

Please sign in to comment.