john-bodley commented on code in PR #20473:
URL: https://github.com/apache/superset/pull/20473#discussion_r904468603
##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,34 @@ def update_columns(
- 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)
+ column_by_id = {column.id: column for column in model.columns}
+ columns = []
+
+ for properties in property_columns:
+ if "id" in properties:
+ columns.append(
+ DatasetDAO.update_column(
+ column_by_id[properties["id"]],
+ properties,
+ commit=False,
+ )
+ )
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.id not in [column.id for column in new_columns]:
- DatasetDAO.delete_column(existing_column)
- return new_columns
+
+ # Note for new columns the primary key is undefined sans a
commit/flush.
+ columns.append(DatasetDAO.create_column(properties,
commit=False))
+
+ for id_ in {obj.id for obj in model.columns} - {obj.id for obj in
columns}:
Review Comment:
Optimized looping.
##########
superset/datasets/dao.py:
##########
@@ -254,23 +262,24 @@ def find_dataset_column(
@classmethod
def update_column(
- cls, model: TableColumn, properties: Dict[str, Any], commit: bool =
True
- ) -> Optional[TableColumn]:
+ cls,
+ model: TableColumn,
+ properties: Dict[str, Any],
+ commit: bool = True,
+ ) -> TableColumn:
Review Comment:
The return is not `Optional[...]` per the `superset/dao/base.py` methods.
##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,34 @@ def update_columns(
- 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)
+ column_by_id = {column.id: column for column in model.columns}
+ columns = []
+
+ for properties in property_columns:
+ if "id" in properties:
+ columns.append(
+ DatasetDAO.update_column(
+ column_by_id[properties["id"]],
+ properties,
+ commit=False,
Review Comment:
Commit once for the dataset.
##########
superset/datasets/dao.py:
##########
@@ -287,24 +296,27 @@ def find_dataset_metric(
return db.session.query(SqlMetric).get(metric_id)
@classmethod
- def delete_metric(
- cls, model: SqlMetric, commit: bool = True
- ) -> Optional[TableColumn]:
Review Comment:
Should have been `SqlMetric` and not `TableColumn`.
--
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]