You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/07/29 09:44:15 UTC

[GitHub] [superset] zhaoyongjie commented on a change in pull request #15939: feat: viz migration script

zhaoyongjie commented on a change in pull request #15939:
URL: https://github.com/apache/superset/pull/15939#discussion_r678996882



##########
File path: superset/migrations/versions/9ed569cf4ba9_migrate_pivottable_from_v1_to_v2.py
##########
@@ -0,0 +1,101 @@
+# 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 pivottable from v1 to v2
+
+Revision ID: 9ed569cf4ba9
+Revises: ae1ed299413b
+Create Date: 2021-07-27 15:20:38.942341
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = "9ed569cf4ba9"
+down_revision = "ae1ed299413b"
+
+import os
+from pathlib import Path
+
+from alembic import op
+from sqlalchemy import Column, Integer, Text
+from sqlalchemy.exc import OperationalError
+from sqlalchemy.ext.declarative import declarative_base
+
+from superset import db
+from superset.config import DATA_DIR
+from superset.migrations.shared.migrate_viz import MigratePivotTable
+
+Base = declarative_base()
+
+
+class Slice(Base):
+    __tablename__ = "slices"
+
+    id = Column(Integer, primary_key=True)
+    slice_name = Column(Text)
+    viz_type = Column(Text)
+    params = Column(Text)
+    uuid = Column(Text)
+
+
+os.makedirs(os.path.join(DATA_DIR, "migrate_viz_pivottable"), exist_ok=True)
+
+
+def upgrade():
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
+    try:
+        slices = session.query(Slice).filter(Slice.viz_type == "pivot_table")
+        total = slices.count()
+        idx = 0
+        for slc in slices.yield_per(100):
+            idx += 1
+            print(f"Upgrading ({idx}/{total}): {slc.slice_name}#{slc.id}")
+            with open(
+                os.path.join(
+                    DATA_DIR, "migrate_viz_pivottable", f"{str(slc.uuid)}.json"
+                ),
+                "w",
+                encoding="utf-8",
+            ) as f:
+                f.write(slc.params)

Review comment:
       The main reason is that I can't find out which viz needs to do downgrade script.(The database might have a pivot table for both v1 and v2).
   
   If all pivot table v2 do downgrade, it will cause all pivot tables (both v1 and v2) to execute downgrade.
   




-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org