Allow overwriting a SQLLab query that has previously been saved (#8298)
* ignore direnv * allow overwriting saved queries * simplify state management a little bit * fix tests and linting
This commit is contained in:
parent
d55fe54198
commit
fbbc5f0577
|
|
@ -24,6 +24,7 @@
|
||||||
.coverage
|
.coverage
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.eggs
|
.eggs
|
||||||
|
.envrc
|
||||||
.idea
|
.idea
|
||||||
.mypy_cache
|
.mypy_cache
|
||||||
.python-version
|
.python-version
|
||||||
|
|
|
||||||
|
|
@ -79,9 +79,9 @@
|
||||||
"@superset-ui/legacy-preset-chart-deckgl": "^0.1.0",
|
"@superset-ui/legacy-preset-chart-deckgl": "^0.1.0",
|
||||||
"@superset-ui/legacy-preset-chart-nvd3": "^0.11.0",
|
"@superset-ui/legacy-preset-chart-nvd3": "^0.11.0",
|
||||||
"@superset-ui/number-format": "^0.12.1",
|
"@superset-ui/number-format": "^0.12.1",
|
||||||
"@superset-ui/query": "^0.12.2",
|
|
||||||
"@superset-ui/plugin-chart-table": "^0.11.0",
|
"@superset-ui/plugin-chart-table": "^0.11.0",
|
||||||
"@superset-ui/preset-chart-xy": "^0.11.0",
|
"@superset-ui/preset-chart-xy": "^0.11.0",
|
||||||
|
"@superset-ui/query": "^0.12.2",
|
||||||
"@superset-ui/time-format": "^0.12.1",
|
"@superset-ui/time-format": "^0.12.1",
|
||||||
"@superset-ui/translation": "^0.12.0",
|
"@superset-ui/translation": "^0.12.0",
|
||||||
"@types/react-json-tree": "^0.6.11",
|
"@types/react-json-tree": "^0.6.11",
|
||||||
|
|
|
||||||
|
|
@ -19,14 +19,18 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { FormControl } from 'react-bootstrap';
|
import { FormControl } from 'react-bootstrap';
|
||||||
import { shallow } from 'enzyme';
|
import { shallow } from 'enzyme';
|
||||||
|
import * as sinon from 'sinon';
|
||||||
import SaveQuery from '../../../src/SqlLab/components/SaveQuery';
|
import SaveQuery from '../../../src/SqlLab/components/SaveQuery';
|
||||||
import ModalTrigger from '../../../src/components/ModalTrigger';
|
import ModalTrigger from '../../../src/components/ModalTrigger';
|
||||||
|
import Button from '../../../src/components/Button';
|
||||||
|
|
||||||
describe('SavedQuery', () => {
|
describe('SavedQuery', () => {
|
||||||
const mockedProps = {
|
const mockedProps = {
|
||||||
dbId: 1,
|
query: {
|
||||||
schema: 'main',
|
dbId: 1,
|
||||||
sql: 'SELECT * FROM t',
|
schema: 'main',
|
||||||
|
sql: 'SELECT * FROM t',
|
||||||
|
},
|
||||||
defaultLabel: 'untitled',
|
defaultLabel: 'untitled',
|
||||||
animation: false,
|
animation: false,
|
||||||
};
|
};
|
||||||
|
|
@ -54,4 +58,27 @@ describe('SavedQuery', () => {
|
||||||
const modal = shallow(wrapper.instance().renderModalBody());
|
const modal = shallow(wrapper.instance().renderModalBody());
|
||||||
expect(modal.find(FormControl)).toHaveLength(2);
|
expect(modal.find(FormControl)).toHaveLength(2);
|
||||||
});
|
});
|
||||||
|
it('has a save button if this is a new query', () => {
|
||||||
|
const saveSpy = sinon.spy();
|
||||||
|
const wrapper = shallow(<SaveQuery {...mockedProps} onSave={saveSpy} />);
|
||||||
|
const modal = shallow(wrapper.instance().renderModalBody());
|
||||||
|
expect(modal.find(Button)).toHaveLength(2);
|
||||||
|
modal.find(Button).at(0).simulate('click');
|
||||||
|
expect(saveSpy.calledOnce).toBe(true);
|
||||||
|
});
|
||||||
|
it('has an update button if this is an existing query', () => {
|
||||||
|
const updateSpy = sinon.spy();
|
||||||
|
const props = {
|
||||||
|
...mockedProps,
|
||||||
|
query: {
|
||||||
|
...mockedProps.query,
|
||||||
|
remoteId: '42',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const wrapper = shallow(<SaveQuery {...props} onUpdate={updateSpy} />);
|
||||||
|
const modal = shallow(wrapper.instance().renderModalBody());
|
||||||
|
expect(modal.find(Button)).toHaveLength(3);
|
||||||
|
modal.find(Button).at(0).simulate('click');
|
||||||
|
expect(updateSpy.calledOnce).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ import shortid from 'shortid';
|
||||||
import JSONbig from 'json-bigint';
|
import JSONbig from 'json-bigint';
|
||||||
import { t } from '@superset-ui/translation';
|
import { t } from '@superset-ui/translation';
|
||||||
import { SupersetClient } from '@superset-ui/connection';
|
import { SupersetClient } from '@superset-ui/connection';
|
||||||
|
import invert from 'lodash/invert';
|
||||||
|
import mapKeys from 'lodash/mapKeys';
|
||||||
|
|
||||||
import { now } from '../../modules/dates';
|
import { now } from '../../modules/dates';
|
||||||
import {
|
import {
|
||||||
|
|
@ -32,6 +34,7 @@ import COMMON_ERR_MESSAGES from '../../utils/errorMessages';
|
||||||
|
|
||||||
export const RESET_STATE = 'RESET_STATE';
|
export const RESET_STATE = 'RESET_STATE';
|
||||||
export const ADD_QUERY_EDITOR = 'ADD_QUERY_EDITOR';
|
export const ADD_QUERY_EDITOR = 'ADD_QUERY_EDITOR';
|
||||||
|
export const UPDATE_QUERY_EDITOR = 'UPDATE_QUERY_EDITOR';
|
||||||
export const CLONE_QUERY_TO_NEW_TAB = 'CLONE_QUERY_TO_NEW_TAB';
|
export const CLONE_QUERY_TO_NEW_TAB = 'CLONE_QUERY_TO_NEW_TAB';
|
||||||
export const REMOVE_QUERY_EDITOR = 'REMOVE_QUERY_EDITOR';
|
export const REMOVE_QUERY_EDITOR = 'REMOVE_QUERY_EDITOR';
|
||||||
export const MERGE_TABLE = 'MERGE_TABLE';
|
export const MERGE_TABLE = 'MERGE_TABLE';
|
||||||
|
|
@ -82,6 +85,24 @@ export const addInfoToast = addInfoToastAction;
|
||||||
export const addSuccessToast = addSuccessToastAction;
|
export const addSuccessToast = addSuccessToastAction;
|
||||||
export const addDangerToast = addDangerToastAction;
|
export const addDangerToast = addDangerToastAction;
|
||||||
|
|
||||||
|
// a map of SavedQuery field names to the different names used client-side,
|
||||||
|
// because for now making the names consistent is too complicated
|
||||||
|
// so it might as well only happen in one place
|
||||||
|
const queryClientMapping = {
|
||||||
|
id: 'remoteId',
|
||||||
|
db_id: 'dbId',
|
||||||
|
client_id: 'id',
|
||||||
|
label: 'title',
|
||||||
|
};
|
||||||
|
const queryServerMapping = invert(queryClientMapping);
|
||||||
|
|
||||||
|
// uses a mapping like those above to convert object key names to another style
|
||||||
|
const fieldConverter = mapping => obj =>
|
||||||
|
mapKeys(obj, (value, key) => key in mapping ? mapping[key] : key);
|
||||||
|
|
||||||
|
const convertQueryToServer = fieldConverter(queryServerMapping);
|
||||||
|
const convertQueryToClient = fieldConverter(queryClientMapping);
|
||||||
|
|
||||||
export function resetState() {
|
export function resetState() {
|
||||||
return { type: RESET_STATE };
|
return { type: RESET_STATE };
|
||||||
}
|
}
|
||||||
|
|
@ -105,13 +126,29 @@ export function saveQuery(query) {
|
||||||
return dispatch =>
|
return dispatch =>
|
||||||
SupersetClient.post({
|
SupersetClient.post({
|
||||||
endpoint: '/savedqueryviewapi/api/create',
|
endpoint: '/savedqueryviewapi/api/create',
|
||||||
postPayload: query,
|
postPayload: convertQueryToServer(query),
|
||||||
stringify: false,
|
stringify: false,
|
||||||
})
|
})
|
||||||
.then(() => dispatch(addSuccessToast(t('Your query was saved'))))
|
.then(() => dispatch(addSuccessToast(t('Your query was saved'))))
|
||||||
.catch(() => dispatch(addDangerToast(t('Your query could not be saved'))));
|
.catch(() => dispatch(addDangerToast(t('Your query could not be saved'))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updateQueryEditor(alterations) {
|
||||||
|
return { type: UPDATE_QUERY_EDITOR, alterations };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function updateSavedQuery(query) {
|
||||||
|
return dispatch =>
|
||||||
|
SupersetClient.put({
|
||||||
|
endpoint: `/savedqueryviewapi/api/update/${query.remoteId}`,
|
||||||
|
postPayload: convertQueryToServer(query),
|
||||||
|
stringify: false,
|
||||||
|
})
|
||||||
|
.then(() => dispatch(addSuccessToast(t('Your query was updated'))))
|
||||||
|
.catch(() => dispatch(addDangerToast(t('Your query could not be updated'))))
|
||||||
|
.then(() => dispatch(updateQueryEditor(query)));
|
||||||
|
}
|
||||||
|
|
||||||
export function scheduleQuery(query) {
|
export function scheduleQuery(query) {
|
||||||
return dispatch =>
|
return dispatch =>
|
||||||
SupersetClient.post({
|
SupersetClient.post({
|
||||||
|
|
@ -504,13 +541,9 @@ export function popSavedQuery(saveQueryId) {
|
||||||
return function (dispatch) {
|
return function (dispatch) {
|
||||||
return SupersetClient.get({ endpoint: `/savedqueryviewapi/api/get/${saveQueryId}` })
|
return SupersetClient.get({ endpoint: `/savedqueryviewapi/api/get/${saveQueryId}` })
|
||||||
.then(({ json }) => {
|
.then(({ json }) => {
|
||||||
const { result } = json;
|
|
||||||
const queryEditorProps = {
|
const queryEditorProps = {
|
||||||
title: result.label,
|
...convertQueryToClient(json.result),
|
||||||
dbId: result.db_id ? parseInt(result.db_id, 10) : null,
|
|
||||||
schema: result.schema,
|
|
||||||
autorun: false,
|
autorun: false,
|
||||||
sql: result.sql,
|
|
||||||
};
|
};
|
||||||
return dispatch(addQueryEditor(queryEditorProps));
|
return dispatch(addQueryEditor(queryEditorProps));
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,11 @@ import Button from '../../components/Button';
|
||||||
import ModalTrigger from '../../components/ModalTrigger';
|
import ModalTrigger from '../../components/ModalTrigger';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
|
query: PropTypes.object,
|
||||||
defaultLabel: PropTypes.string,
|
defaultLabel: PropTypes.string,
|
||||||
sql: PropTypes.string,
|
|
||||||
schema: PropTypes.string,
|
|
||||||
dbId: PropTypes.number,
|
|
||||||
animation: PropTypes.bool,
|
animation: PropTypes.bool,
|
||||||
onSave: PropTypes.func,
|
onSave: PropTypes.func,
|
||||||
|
onUpdate: PropTypes.func,
|
||||||
saveQueryWarning: PropTypes.string,
|
saveQueryWarning: PropTypes.string,
|
||||||
};
|
};
|
||||||
const defaultProps = {
|
const defaultProps = {
|
||||||
|
|
@ -50,23 +49,21 @@ class SaveQuery extends React.PureComponent {
|
||||||
};
|
};
|
||||||
this.toggleSave = this.toggleSave.bind(this);
|
this.toggleSave = this.toggleSave.bind(this);
|
||||||
this.onSave = this.onSave.bind(this);
|
this.onSave = this.onSave.bind(this);
|
||||||
|
this.onUpdate = this.onUpdate.bind(this);
|
||||||
this.onCancel = this.onCancel.bind(this);
|
this.onCancel = this.onCancel.bind(this);
|
||||||
this.onLabelChange = this.onLabelChange.bind(this);
|
this.onLabelChange = this.onLabelChange.bind(this);
|
||||||
this.onDescriptionChange = this.onDescriptionChange.bind(this);
|
this.onDescriptionChange = this.onDescriptionChange.bind(this);
|
||||||
}
|
}
|
||||||
onSave() {
|
onSave() {
|
||||||
const query = {
|
this.props.onSave(this.queryPayload());
|
||||||
label: this.state.label,
|
this.close();
|
||||||
description: this.state.description,
|
}
|
||||||
db_id: this.props.dbId,
|
onUpdate() {
|
||||||
schema: this.props.schema,
|
this.props.onUpdate(this.queryPayload());
|
||||||
sql: this.props.sql,
|
this.close();
|
||||||
};
|
|
||||||
this.props.onSave(query);
|
|
||||||
this.saveModal.close();
|
|
||||||
}
|
}
|
||||||
onCancel() {
|
onCancel() {
|
||||||
this.saveModal.close();
|
this.close();
|
||||||
}
|
}
|
||||||
onLabelChange(e) {
|
onLabelChange(e) {
|
||||||
this.setState({ label: e.target.value });
|
this.setState({ label: e.target.value });
|
||||||
|
|
@ -74,10 +71,21 @@ class SaveQuery extends React.PureComponent {
|
||||||
onDescriptionChange(e) {
|
onDescriptionChange(e) {
|
||||||
this.setState({ description: e.target.value });
|
this.setState({ description: e.target.value });
|
||||||
}
|
}
|
||||||
|
queryPayload() {
|
||||||
|
return {
|
||||||
|
...this.props.query,
|
||||||
|
title: this.state.label,
|
||||||
|
description: this.state.description,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
close() {
|
||||||
|
if (this.saveModal) this.saveModal.close();
|
||||||
|
}
|
||||||
toggleSave(e) {
|
toggleSave(e) {
|
||||||
this.setState({ target: e.target, showSave: !this.state.showSave });
|
this.setState({ target: e.target, showSave: !this.state.showSave });
|
||||||
}
|
}
|
||||||
renderModalBody() {
|
renderModalBody() {
|
||||||
|
const isSaved = !!this.props.query.remoteId;
|
||||||
return (
|
return (
|
||||||
<FormGroup bsSize="small">
|
<FormGroup bsSize="small">
|
||||||
<Row>
|
<Row>
|
||||||
|
|
@ -124,12 +132,21 @@ class SaveQuery extends React.PureComponent {
|
||||||
)}
|
)}
|
||||||
<Row>
|
<Row>
|
||||||
<Col md={12}>
|
<Col md={12}>
|
||||||
|
{isSaved && (
|
||||||
|
<Button
|
||||||
|
bsStyle="primary"
|
||||||
|
onClick={this.onUpdate}
|
||||||
|
className="m-r-3"
|
||||||
|
>
|
||||||
|
{t('Update')}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
<Button
|
<Button
|
||||||
bsStyle="primary"
|
bsStyle={isSaved ? undefined : 'primary'}
|
||||||
onClick={this.onSave}
|
onClick={this.onSave}
|
||||||
className="m-r-3"
|
className="m-r-3"
|
||||||
>
|
>
|
||||||
{t('Save')}
|
{isSaved ? t('Save New') : t('Save')}
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={this.onCancel} className="cancelQuery">
|
<Button onClick={this.onCancel} className="cancelQuery">
|
||||||
{t('Cancel')}
|
{t('Cancel')}
|
||||||
|
|
|
||||||
|
|
@ -432,12 +432,11 @@ class SqlEditor extends React.PureComponent {
|
||||||
}
|
}
|
||||||
<span className="m-r-5">
|
<span className="m-r-5">
|
||||||
<SaveQuery
|
<SaveQuery
|
||||||
defaultLabel={qe.title}
|
query={qe}
|
||||||
sql={qe.sql}
|
defaultLabel={qe.description == null ? qe.title : qe.description}
|
||||||
className="m-r-5"
|
className="m-r-5"
|
||||||
onSave={this.props.actions.saveQuery}
|
onSave={this.props.actions.saveQuery}
|
||||||
schema={qe.schema}
|
onUpdate={this.props.actions.updateSavedQuery}
|
||||||
dbId={qe.dbId}
|
|
||||||
saveQueryWarning={this.props.saveQueryWarning}
|
saveQueryWarning={this.props.saveQueryWarning}
|
||||||
/>
|
/>
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
|
|
@ -39,11 +39,18 @@ export default function sqlLabReducer(state = {}, action) {
|
||||||
const newState = Object.assign({}, state, { tabHistory });
|
const newState = Object.assign({}, state, { tabHistory });
|
||||||
return addToArr(newState, 'queryEditors', action.queryEditor);
|
return addToArr(newState, 'queryEditors', action.queryEditor);
|
||||||
},
|
},
|
||||||
|
[actions.UPDATE_QUERY_EDITOR]() {
|
||||||
|
const id = action.alterations.remoteId;
|
||||||
|
const existing = state.queryEditors.find(qe => qe.remoteId === id);
|
||||||
|
if (existing == null) return state;
|
||||||
|
return alterInArr(state, 'queryEditors', existing, action.alterations, 'remoteId');
|
||||||
|
},
|
||||||
[actions.CLONE_QUERY_TO_NEW_TAB]() {
|
[actions.CLONE_QUERY_TO_NEW_TAB]() {
|
||||||
const progenitor = state.queryEditors.find(
|
const progenitor = state.queryEditors.find(
|
||||||
qe => qe.id === state.tabHistory[state.tabHistory.length - 1],
|
qe => qe.id === state.tabHistory[state.tabHistory.length - 1],
|
||||||
);
|
);
|
||||||
const qe = {
|
const qe = {
|
||||||
|
remoteId: progenitor.remoteId,
|
||||||
id: shortid.generate(),
|
id: shortid.generate(),
|
||||||
title: t('Copy of %s', progenitor.title),
|
title: t('Copy of %s', progenitor.title),
|
||||||
dbId: action.query.dbId ? action.query.dbId : null,
|
dbId: action.query.dbId ? action.query.dbId : null,
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
import { LOCALSTORAGE_MAX_QUERY_AGE_MS } from '../constants';
|
import { LOCALSTORAGE_MAX_QUERY_AGE_MS } from '../constants';
|
||||||
|
|
||||||
const PERSISTENT_QUERY_EDITOR_KEYS = new Set([
|
const PERSISTENT_QUERY_EDITOR_KEYS = new Set([
|
||||||
|
'remoteId',
|
||||||
'autorun',
|
'autorun',
|
||||||
'dbId',
|
'dbId',
|
||||||
'height',
|
'height',
|
||||||
|
|
|
||||||
|
|
@ -2678,7 +2678,7 @@ class Superset(BaseSupersetView):
|
||||||
@expose("/sql_json/", methods=["POST"])
|
@expose("/sql_json/", methods=["POST"])
|
||||||
@event_logger.log_this
|
@event_logger.log_this
|
||||||
def sql_json(self):
|
def sql_json(self):
|
||||||
"""Runs arbitrary sql and returns and json"""
|
"""Runs arbitrary sql and returns data as json"""
|
||||||
# Collect Values
|
# Collect Values
|
||||||
database_id: int = request.json.get("database_id")
|
database_id: int = request.json.get("database_id")
|
||||||
schema: str = request.json.get("schema")
|
schema: str = request.json.get("schema")
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,15 @@ class SavedQueryViewApi(SavedQueryView):
|
||||||
"sql",
|
"sql",
|
||||||
"extra_json",
|
"extra_json",
|
||||||
]
|
]
|
||||||
show_columns = ["label", "db_id", "schema", "description", "sql", "extra_json"]
|
show_columns = [
|
||||||
|
"id",
|
||||||
|
"label",
|
||||||
|
"db_id",
|
||||||
|
"schema",
|
||||||
|
"description",
|
||||||
|
"sql",
|
||||||
|
"extra_json",
|
||||||
|
]
|
||||||
add_columns = show_columns
|
add_columns = show_columns
|
||||||
edit_columns = add_columns
|
edit_columns = add_columns
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue