ktmud commented on code in PR #20473:
URL: https://github.com/apache/superset/pull/20473#discussion_r905288941
##########
superset/datasets/dao.py:
##########
@@ -222,21 +223,28 @@ def update_metrics(
- If there are extra metrics on the metadata db that are not defined
on the List
then we delete.
"""
- new_metrics = []
- 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)
+
+ metric_by_id = {metric.id: metric for metric in model.metrics}
+ metrics = []
+
+ for properties in property_metrics:
+ if "id" in properties:
+ metrics.append(
+ DatasetDAO.update_metric(
+ metric_by_id[properties["id"]],
+ properties,
+ commit=False,
+ )
+ )
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.id not in [metric.id for metric in new_metrics]:
- DatasetDAO.delete_metric(existing_metric)
- return new_metrics
+ # Note for new metrics the primary key is undefined sans a
commit/flush.
+ metrics.append(DatasetDAO.create_metric(properties,
commit=False))
+
+ for id_ in {obj.id for obj in model.metrics} - {obj.id for obj in
metrics}:
+ DatasetDAO.delete_column(metric_by_id[id_], commit=False)
Review Comment:
Wouldn't SQLA automatically delete orphaned metrics once they are not
associated with a dataset?
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]