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
38 changes: 21 additions & 17 deletions superset/datasets/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class DatasetColumnsPutSchema(Schema):
column_name = fields.String(required=True, validate=Length(1, 255))
type = fields.String(allow_none=True)
advanced_data_type = fields.String(allow_none=True, validate=Length(1, 255))
verbose_name = fields.String(allow_none=True, Length=(1, 1024))
verbose_name = fields.String(allow_none=True, metadata={Length: (1, 1024)})
hughhhh marked this conversation as resolved.
Show resolved Hide resolved
description = fields.String(allow_none=True)
expression = fields.String(allow_none=True)
extra = fields.String(allow_none=True)
filterable = fields.Boolean()
groupby = fields.Boolean()
is_active = fields.Boolean()
is_dttm = fields.Boolean(default=False)
is_active = fields.Boolean(allow_none=True)
is_dttm = fields.Boolean(dump_default=False)
python_date_format = fields.String(
allow_none=True, validate=[Length(1, 255), validate_python_date_format]
)
Expand All @@ -71,7 +71,7 @@ class DatasetMetricsPutSchema(Schema):
metric_name = fields.String(required=True, validate=Length(1, 255))
metric_type = fields.String(allow_none=True, validate=Length(1, 32))
d3format = fields.String(allow_none=True, validate=Length(1, 128))
verbose_name = fields.String(allow_none=True, Length=(1, 1024))
verbose_name = fields.String(allow_none=True, metadata={Length: (1, 1024)})
hughhhh marked this conversation as resolved.
Show resolved Hide resolved
warning_text = fields.String(allow_none=True)
uuid = fields.UUID(allow_none=True)

Expand All @@ -82,7 +82,7 @@ class DatasetPostSchema(Schema):
table_name = fields.String(required=True, allow_none=False, validate=Length(1, 250))
sql = fields.String(allow_none=True)
owners = fields.List(fields.Integer())
is_managed_externally = fields.Boolean(allow_none=True, default=False)
is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
external_url = fields.String(allow_none=True)


Expand All @@ -104,7 +104,7 @@ class DatasetPutSchema(Schema):
columns = fields.List(fields.Nested(DatasetColumnsPutSchema))
metrics = fields.List(fields.Nested(DatasetMetricsPutSchema))
extra = fields.String(allow_none=True)
is_managed_externally = fields.Boolean(allow_none=True, default=False)
is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
external_url = fields.String(allow_none=True)


Expand All @@ -127,16 +127,18 @@ class DatasetRelatedDashboard(Schema):


class DatasetRelatedCharts(Schema):
count = fields.Integer(description="Chart count")
count = fields.Integer(metadata={"description": "Chart count"})
result = fields.List(
fields.Nested(DatasetRelatedChart), description="A list of dashboards"
fields.Nested(DatasetRelatedChart),
metadata={"description": "A list of dashboards"},
)


class DatasetRelatedDashboards(Schema):
count = fields.Integer(description="Dashboard count")
count = fields.Integer(metadata={"description": "Dashboard count"})
result = fields.List(
fields.Nested(DatasetRelatedDashboard), description="A list of dashboards"
fields.Nested(DatasetRelatedDashboard),
metadata={"description": "A list of dashboards"},
)


Expand All @@ -160,8 +162,8 @@ def fix_extra(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
column_name = fields.String(required=True)
extra = fields.Dict(allow_none=True)
verbose_name = fields.String(allow_none=True)
is_dttm = fields.Boolean(default=False, allow_none=True)
is_active = fields.Boolean(default=True, allow_none=True)
is_dttm = fields.Boolean(dump_default=False, allow_none=True)
is_active = fields.Boolean(dump_default=True, allow_none=True)
type = fields.String(allow_none=True)
advanced_data_type = fields.String(allow_none=True)
groupby = fields.Boolean()
Expand Down Expand Up @@ -224,19 +226,21 @@ def fix_extra(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
version = fields.String(required=True)
database_uuid = fields.UUID(required=True)
data = fields.URL()
is_managed_externally = fields.Boolean(allow_none=True, default=False)
is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
external_url = fields.String(allow_none=True)


class GetOrCreateDatasetSchema(Schema):
table_name = fields.String(required=True, description="Name of table")
table_name = fields.String(required=True, metadata={"description": "Name of table"})
database_id = fields.Integer(
required=True, description="ID of database table belongs to"
required=True, metadata={"description": "ID of database table belongs to"}
)
schema = fields.String(
description="The schema the table belongs to", allow_none=True
metadata={"description": "The schema the table belongs to"}, allow_none=True
)
template_params = fields.String(
metadata={"description": "Template params for the table"}
)
template_params = fields.String(description="Template params for the table")


class DatasetSchema(SQLAlchemyAutoSchema):
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