bito-code-review[bot] commented on code in PR #44362: URL: https://github.com/apache/superset/pull/44362#discussion_r4042068942
########## superset/migrations/versions/2026-09-16_00-01_00fab727cd0a_add_theme_editors_theme_id_index.py: ########## @@ -0,0 +1,45 @@ +# 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 theme_editors.theme_id index + +Revision ID: 00fab727cd0a +Revises: 60f94cd6cd11 +Create Date: 2026-09-16 00:01:00.000000 + +""" + +from superset.migrations.shared.utils import create_index, drop_index + +# revision identifiers, used by Alembic. +revision = "00fab727cd0a" +down_revision = "60f94cd6cd11" + +TABLE_NAME = "theme_editors" +INDEX_NAME = "ix_theme_editors_theme_id" + + +def upgrade() -> None: + # theme_editors' only index is the UNIQUE(subject_id, theme_id) constraint + # from its creation (#42404), which leads with subject_id -- most engines + # can't use a composite index to serve a lookup filtered on its trailing + # column alone. Loading a theme's editor list filters on theme_id only, + # so that's a table scan on every load without a dedicated index. + create_index(TABLE_NAME, INDEX_NAME, ["theme_id"]) + + +def downgrade() -> None: + drop_index(TABLE_NAME, INDEX_NAME) Review Comment: <!-- Bito Reply --> The suggestion to add docstrings to the `upgrade` and `downgrade` functions is appropriate. Adding one-line docstrings improves the discoverability of the migration's intent through documentation tools and aligns with the organization's standards for new Python functions. **superset/migrations/versions/2026-09-16_00-01_00fab727cd0a_add_theme_editors_theme_id_index.py** ``` def upgrade() -> None: """Add index to theme_editors.theme_id.""" # theme_editors' only index is the UNIQUE(subject_id, theme_id) constraint # from its creation (#42404), which leads with subject_id -- most engines # can't use a composite index to serve a lookup filtered on its trailing # column alone. Loading a theme's editor list filters on theme_id only, # so that's a table scan on every load without a dedicated index. create_index(TABLE_NAME, INDEX_NAME, ["theme_id"]) def downgrade() -> None: """Remove index from theme_editors.theme_id.""" drop_index(TABLE_NAME, INDEX_NAME) ``` ########## superset/security/manager.py: ########## @@ -201,6 +201,26 @@ def get_extra_editors_by_pk( } +def attach_extra_editors(result: dict[str, Any], resource: Model) -> None: + """ + Attach ``extra_editors`` to a single-object API response, if configured. + """ + if has_app_context() and current_app.config.get("EXTRA_EDITORS_RESOLVER"): + result["extra_editors"] = get_extra_editor_subject_ids(resource) + + +def attach_extra_editors_to_rows(data: dict[str, Any], model_cls: type[Model]) -> None: + """ + Attach ``extra_editors`` to each row of a list API response, matching + ``attach_extra_editors``'s single-object behavior. + """ + ids = data.get("ids", []) + extra_editors_by_id = get_extra_editors_by_pk(model_cls, ids) + for row, row_id in zip(data.get("result", []), ids, strict=False): + if row_id in extra_editors_by_id: + row["extra_editors"] = extra_editors_by_id[row_id] Review Comment: <!-- Bito Reply --> The reviewer's concern regarding a potential positional mismatch is valid. While the `ids` and `result` lists are often aligned in standard Flask-AppBuilder (FAB) list responses, relying on `zip` assumes that the order of `data['result']` and `data['ids']` is guaranteed to be identical and that no rows are omitted or reordered between the two. If the underlying database query or the API response construction changes, this assumption could lead to incorrect data being attached to rows. To address this, it is safer to map the `extra_editors` by the primary key of each row directly, rather than relying on positional alignment. You can modify the loop to look up the `extra_editors` using the row's own primary key (e.g., `row.get('id')` or the appropriate PK field) instead of the `row_id` from the `ids` list. **superset/security/manager.py** ``` ids = data.get("ids", []) extra_editors_by_id = get_extra_editors_by_pk(model_cls, ids) for row in data.get("result", []): row_id = row.get("id") if row_id in extra_editors_by_id: row["extra_editors"] = extra_editors_by_id[row_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. 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]
