bito-code-review[bot] commented on code in PR #43232: URL: https://github.com/apache/superset/pull/43232#discussion_r4149484104
########## superset/migrations/versions/2026-08-17_18-38_884a2115ebd3_add_uuid_to_annotation_layer_and_annotation.py: ########## @@ -0,0 +1,100 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""add_uuid_to_annotation_layer_and_annotation + +Revision ID: 884a2115ebd3 +Revises: 95d8a99c822e +Create Date: 2026-08-11 18:38:15.412396 + +""" + +from uuid import uuid4 + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy_utils import UUIDType + +from superset import db +from superset.migrations.shared.utils import ( + add_columns, + assign_uuids, + drop_columns, + has_table, +) + +# revision identifiers, used by Alembic. +revision = "884a2115ebd3" +down_revision = "95d8a99c822e" + +Base = declarative_base() + + +class ImportMixin: + id = sa.Column(sa.Integer, primary_key=True) + uuid = sa.Column(UUIDType(binary=True), primary_key=False, default=uuid4) + + +class AnnotationLayer(ImportMixin, Base): + __tablename__ = "annotation_layer" + + +class Annotation(ImportMixin, Base): + __tablename__ = "annotation" + + +MODELS = (AnnotationLayer, Annotation) + + +def _has_unique_constraint(table_name: str, constraint_name: str) -> bool: + inspector = sa.inspect(op.get_bind()) + return any( + constraint["name"] == constraint_name + for constraint in inspector.get_unique_constraints(table_name) + ) + + +def upgrade() -> None: + session = db.Session(bind=op.get_bind()) + + for model in MODELS: + table_name = model.__tablename__ + if not has_table(table_name): + continue + add_columns( + table_name, + sa.Column("uuid", UUIDType(binary=True), primary_key=False, default=uuid4), + ) + if session.query(model).filter(model.uuid.is_(None)).count(): + assign_uuids(model, session) Review Comment: <!-- Bito Reply --> The update to the migration script correctly addresses the concern about overwriting existing UUIDs. By checking `if session.query(model).filter(model.uuid.is_(None)).count():` before calling `assign_uuids`, the migration now ensures that only rows missing a UUID are updated, preserving any existing identifiers. **superset/migrations/versions/2026-08-17_18-38_884a2115ebd3_add_uuid_to_annotation_layer_and_annotation.py** ``` if session.query(model).filter(model.uuid.is_(None)).count(): assign_uuids(model, session) ``` ########## tests/unit_tests/charts/commands/importers/v1/utils_test.py: ########## @@ -281,3 +285,24 @@ def test_migrate_chart_table_migrates_when_flag_enabled( assert new_config["viz_type"] == "ag-grid-table" assert json.loads(new_config["params"])["viz_type"] == "ag-grid-table" + + +def test_get_dependency_chart_uuids_uses_selection_file() -> None: + """Charts missing from the selection file are dependencies.""" + configs = [{"uuid": "a"}, {"uuid": "b"}, {"uuid": "c"}] Review Comment: <!-- Bito Reply --> The reviewer's suggestion to add type hints to local variables is consistent with standard practices for improving code readability and maintainability, even when types are inferable. While the existing tests in the module may not currently follow this pattern, adopting these annotations helps ensure type safety and clarity for future maintenance. You may choose to apply these annotations to align with modern development standards. ########## superset/commands/dashboard/importers/v1/__init__.py: ########## @@ -119,6 +127,23 @@ def _import( if config.get("theme_uuid"): theme_uuids.add(config["theme_uuid"]) + # discover charts used as annotation sources by those charts, which can + # be in the bundle without being in any dashboard layout + chart_configs_by_uuid = { + str(config["uuid"]): config + for file_name, config in configs.items() + if file_name.startswith("charts/") + } + pending_chart_uuids = list(chart_uuids) Review Comment: <!-- Bito Reply --> The suggestion to add explicit type annotations to `chart_configs_by_uuid` and `pending_chart_uuids` is appropriate and aligns with the project's coding standards for new local variables. Applying these annotations improves code readability and consistency with existing declarations in the `_import` function. The suggested types `dict[str, dict[str, Any]]` and `list[str]` are correct for the usage shown in the diff. **superset/commands/dashboard/importers/v1/__init__.py** ``` # discover charts used as annotation sources by those charts, which can # be in the bundle without being in any dashboard layout chart_configs_by_uuid: dict[str, dict[str, Any]] = { str(config["uuid"]): config for file_name, config in configs.items() if file_name.startswith("charts/") } pending_chart_uuids: list[str] = list(chart_uuids) ``` ########## superset/commands/chart/export.py: ########## @@ -100,28 +190,53 @@ def enable_tag_export(cls) -> None: def run( self, seen: set[str] | None = None ) -> Iterator[tuple[str, Callable[[], str]]]: + is_root = seen is None yield from super().run(seen=seen) + if not self.export_related: + return + + chart_ids = ExportChartsCommand.chart_ids_with_annotation_sources(self._models) Review Comment: <!-- Bito Reply --> The suggestion to gate the computation of `chart_ids_with_annotation_sources` is appropriate. By moving this computation after the `if not self.export_related:` check, the code avoids unnecessary processing when the export does not require related objects, which improves efficiency on paths like dashboard exports where this result is not needed. **superset/commands/chart/export.py** ``` if not self.export_related: return chart_ids = ExportChartsCommand.chart_ids_with_annotation_sources(self._models) ``` -- 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]
