diff --git a/superset/migrations/versions/2023-03-27_12-30_7e67aecbf3f1_chart_ds_constraint.py b/superset/migrations/versions/2023-03-27_12-30_7e67aecbf3f1_chart_ds_constraint.py index f7d6ca5656e8a..8d3115acbdb91 100644 --- a/superset/migrations/versions/2023-03-27_12-30_7e67aecbf3f1_chart_ds_constraint.py +++ b/superset/migrations/versions/2023-03-27_12-30_7e67aecbf3f1_chart_ds_constraint.py @@ -27,6 +27,7 @@ down_revision = "07f9a902af1b" import json +import logging import sqlalchemy as sa from alembic import op @@ -36,6 +37,8 @@ Base = declarative_base() +logger = logging.getLogger(__name__) + class Slice(Base): # type: ignore __tablename__ = "slices" @@ -48,13 +51,33 @@ class Slice(Base): # type: ignore def upgrade_slc(slc: Slice) -> None: # clean up all charts with datasource_type not != table slc.datasource_type = "table" + ds_id = None + ds_type = None try: params_dict = json.loads(slc.params) ds_id, ds_type = params_dict["datasource"].split("__") + # the assumption here is that the query was saved as a dataset + # but the type wasn't written properly to the slice + # by updating the type here we expect it will either work + # or it will 404 when the dataset is looked up. params_dict["datasource"] = f"{ds_id}__table" slc.params = json.dumps(params_dict) + logger.warning( + "updated slice datasource from %s__%s to %s__table for slice: %s", + ds_id, + ds_type, + ds_id, + slc.id, + ) except Exception: # skip any malformatted params + logger.warning( + "failed to update slice.id = %s w/ datasource = %s__%s to %s__table", + slc.id, + ds_id, + ds_type, + ds_id, + ) pass @@ -63,9 +86,16 @@ def upgrade(): session = db.Session(bind=bind) with op.batch_alter_table("slices") as batch_op: - for slc in session.query(Slice).filter(Slice.datasource_type == "query").all(): - upgrade_slc(slc) - session.add(slc) + for slc in session.query(Slice).filter(Slice.datasource_type != "table").all(): + if slc.datasource_type == "query": + upgrade_slc(slc) + session.add(slc) + + else: + logger.warning( + "unknown value detected for slc.datasource_type: %s", + slc.datasource_type, + ) batch_op.create_check_constraint( "ck_chart_datasource", "datasource_type in ('table')"