mirror of
https://github.com/apache/superset.git
synced 2024-09-12 00:29:39 -04:00
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 Button from 'src/components/Button';
|
||||||
import shortid from 'shortid';
|
import shortid from 'shortid';
|
||||||
import rison from 'rison';
|
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 { debounce } from 'lodash';
|
||||||
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
|
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
|
||||||
import { SaveDatasetModal } from 'src/SqlLab/components/SaveDatasetModal';
|
import { SaveDatasetModal } from 'src/SqlLab/components/SaveDatasetModal';
|
||||||
import { put as updateDatset } from 'src/api/dataset';
|
|
||||||
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
|
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
|
||||||
import Loading from '../../components/Loading';
|
import Loading from '../../components/Loading';
|
||||||
import ExploreCtasResultsButton from './ExploreCtasResultsButton';
|
import ExploreCtasResultsButton from './ExploreCtasResultsButton';
|
||||||
@ -133,6 +138,27 @@ const ResultSetErrorMessage = styled.div`
|
|||||||
padding-top: ${({ theme }) => 4 * theme.gridUnit}px;
|
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<
|
export default class ResultSet extends React.PureComponent<
|
||||||
ResultSetProps,
|
ResultSetProps,
|
||||||
ResultSetState
|
ResultSetState
|
||||||
@ -236,12 +262,11 @@ export default class ResultSet extends React.PureComponent<
|
|||||||
};
|
};
|
||||||
|
|
||||||
handleOverwriteDataset = async () => {
|
handleOverwriteDataset = async () => {
|
||||||
const { sql, results, dbId } = this.props.query;
|
const { sql, results } = this.props.query;
|
||||||
const { datasetToOverwrite } = this.state;
|
const { datasetToOverwrite } = this.state;
|
||||||
|
|
||||||
await updateDatset(
|
await updateDataset(
|
||||||
datasetToOverwrite.datasetId,
|
datasetToOverwrite.datasetId,
|
||||||
dbId,
|
|
||||||
sql,
|
sql,
|
||||||
results.selected_columns.map(d => ({ column_name: d.name })),
|
results.selected_columns.map(d => ({ column_name: d.name })),
|
||||||
true,
|
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:
|
if self._model:
|
||||||
try:
|
try:
|
||||||
dataset = DatasetDAO.update(
|
dataset = DatasetDAO.update(
|
||||||
model=self._model,
|
model=self._model, properties=self._properties,
|
||||||
properties=self._properties,
|
|
||||||
override_columns=self.override_columns,
|
|
||||||
)
|
)
|
||||||
return dataset
|
return dataset
|
||||||
except DAOUpdateFailedError as ex:
|
except DAOUpdateFailedError as ex:
|
||||||
|
@ -144,31 +144,22 @@ class DatasetDAO(BaseDAO): # pylint: disable=too-many-public-methods
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update( # pylint: disable=arguments-differ
|
def update( # pylint: disable=arguments-differ
|
||||||
cls,
|
cls, model: SqlaTable, properties: Dict[str, Any], commit: bool = True
|
||||||
model: SqlaTable,
|
|
||||||
properties: Dict[str, Any],
|
|
||||||
commit: bool = True,
|
|
||||||
override_columns: bool = False,
|
|
||||||
) -> Optional[SqlaTable]:
|
) -> Optional[SqlaTable]:
|
||||||
"""
|
"""
|
||||||
Updates a Dataset model on the metadata DB
|
Updates a Dataset model on the metadata DB
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if "columns" in properties:
|
if "columns" in properties:
|
||||||
properties["columns"] = cls.update_columns(
|
properties["columns"] = cls.update_columns(
|
||||||
model, properties.get("columns", []), commit=commit
|
model, properties.get("columns", []), commit=commit
|
||||||
)
|
)
|
||||||
|
|
||||||
if "metrics" in properties:
|
if "metrics" in properties:
|
||||||
properties["metrics"] = cls.update_metrics(
|
properties["metrics"] = cls.update_metrics(
|
||||||
model, properties.get("metrics", []), commit=commit
|
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)
|
return super().update(model, properties, commit=False)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
Loading…
Reference in New Issue
Block a user