fitzee commented on code in PR #41591: URL: https://github.com/apache/superset/pull/41591#discussion_r3582559160
########## superset/migrations/versions/2026-06-30_00-00_d24e6b0a9c7f_strip_metricsqlexpressions_from_ag_grid_params.py: ########## @@ -0,0 +1,108 @@ +# 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. +"""strip metricSqlExpressions from ag_grid_table params + +Before PR #41555 the AG Grid table plugin leaked ``metricSqlExpressions`` +(a mapping of every datasource metric/column name to its SQL expression) into +``extra_form_data`` on every filter interaction, which was then serialised into +the ``params`` and ``query_context`` columns on save. For datasources with +many metrics this bloated each chart record by hundreds of MB. + +This migration strips the field from existing rows so those records are no +longer loaded eagerly on every dashboard request. + +Revision ID: d24e6b0a9c7f +Revises: 2bee73611e32 +Create Date: 2026-06-30 00:00:00.000000 + +""" + +from alembic import op +from sqlalchemy import Column, Integer, String, Text +from sqlalchemy.ext.declarative import declarative_base + +from superset import db +from superset.utils import json + +# revision identifiers, used by Alembic. +revision = "d24e6b0a9c7f" +down_revision = "2bee73611e32" + +Base = declarative_base() + +_FIELD = "metricSqlExpressions" +_VIZ_TYPE = "ag_grid_table" + + +class Slice(Base): + __tablename__ = "slices" + id = Column(Integer, primary_key=True) + viz_type = Column(String(250)) + params = Column(Text) + query_context = Column(Text) + + +def _strip_params(slc: Slice) -> bool: + """Remove _FIELD from extra_form_data in params. Returns True if changed.""" + if not slc.params: + return False + try: + params = json.loads(slc.params) + except Exception: + return False + + extra = params.get("extra_form_data", {}) + if _FIELD not in extra: + return False + + del extra[_FIELD] + params["extra_form_data"] = extra + slc.params = json.dumps(params) + return True + + +def _strip_query_context(slc: Slice) -> bool: + """Remove _FIELD from query_context.form_data.extra_form_data.""" + if not slc.query_context: + return False + try: + qc = json.loads(slc.query_context) + except Exception: + return False + + extra = qc.get("form_data", {}).get("extra_form_data", {}) + if _FIELD not in extra: + return False + + del extra[_FIELD] + slc.query_context = json.dumps(qc) + return True + + +def upgrade() -> None: + bind = op.get_bind() + session = db.Session(bind=bind) + + for slc in session.query(Slice).filter(Slice.viz_type == _VIZ_TYPE): + _strip_params(slc) + _strip_query_context(slc) Review Comment: Stale — the current code uses `session.flush()` (not `session.commit()`) at the end of `upgrade()`, added in response to Joe's review. Calling `session.commit()` inside a migration would break Alembic's transaction management and prevent rollback; `flush()` is the correct pattern here — it sends the pending SQL UPDATEs within the outer transaction that Alembic owns. ########## superset/migrations/versions/2026-06-30_00-00_d24e6b0a9c7f_strip_metricsqlexpressions_from_ag_grid_params.py: ########## @@ -0,0 +1,108 @@ +# 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. +"""strip metricSqlExpressions from ag_grid_table params + +Before PR #41555 the AG Grid table plugin leaked ``metricSqlExpressions`` +(a mapping of every datasource metric/column name to its SQL expression) into +``extra_form_data`` on every filter interaction, which was then serialised into +the ``params`` and ``query_context`` columns on save. For datasources with +many metrics this bloated each chart record by hundreds of MB. + +This migration strips the field from existing rows so those records are no +longer loaded eagerly on every dashboard request. + +Revision ID: d24e6b0a9c7f +Revises: 2bee73611e32 +Create Date: 2026-06-30 00:00:00.000000 + +""" + +from alembic import op +from sqlalchemy import Column, Integer, String, Text +from sqlalchemy.ext.declarative import declarative_base + +from superset import db +from superset.utils import json + +# revision identifiers, used by Alembic. +revision = "d24e6b0a9c7f" +down_revision = "2bee73611e32" + +Base = declarative_base() + +_FIELD = "metricSqlExpressions" +_VIZ_TYPE = "ag_grid_table" + + +class Slice(Base): + __tablename__ = "slices" + id = Column(Integer, primary_key=True) + viz_type = Column(String(250)) + params = Column(Text) + query_context = Column(Text) + + +def _strip_params(slc: Slice) -> bool: + """Remove _FIELD from extra_form_data in params. Returns True if changed.""" + if not slc.params: + return False + try: + params = json.loads(slc.params) + except Exception: + return False + + extra = params.get("extra_form_data", {}) + if _FIELD not in extra: + return False + + del extra[_FIELD] + params["extra_form_data"] = extra + slc.params = json.dumps(params) + return True + + +def _strip_query_context(slc: Slice) -> bool: + """Remove _FIELD from query_context.form_data.extra_form_data.""" + if not slc.query_context: + return False + try: + qc = json.loads(slc.query_context) + except Exception: + return False + + extra = qc.get("form_data", {}).get("extra_form_data", {}) + if _FIELD not in extra: + return False + + del extra[_FIELD] + slc.query_context = json.dumps(qc) + return True + + +def upgrade() -> None: + bind = op.get_bind() + session = db.Session(bind=bind) + + for slc in session.query(Slice).filter(Slice.viz_type == _VIZ_TYPE): + _strip_params(slc) + _strip_query_context(slc) Review Comment: Stale — the bot scanned an earlier snapshot. The current code has `session.flush()` at the end of `upgrade()`, added in response to Joe's review. Using `session.commit()` here would be incorrect: it would commit mid-migration and break Alembic's ability to roll back if a later step fails. `session.flush()` is the right pattern — it sends the pending SQL UPDATEs inside the outer transaction that Alembic owns and commits. -- 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]
