mirror of https://github.com/apache/superset.git
fix: Fix Uniqueness check before update for Sqllab Overwrites (#16859)
* add condition to make sure columns are available before throwing * fix * remove database_id from history changed * refactor update call to datasets * cleanup * oops * prettier
This commit is contained in:
parent
904c506591
commit
331de0c4ae
|
@ -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';
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { SupersetClient, JsonResponse } from '@superset-ui/core';
|
||||
import rison from 'rison';
|
||||
|
||||
export const getByUser = async (userId: number) => {
|
||||
const queryParams = rison.encode({
|
||||
filters: [
|
||||
{
|
||||
col: 'owners',
|
||||
opr: 'rel_m_m',
|
||||
value: userId,
|
||||
},
|
||||
],
|
||||
order_column: 'changed_on_delta_humanized',
|
||||
order_direction: 'desc',
|
||||
});
|
||||
const endpoint = `/api/v1/dataset?q=${queryParams}`;
|
||||
const data: JsonResponse = await SupersetClient.get({
|
||||
endpoint,
|
||||
});
|
||||
return data.json.result;
|
||||
};
|
||||
|
||||
export const put = async (
|
||||
datasetId: number,
|
||||
dbId: 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,
|
||||
database_id: dbId,
|
||||
});
|
||||
|
||||
const data: JsonResponse = await SupersetClient.put({
|
||||
endpoint,
|
||||
headers,
|
||||
body,
|
||||
});
|
||||
return data.json.result;
|
||||
};
|
|
@ -65,9 +65,7 @@ class UpdateDatasetCommand(UpdateMixin, BaseCommand):
|
|||
if self._model:
|
||||
try:
|
||||
dataset = DatasetDAO.update(
|
||||
model=self._model,
|
||||
properties=self._properties,
|
||||
override_columns=self.override_columns,
|
||||
model=self._model, properties=self._properties,
|
||||
)
|
||||
return dataset
|
||||
except DAOUpdateFailedError as ex:
|
||||
|
|
|
@ -144,31 +144,22 @@ class DatasetDAO(BaseDAO): # pylint: disable=too-many-public-methods
|
|||
|
||||
@classmethod
|
||||
def update( # pylint: disable=arguments-differ
|
||||
cls,
|
||||
model: SqlaTable,
|
||||
properties: Dict[str, Any],
|
||||
commit: bool = True,
|
||||
override_columns: bool = False,
|
||||
cls, model: SqlaTable, properties: Dict[str, Any], commit: bool = True
|
||||
) -> Optional[SqlaTable]:
|
||||
"""
|
||||
Updates a Dataset model on the metadata DB
|
||||
"""
|
||||
|
||||
if "columns" in properties:
|
||||
properties["columns"] = cls.update_columns(
|
||||
model, properties.get("columns", []), commit=commit
|
||||
)
|
||||
|
||||
if "metrics" in properties:
|
||||
properties["metrics"] = cls.update_metrics(
|
||||
model, properties.get("metrics", []), commit=commit
|
||||
)
|
||||
|
||||
if override_columns:
|
||||
# remove columns initially for full refresh
|
||||
original_properties = properties["columns"]
|
||||
properties["columns"] = []
|
||||
super().update(model, properties, commit=commit)
|
||||
properties["columns"] = original_properties
|
||||
|
||||
return super().update(model, properties, commit=False)
|
||||
|
||||
@classmethod
|
||||
|
|
Loading…
Reference in New Issue