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:28:28 UTC

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

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



##########
File path: superset/migrations/shared/migrate_viz.py
##########
@@ -0,0 +1,93 @@
+# 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.
+import json
+from typing import Any, Dict, Optional, Set
+
+
+class MigrateViz:
+    remove_keys: Set[str] = set()
+
+    mapping_keys: Dict[str, str]
+
+    viz_type: str
+
+    def __init__(self, v1_data: str) -> None:
+        self.raw_data = v1_data
+        self.data = json.loads(v1_data)
+
+    def post_processing(self, data: Dict[str, Any]) -> Dict[str, Any]:
+        raise NotImplementedError()
+
+    def migrate(self) -> str:

Review comment:
       It would probably be a good idea to support up and down migrations in this class

##########
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:
       Why is this needed, is it to make it possible to do a down migration? I don't think we need to write these to disk if the migrator would work in both directions.




-- 
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