etr2460 commented on a change in pull request #10590:
URL:
https://github.com/apache/incubator-superset/pull/10590#discussion_r469620237
##########
File path: superset/migrations/versions/f80a3b88324b_.py
##########
@@ -0,0 +1,38 @@
+# Licensed to the Apache Software Foundation (ASF) under one
Review comment:
Looks like this can be removed? I assume it was a placeholder for the
migration I just ran
##########
File path:
superset/migrations/versions/978245563a02_migrate_iframe_to_dash_markdown.py
##########
@@ -0,0 +1,170 @@
+# 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.
+"""Migrate iframe in dashboard to markdown component
+
+Revision ID: 978245563a02
+Revises: f2672aa8350a
+Create Date: 2020-08-12 00:24:39.617899
+
+"""
+import collections
+import json
+import logging
+import uuid
+from collections import defaultdict
+
+from alembic import op
+from sqlalchemy import and_, Column, ForeignKey, Integer, String, Table, Text
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship
+
+from superset import db
+
+# revision identifiers, used by Alembic.
+revision = "978245563a02"
+down_revision = "f2672aa8350a"
+
+Base = declarative_base()
+
+
+class Slice(Base):
+ """Declarative class to do query in upgrade"""
+
+ __tablename__ = "slices"
+ id = Column(Integer, primary_key=True)
+ params = Column(Text)
+ viz_type = Column(String(250))
+
+
+dashboard_slices = Table(
+ "dashboard_slices",
+ Base.metadata,
+ Column("id", Integer, primary_key=True),
+ Column("dashboard_id", Integer, ForeignKey("dashboards.id")),
+ Column("slice_id", Integer, ForeignKey("slices.id")),
+)
+
+
+class Dashboard(Base):
+ __tablename__ = "dashboards"
+ id = Column(Integer, primary_key=True)
+ position_json = Column(Text)
+ slices = relationship("Slice", secondary=dashboard_slices,
backref="dashboards")
+
+
+def create_new_markdown_component(chart_position, url):
+ return {
+ "type": "MARKDOWN",
+ "id": "MARKDOWN-{}".format(uuid.uuid4().hex[:8]),
+ "children": [],
+ "parents": chart_position["parents"],
+ "meta": {
+ "width": chart_position["meta"]["width"],
+ "height": chart_position["meta"]["height"],
+ "code": f'<iframe src="{url}" width="100%"
height="100%"></iframe>',
+ },
+ }
+
+
+def upgrade():
+ bind = op.get_bind()
+ session = db.Session(bind=bind)
+
+ dash_to_migrate = defaultdict(list)
+ iframe_urls = defaultdict(list)
+
+ try:
+ # find iframe viz_type and its url
+ iframes = session.query(Slice).filter_by(viz_type="iframe").all()
+ iframe_ids = [slc.id for slc in iframes]
+
+ for iframe in iframes:
+ iframe_params = json.loads(iframe.params or "{}")
+ url = iframe_params.get("url")
+ iframe_urls[iframe.id] = url
+
+ # find iframe viz_type that used in dashboard
+ dash_slc = (
+ session.query(dashboard_slices)
+ .filter(dashboard_slices.c.slice_id.in_(iframe_ids))
+ .all()
+ )
+ for entry in dash_slc:
+ dash_to_migrate[entry.dashboard_id].append(entry.slice_id)
+ dashboard_ids = list(dash_to_migrate.keys())
+
+ # replace iframe in dashboard metadata
+ dashboards = (
+
session.query(Dashboard).filter(Dashboard.id.in_(dashboard_ids)).all()
+ )
+ for i, dashboard in enumerate(dashboards):
+ print(
+ f"scanning dashboard ({i + 1}/{len(dashboards)}) dashboard:
{dashboard.id} >>>>"
+ )
+
+ # remove iframe slices from dashboard
+ dashboard.slices = [
+ slc for slc in dashboard.slices if slc.id not in iframe_ids
+ ]
+
+ # find iframe chart position in metadata
+ # and replace it with markdown component
+ position_dict = json.loads(dashboard.position_json or "{}")
+ for key, value in position_dict.items():
Review comment:
nit: replace `value` with `chart_position` (or perhaps `position_value`
as it's more clear
##########
File path: superset/viz.py
##########
@@ -2040,25 +2040,6 @@ def get_data(self, df: pd.DataFrame) -> VizData:
return d
-class IFrameViz(BaseViz):
Review comment:
Do we need to remove markdown components from viz.py too?
##########
File path:
superset/migrations/versions/978245563a02_migrate_iframe_to_dash_markdown.py
##########
@@ -0,0 +1,170 @@
+# 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.
+"""Migrate iframe in dashboard to markdown component
+
+Revision ID: 978245563a02
+Revises: f2672aa8350a
+Create Date: 2020-08-12 00:24:39.617899
+
+"""
+import collections
+import json
+import logging
+import uuid
+from collections import defaultdict
+
+from alembic import op
+from sqlalchemy import and_, Column, ForeignKey, Integer, String, Table, Text
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship
+
+from superset import db
+
+# revision identifiers, used by Alembic.
+revision = "978245563a02"
+down_revision = "f2672aa8350a"
+
+Base = declarative_base()
+
+
+class Slice(Base):
+ """Declarative class to do query in upgrade"""
+
+ __tablename__ = "slices"
+ id = Column(Integer, primary_key=True)
+ params = Column(Text)
+ viz_type = Column(String(250))
+
+
+dashboard_slices = Table(
+ "dashboard_slices",
+ Base.metadata,
+ Column("id", Integer, primary_key=True),
+ Column("dashboard_id", Integer, ForeignKey("dashboards.id")),
+ Column("slice_id", Integer, ForeignKey("slices.id")),
+)
+
+
+class Dashboard(Base):
+ __tablename__ = "dashboards"
+ id = Column(Integer, primary_key=True)
+ position_json = Column(Text)
+ slices = relationship("Slice", secondary=dashboard_slices,
backref="dashboards")
+
+
+def create_new_markdown_component(chart_position, url):
+ return {
+ "type": "MARKDOWN",
+ "id": "MARKDOWN-{}".format(uuid.uuid4().hex[:8]),
+ "children": [],
+ "parents": chart_position["parents"],
+ "meta": {
+ "width": chart_position["meta"]["width"],
+ "height": chart_position["meta"]["height"],
+ "code": f'<iframe src="{url}" width="100%"
height="100%"></iframe>',
+ },
+ }
+
+
+def upgrade():
+ bind = op.get_bind()
+ session = db.Session(bind=bind)
+
+ dash_to_migrate = defaultdict(list)
+ iframe_urls = defaultdict(list)
+
+ try:
+ # find iframe viz_type and its url
+ iframes = session.query(Slice).filter_by(viz_type="iframe").all()
+ iframe_ids = [slc.id for slc in iframes]
+
+ for iframe in iframes:
+ iframe_params = json.loads(iframe.params or "{}")
+ url = iframe_params.get("url")
+ iframe_urls[iframe.id] = url
+
+ # find iframe viz_type that used in dashboard
+ dash_slc = (
+ session.query(dashboard_slices)
+ .filter(dashboard_slices.c.slice_id.in_(iframe_ids))
+ .all()
+ )
+ for entry in dash_slc:
+ dash_to_migrate[entry.dashboard_id].append(entry.slice_id)
+ dashboard_ids = list(dash_to_migrate.keys())
+
+ # replace iframe in dashboard metadata
+ dashboards = (
+
session.query(Dashboard).filter(Dashboard.id.in_(dashboard_ids)).all()
+ )
+ for i, dashboard in enumerate(dashboards):
Review comment:
we should probably add to UPDATING.md that this should be done during
off hours as it could break people's charts while they're editing. They might
save over the migrated dash and accidentally add an iframe chart back into it
##########
File path: superset-frontend/src/visualizations/presets/MainPreset.js
##########
@@ -29,9 +29,7 @@ import ForceDirectedChartPlugin from
'@superset-ui/legacy-plugin-chart-force-dir
import HeatmapChartPlugin from '@superset-ui/legacy-plugin-chart-heatmap';
import HistogramChartPlugin from '@superset-ui/legacy-plugin-chart-histogram';
import HorizonChartPlugin from '@superset-ui/legacy-plugin-chart-horizon';
-import IframeChartPlugin from '@superset-ui/legacy-plugin-chart-iframe';
Review comment:
oooo, we can delete these packages next! So much red code!
##########
File path:
superset/migrations/versions/978245563a02_migrate_iframe_to_dash_markdown.py
##########
@@ -0,0 +1,170 @@
+# 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.
+"""Migrate iframe in dashboard to markdown component
+
+Revision ID: 978245563a02
+Revises: f2672aa8350a
+Create Date: 2020-08-12 00:24:39.617899
+
+"""
+import collections
+import json
+import logging
+import uuid
+from collections import defaultdict
+
+from alembic import op
+from sqlalchemy import and_, Column, ForeignKey, Integer, String, Table, Text
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship
+
+from superset import db
+
+# revision identifiers, used by Alembic.
+revision = "978245563a02"
+down_revision = "f2672aa8350a"
+
+Base = declarative_base()
+
+
+class Slice(Base):
+ """Declarative class to do query in upgrade"""
+
+ __tablename__ = "slices"
+ id = Column(Integer, primary_key=True)
+ params = Column(Text)
+ viz_type = Column(String(250))
+
+
+dashboard_slices = Table(
+ "dashboard_slices",
+ Base.metadata,
+ Column("id", Integer, primary_key=True),
+ Column("dashboard_id", Integer, ForeignKey("dashboards.id")),
+ Column("slice_id", Integer, ForeignKey("slices.id")),
+)
+
+
+class Dashboard(Base):
+ __tablename__ = "dashboards"
+ id = Column(Integer, primary_key=True)
+ position_json = Column(Text)
+ slices = relationship("Slice", secondary=dashboard_slices,
backref="dashboards")
+
+
+def create_new_markdown_component(chart_position, url):
+ return {
+ "type": "MARKDOWN",
+ "id": "MARKDOWN-{}".format(uuid.uuid4().hex[:8]),
+ "children": [],
+ "parents": chart_position["parents"],
+ "meta": {
+ "width": chart_position["meta"]["width"],
+ "height": chart_position["meta"]["height"],
+ "code": f'<iframe src="{url}" width="100%"
height="100%"></iframe>',
+ },
+ }
+
+
+def upgrade():
+ bind = op.get_bind()
+ session = db.Session(bind=bind)
+
+ dash_to_migrate = defaultdict(list)
+ iframe_urls = defaultdict(list)
+
+ try:
+ # find iframe viz_type and its url
+ iframes = session.query(Slice).filter_by(viz_type="iframe").all()
+ iframe_ids = [slc.id for slc in iframes]
+
+ for iframe in iframes:
+ iframe_params = json.loads(iframe.params or "{}")
+ url = iframe_params.get("url")
+ iframe_urls[iframe.id] = url
+
+ # find iframe viz_type that used in dashboard
+ dash_slc = (
+ session.query(dashboard_slices)
+ .filter(dashboard_slices.c.slice_id.in_(iframe_ids))
+ .all()
+ )
+ for entry in dash_slc:
+ dash_to_migrate[entry.dashboard_id].append(entry.slice_id)
+ dashboard_ids = list(dash_to_migrate.keys())
+
+ # replace iframe in dashboard metadata
+ dashboards = (
+
session.query(Dashboard).filter(Dashboard.id.in_(dashboard_ids)).all()
+ )
+ for i, dashboard in enumerate(dashboards):
+ print(
+ f"scanning dashboard ({i + 1}/{len(dashboards)}) dashboard:
{dashboard.id} >>>>"
+ )
+
+ # remove iframe slices from dashboard
+ dashboard.slices = [
+ slc for slc in dashboard.slices if slc.id not in iframe_ids
+ ]
+
+ # find iframe chart position in metadata
+ # and replace it with markdown component
+ position_dict = json.loads(dashboard.position_json or "{}")
+ for key, value in position_dict.items():
+ if (
+ value
+ and isinstance(value, dict)
+ and value["type"] == "CHART"
+ and value["meta"]
+ and value["meta"]["chartId"] in iframe_ids
+ ):
+ iframe_id = value["meta"]["chartId"]
+ # make new markdown component
+ markdown = create_new_markdown_component(
+ value, iframe_urls[iframe_id]
+ )
+ position_dict.pop(key)
+ position_dict[markdown["id"]] = markdown
+
+ # add markdown to layout tree
+ parent_id = markdown["parents"][-1]
+ children = position_dict[parent_id]["children"]
+ children.remove(key)
+ children.append(markdown["id"])
+
+ dashboard.position_json = json.dumps(
+ position_dict,
+ indent=None,
+ separators=(",", ":"),
+ sort_keys=True,
+ )
+ session.merge(dashboard)
+
+ # remove iframe, separator and markup charts
+ session.query(Slice).filter(
+ Slice.viz_type.in_(["iframe", "separator", "markup"])
+ ).delete(synchronize_session=False)
+
+ except Exception as ex:
+ logging.exception(f"dashboard {dashboard.id} has error: {ex}")
+
+ session.commit()
+ session.close()
+
+
+def downgrade():
+ pass
Review comment:
I agree this is nearly impossible to do a downgrade for. Maybe add a
comment explaining why though?
##########
File path:
superset/migrations/versions/978245563a02_migrate_iframe_to_dash_markdown.py
##########
@@ -0,0 +1,170 @@
+# 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.
+"""Migrate iframe in dashboard to markdown component
+
+Revision ID: 978245563a02
+Revises: f2672aa8350a
+Create Date: 2020-08-12 00:24:39.617899
+
+"""
+import collections
+import json
+import logging
+import uuid
+from collections import defaultdict
+
+from alembic import op
+from sqlalchemy import and_, Column, ForeignKey, Integer, String, Table, Text
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship
+
+from superset import db
+
+# revision identifiers, used by Alembic.
+revision = "978245563a02"
+down_revision = "f2672aa8350a"
+
+Base = declarative_base()
+
+
+class Slice(Base):
+ """Declarative class to do query in upgrade"""
+
+ __tablename__ = "slices"
+ id = Column(Integer, primary_key=True)
+ params = Column(Text)
+ viz_type = Column(String(250))
+
+
+dashboard_slices = Table(
+ "dashboard_slices",
+ Base.metadata,
+ Column("id", Integer, primary_key=True),
+ Column("dashboard_id", Integer, ForeignKey("dashboards.id")),
+ Column("slice_id", Integer, ForeignKey("slices.id")),
+)
+
+
+class Dashboard(Base):
+ __tablename__ = "dashboards"
+ id = Column(Integer, primary_key=True)
+ position_json = Column(Text)
+ slices = relationship("Slice", secondary=dashboard_slices,
backref="dashboards")
+
+
+def create_new_markdown_component(chart_position, url):
+ return {
+ "type": "MARKDOWN",
+ "id": "MARKDOWN-{}".format(uuid.uuid4().hex[:8]),
+ "children": [],
+ "parents": chart_position["parents"],
+ "meta": {
+ "width": chart_position["meta"]["width"],
+ "height": chart_position["meta"]["height"],
+ "code": f'<iframe src="{url}" width="100%"
height="100%"></iframe>',
+ },
+ }
+
+
+def upgrade():
+ bind = op.get_bind()
+ session = db.Session(bind=bind)
+
+ dash_to_migrate = defaultdict(list)
+ iframe_urls = defaultdict(list)
+
+ try:
+ # find iframe viz_type and its url
+ iframes = session.query(Slice).filter_by(viz_type="iframe").all()
+ iframe_ids = [slc.id for slc in iframes]
+
+ for iframe in iframes:
+ iframe_params = json.loads(iframe.params or "{}")
+ url = iframe_params.get("url")
+ iframe_urls[iframe.id] = url
+
+ # find iframe viz_type that used in dashboard
+ dash_slc = (
+ session.query(dashboard_slices)
+ .filter(dashboard_slices.c.slice_id.in_(iframe_ids))
+ .all()
+ )
+ for entry in dash_slc:
+ dash_to_migrate[entry.dashboard_id].append(entry.slice_id)
+ dashboard_ids = list(dash_to_migrate.keys())
+
+ # replace iframe in dashboard metadata
+ dashboards = (
+
session.query(Dashboard).filter(Dashboard.id.in_(dashboard_ids)).all()
+ )
+ for i, dashboard in enumerate(dashboards):
+ print(
+ f"scanning dashboard ({i + 1}/{len(dashboards)}) dashboard:
{dashboard.id} >>>>"
+ )
+
+ # remove iframe slices from dashboard
+ dashboard.slices = [
+ slc for slc in dashboard.slices if slc.id not in iframe_ids
+ ]
+
+ # find iframe chart position in metadata
+ # and replace it with markdown component
+ position_dict = json.loads(dashboard.position_json or "{}")
+ for key, value in position_dict.items():
+ if (
+ value
+ and isinstance(value, dict)
+ and value["type"] == "CHART"
+ and value["meta"]
+ and value["meta"]["chartId"] in iframe_ids
+ ):
+ iframe_id = value["meta"]["chartId"]
+ # make new markdown component
+ markdown = create_new_markdown_component(
+ value, iframe_urls[iframe_id]
+ )
+ position_dict.pop(key)
+ position_dict[markdown["id"]] = markdown
+
+ # add markdown to layout tree
+ parent_id = markdown["parents"][-1]
+ children = position_dict[parent_id]["children"]
+ children.remove(key)
+ children.append(markdown["id"])
+
+ dashboard.position_json = json.dumps(
+ position_dict,
+ indent=None,
+ separators=(",", ":"),
+ sort_keys=True,
+ )
+ session.merge(dashboard)
+
+ # remove iframe, separator and markup charts
+ session.query(Slice).filter(
+ Slice.viz_type.in_(["iframe", "separator", "markup"])
+ ).delete(synchronize_session=False)
+
+ except Exception as ex:
+ logging.exception(f"dashboard {dashboard.id} has error: {ex}")
Review comment:
What state would we end in if only part of the iframes got deleted? I
assume the charts would break because we're removing the backend api. Maybe
it's not an issue though if the chance of an exception here is quite small
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]