Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate sqllab_viz endpoint to api v1 #23729

Merged
merged 14 commits into from
Apr 26, 2023
15 changes: 12 additions & 3 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ export function createDatasourceStarted() {
return { type: CREATE_DATASOURCE_STARTED };
}
export function createDatasourceSuccess(data) {
const datasource = `${data.table_id}__table`;
const datasource = `${data.id}__table`;
return { type: CREATE_DATASOURCE_SUCCESS, datasource };
}
export function createDatasourceFailed(err) {
Expand All @@ -1511,9 +1511,18 @@ export function createDatasourceFailed(err) {
export function createDatasource(vizOptions) {
return dispatch => {
dispatch(createDatasourceStarted());
const { dbId, schema, datasourceName, sql } = vizOptions;
return SupersetClient.post({
endpoint: '/superset/sqllab_viz/',
postPayload: { data: vizOptions },
endpoint: '/api/v1/dataset/',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
database: dbId,
schema,
sql,
table_name: datasourceName,
is_managed_externally: false,
external_url: null,
}),
})
.then(({ json }) => {
dispatch(createDatasourceSuccess(json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ export const SaveDatasetModal = ({
columns: selectedColumns,
}),
)
.then((data: { table_id: number }) =>
postFormData(data.table_id, 'table', {
.then((data: { id: number }) =>
postFormData(data.id, 'table', {
...formDataWithDefaults,
datasource: `${data.table_id}__table`,
datasource: `${data.id}__table`,
...(defaultVizType === 'table' && {
all_columns: selectedColumns.map(column => column.column_name),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const defaultDatasourcesReducerState = {
[CURRENT_DATASOURCE.uid]: CURRENT_DATASOURCE,
};

const saveDatasetEndpoint = `glob:*/superset/sqllab_viz/`;
const saveDatasetEndpoint = `glob:*/api/v1/dataset/`;

test('sets new datasource', () => {
const newState = datasourcesReducer(
Expand Down
42 changes: 20 additions & 22 deletions superset-frontend/src/explore/actions/datasourcesActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export function setDatasource(datasource: Dataset) {
return { type: SET_DATASOURCE, datasource };
}

export function changeDatasource(newDatasource: Dataset) {
return function (dispatch: Dispatch, getState: () => ExplorePageState) {
const {
explore: { datasource: prevDatasource },
} = getState();
dispatch(setDatasource(newDatasource));
dispatch(updateFormDataByDatasource(prevDatasource, newDatasource));
};
}

export function saveDataset({
schema,
sql,
Expand All @@ -49,18 +59,16 @@ export function saveDataset({
const {
json: { data },
} = await SupersetClient.post({
endpoint: '/superset/sqllab_viz/',
postPayload: {
data: {
schema,
sql,
dbId: database?.id,
templateParams,
datasourceName,
metrics: [],
columns,
},
},
endpoint: '/api/v1/dataset/',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
database: database?.id,
table_name: datasourceName,
schema,
sql,
template_params: templateParams,
columns,
}),
});
// Update form_data to point to new dataset
dispatch(changeDatasource(data));
Expand All @@ -74,16 +82,6 @@ export function saveDataset({
};
}

export function changeDatasource(newDatasource: Dataset) {
return function (dispatch: Dispatch, getState: () => ExplorePageState) {
const {
explore: { datasource: prevDatasource },
} = getState();
dispatch(setDatasource(newDatasource));
dispatch(updateFormDataByDatasource(prevDatasource, newDatasource));
};
}

export const datasourcesActions = {
setDatasource,
changeDatasource,
Expand Down
1 change: 1 addition & 0 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,7 @@ def sqllab_table_viz(self) -> FlaskResponse: # pylint: disable=no-self-use
@has_access
@expose("/sqllab_viz/", methods=["POST"])
@event_logger.log_this
@deprecated()
def sqllab_viz(self) -> FlaskResponse: # pylint: disable=no-self-use
data = json.loads(request.form["data"])
try:
Expand Down