willbarrett commented on a change in pull request #13389:
URL: https://github.com/apache/superset/pull/13389#discussion_r586816802
##########
File path: superset/datasets/dao.py
##########
@@ -186,9 +169,77 @@ def update( # pylint: disable=W:279
super().update(model, properties, commit=commit)
properties["columns"] = original_properties
- super().update(model, properties, commit=False)
+ updated_model = super().update(model, properties, commit=False)
model.health_check(force=True, commit=False)
- return super().update(model, properties, commit=commit)
+ return updated_model
+
+ @classmethod
+ def update_columns(
+ cls,
+ model: SqlaTable,
+ property_columns: List[Dict[str, Any]],
+ commit: bool = True,
+ ) -> List[TableColumn]:
+ """
+ Creates/updates and/or deletes a list of columns, based on a
+ list of Dict.
+
+ - If a column Dict has an `id` property then we update.
+ - If a column Dict does not have an `id` then we create a new metric.
+ - If there are extra columns on the metadata db that are not defined
on the List
+ then we delete.
+ """
+ new_columns = []
+ for column in property_columns:
+ column_id = column.get("id")
+
+ if column_id:
+ column_obj = db.session.query(TableColumn).get(column_id)
+ column_obj = DatasetDAO.update_column(column_obj, column,
commit=commit)
+ else:
+ column_obj = DatasetDAO.create_column(column, commit=commit)
+ new_columns.append(column_obj)
+ # Checks if an exiting column is missing from properties and delete it
+ for existing_column in model.columns:
+ if existing_column.column_name not in [
+ column["column_name"] for column in property_columns
+ ]:
+ DatasetDAO.delete_column(existing_column)
+ return new_columns
+
+ @classmethod
+ def update_metrics(
+ cls,
+ model: SqlaTable,
+ property_metrics: List[Dict[str, Any]],
+ commit: bool = True,
+ ) -> List[SqlMetric]:
+ """
+ Creates/updates and/or deletes a list of metrics, based on a
+ list of Dict.
+
+ - If a metric Dict has an `id` property then we update.
+ - If a metric Dict does not have an `id` then we create a new metric.
+ - If there are extra metrics on the metadata db that are not defined
on the List
+ then we delete.
+ """
+ new_metrics = list()
+ for metric in property_metrics:
+ metric_id = metric.get("id")
+ if metric.get("id"):
+ metric_obj = db.session.query(SqlMetric).get(metric_id)
+ metric_obj = DatasetDAO.update_metric(metric_obj, metric,
commit=commit)
+ else:
+ metric_obj = DatasetDAO.create_metric(metric, commit=commit)
+ new_metrics.append(metric_obj)
+
+ # Checks if an exiting column is missing from properties and delete it
+ for existing_metric in model.metrics:
+ if existing_metric.metric_name not in [
+ metric["metric_name"] for metric in property_metrics
Review comment:
Same question here - I would expect us to use `id`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]