Skip to content

Commit

Permalink
Revert "feat(sqllab): save query parameters in database (apache#21682)"
Browse files Browse the repository at this point in the history
This reverts commit 61319fd.
  • Loading branch information
sadpandajoe committed Oct 17, 2022
1 parent e3d4d19 commit 8413327
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 132 deletions.
49 changes: 18 additions & 31 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ const ERR_MSG_CANT_LOAD_QUERY = t("The query couldn't be loaded");
const queryClientMapping = {
id: 'remoteId',
db_id: 'dbId',
client_id: 'id',
label: 'name',
template_parameters: 'templateParams',
};
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));

export const convertQueryToServer = fieldConverter(queryServerMapping);
export const convertQueryToClient = fieldConverter(queryClientMapping);
const convertQueryToServer = fieldConverter(queryServerMapping);
const convertQueryToClient = fieldConverter(queryClientMapping);

export function getUpToDateQuery(rootState, queryEditor, key) {
const {
Expand Down Expand Up @@ -903,23 +903,17 @@ export function queryEditorSetAutorun(queryEditor, autorun) {
};
}

export function queryEditorSetTitle(queryEditor, name, id) {
export function queryEditorSetTitle(queryEditor, name) {
return function (dispatch) {
const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)
? SupersetClient.put({
endpoint: encodeURI(`/tabstateview/${id}`),
endpoint: encodeURI(`/tabstateview/${queryEditor.id}`),
postPayload: { label: name },
})
: Promise.resolve();

return sync
.then(() =>
dispatch({
type: QUERY_EDITOR_SET_TITLE,
queryEditor: { ...queryEditor, id },
name,
}),
)
.then(() => dispatch({ type: QUERY_EDITOR_SET_TITLE, queryEditor, name }))
.catch(() =>
dispatch(
addDangerToast(
Expand All @@ -932,26 +926,21 @@ export function queryEditorSetTitle(queryEditor, name, id) {
};
}

export function saveQuery(query, clientId) {
const { id, ...payload } = convertQueryToServer(query);

export function saveQuery(query) {
return dispatch =>
SupersetClient.post({
endpoint: '/api/v1/saved_query/',
jsonPayload: convertQueryToServer(payload),
endpoint: '/savedqueryviewapi/api/create',
postPayload: convertQueryToServer(query),
stringify: false,
})
.then(result => {
const savedQuery = convertQueryToClient({
id: result.json.id,
...result.json.result,
});
const savedQuery = convertQueryToClient(result.json.item);
dispatch({
type: QUERY_EDITOR_SAVED,
query,
clientId,
result: savedQuery,
});
dispatch(queryEditorSetTitle(query, query.name, clientId));
dispatch(queryEditorSetTitle(query, query.name));
return savedQuery;
})
.catch(() =>
Expand All @@ -977,17 +966,16 @@ export const addSavedQueryToTabState =
});
};

export function updateSavedQuery(query, clientId) {
const { id, ...payload } = convertQueryToServer(query);

export function updateSavedQuery(query) {
return dispatch =>
SupersetClient.put({
endpoint: `/api/v1/saved_query/${query.remoteId}`,
jsonPayload: convertQueryToServer(payload),
endpoint: `/savedqueryviewapi/api/update/${query.remoteId}`,
postPayload: convertQueryToServer(query),
stringify: false,
})
.then(() => {
dispatch(addSuccessToast(t('Your query was updated')));
dispatch(queryEditorSetTitle(query, query.name, clientId));
dispatch(queryEditorSetTitle(query, query.name));
})
.catch(e => {
const message = t('Your query could not be updated');
Expand Down Expand Up @@ -1362,12 +1350,11 @@ export function popStoredQuery(urlId) {
export function popSavedQuery(saveQueryId) {
return function (dispatch) {
return SupersetClient.get({
endpoint: `/api/v1/saved_query/${saveQueryId}`,
endpoint: `/savedqueryviewapi/api/get/${saveQueryId}`,
})
.then(({ json }) => {
const queryEditorProps = {
...convertQueryToClient(json.result),
dbId: json.result?.database?.id,
loaded: true,
autorun: false,
};
Expand Down
30 changes: 10 additions & 20 deletions superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ import thunk from 'redux-thunk';
import shortid from 'shortid';
import * as featureFlags from 'src/featureFlags';
import * as actions from 'src/SqlLab/actions/sqlLab';
import {
defaultQueryEditor,
query,
initialState,
queryId,
} from 'src/SqlLab/fixtures';
import { defaultQueryEditor, query, initialState } from 'src/SqlLab/fixtures';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
Expand Down Expand Up @@ -64,32 +59,30 @@ describe('async actions', () => {
fetchMock.post(runQueryEndpoint, `{ "data": ${mockBigNumber} }`);

describe('saveQuery', () => {
const saveQueryEndpoint = 'glob:*/api/v1/saved_query/';
const saveQueryEndpoint = 'glob:*/savedqueryviewapi/api/create';
fetchMock.post(saveQueryEndpoint, { results: { json: {} } });

const makeRequest = () => {
const request = actions.saveQuery(query, queryId);
const request = actions.saveQuery(query);
return request(dispatch, () => initialState);
};

it('posts to the correct url', () => {
expect.assertions(1);

const store = mockStore(initialState);
return store.dispatch(actions.saveQuery(query, queryId)).then(() => {
return store.dispatch(actions.saveQuery(query)).then(() => {
expect(fetchMock.calls(saveQueryEndpoint)).toHaveLength(1);
});
});

it('posts the correct query object', () => {
const store = mockStore(initialState);
return store.dispatch(actions.saveQuery(query, queryId)).then(() => {
return store.dispatch(actions.saveQuery(query)).then(() => {
const call = fetchMock.calls(saveQueryEndpoint)[0];
const formData = JSON.parse(call[1].body);
const mappedQueryToServer = actions.convertQueryToServer(query);

Object.keys(mappedQueryToServer).forEach(key => {
expect(formData[key]).toBeDefined();
const formData = call[1].body;
Object.keys(query).forEach(key => {
expect(formData.get(key)).toBeDefined();
});
});
});
Expand Down Expand Up @@ -377,13 +370,12 @@ describe('async actions', () => {
queryEditor: {
name: 'Copy of Dummy query editor',
dbId: 1,
schema: query.schema,
schema: null,
autorun: true,
sql: 'SELECT * FROM something',
queryLimit: undefined,
maxRow: undefined,
id: 'abcd',
templateParams: undefined,
},
},
];
Expand Down Expand Up @@ -643,9 +635,7 @@ describe('async actions', () => {
},
];
return store
.dispatch(
actions.queryEditorSetTitle(queryEditor, name, queryEditor.id),
)
.dispatch(actions.queryEditorSetTitle(queryEditor, name))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
expect(fetchMock.calls(updateTabStateEndpoint)).toHaveLength(1);
Expand Down
30 changes: 19 additions & 11 deletions superset-frontend/src/SqlLab/components/SaveQuery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { QueryEditor } from 'src/SqlLab/types';
interface SaveQueryProps {
queryEditorId: string;
columns: ISaveableDatasource['columns'];
onSave: (arg0: QueryPayload, id: string) => void;
onUpdate: (arg0: QueryPayload, id: string) => void;
onSave: (arg0: QueryPayload) => void;
onUpdate: (arg0: QueryPayload) => void;
saveQueryWarning: string | null;
database: Record<string, any>;
}
Expand All @@ -46,8 +46,19 @@ type QueryPayload = {
name: string;
description?: string;
id?: string;
remoteId?: number;
} & Pick<QueryEditor, 'dbId' | 'schema' | 'sql'>;
} & Pick<
QueryEditor,
| 'autorun'
| 'dbId'
| 'schema'
| 'sql'
| 'selectedText'
| 'remoteId'
| 'latestQueryId'
| 'queryLimit'
| 'tableOptions'
| 'schemaOptions'
>;

const Styles = styled.span`
span[role='img'] {
Expand Down Expand Up @@ -82,11 +93,11 @@ export default function SaveQuery({
'selectedText',
'sql',
'tableOptions',
'templateParams',
]);
const query = useMemo(
() => ({
...queryEditor,
columns,
}),
[queryEditor, columns],
);
Expand All @@ -109,13 +120,10 @@ export default function SaveQuery({
);

const queryPayload = () => ({
...query,
name: label,
description,
dbId: query.dbId ?? 0,
sql: query.sql,
schema: query.schema,
templateParams: query.templateParams,
remoteId: query?.remoteId || undefined,
});

useEffect(() => {
Expand All @@ -125,12 +133,12 @@ export default function SaveQuery({
const close = () => setShowSave(false);

const onSaveWrapper = () => {
onSave(queryPayload(), query.id);
onSave(queryPayload());
close();
};

const onUpdateWrapper = () => {
onUpdate(queryPayload(), query.id);
onUpdate(queryPayload());
close();
};

Expand Down
8 changes: 3 additions & 5 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ const SqlEditor = ({
);
};

const onSaveQuery = async (query, clientId) => {
const savedQuery = await dispatch(saveQuery(query, clientId));
const onSaveQuery = async query => {
const savedQuery = await dispatch(saveQuery(query));
dispatch(addSavedQueryToTabState(queryEditor, savedQuery));
};

Expand Down Expand Up @@ -580,9 +580,7 @@ const SqlEditor = ({
queryEditorId={queryEditor.id}
columns={latestQuery?.results?.columns || []}
onSave={onSaveQuery}
onUpdate={(query, remoteId, id) =>
dispatch(updateSavedQuery(query, remoteId, id))
}
onUpdate={query => dispatch(updateSavedQuery(query))}
saveQueryWarning={saveQueryWarning}
database={database}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const SqlEditorTabHeader: React.FC<Props> = ({ queryEditor }) => {
function renameTab() {
const newTitle = prompt(t('Enter a new title for the tab'));
if (newTitle) {
actions.queryEditorSetTitle(qe, newTitle, qe.id);
actions.queryEditorSetTitle(qe, newTitle);
}
}

Expand Down
12 changes: 7 additions & 5 deletions superset-frontend/src/SqlLab/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,15 +682,17 @@ export const initialState = {
};

export const query = {
name: 'test query',
id: 'clientId2353',
dbId: 1,
sql: 'SELECT * FROM something',
description: 'test description',
schema: 'test schema',
sqlEditorId: defaultQueryEditor.id,
tab: 'unimportant',
tempTable: null,
runAsync: false,
ctas: false,
cached: false,
};

export const queryId = 'clientId2353';

export const testQuery: ISaveableDatasource = {
name: 'unimportant',
dbId: 1,
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/SqlLab/reducers/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export default function sqlLabReducer(state = {}, action) {
return addToArr(newState, 'queryEditors', action.queryEditor);
},
[actions.QUERY_EDITOR_SAVED]() {
const { query, result, clientId } = action;
const existing = state.queryEditors.find(qe => qe.id === clientId);
const { query, result } = action;
const existing = state.queryEditors.find(qe => qe.id === query.id);
return alterInArr(
state,
'queryEditors',
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion superset/models/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ class SavedQuery(Model, AuditMixinNullable, ExtraJSONMixin, ImportExportMixin):
label = Column(String(256))
description = Column(Text)
sql = Column(Text)
template_parameters = Column(Text)
user = relationship(
security_manager.user_model,
backref=backref("saved_queries", cascade="all, delete-orphan"),
Expand Down
Loading

0 comments on commit 8413327

Please sign in to comment.