superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnbod...@apache.org
Subject [incubator-superset] 22/43: added migration script WIP
Date Fri, 11 Jan 2019 02:22:26 GMT
This is an automated email from the ASF dual-hosted git repository.

johnbodley pushed a commit to branch feature--embeddable-charts-pilot
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git

commit f4385e5019e120f9820e342d41765819875c3134
Author: Conglei Shi <conglei.shi@airbnb.com>
AuthorDate: Wed Dec 12 11:37:40 2018 -0800

    added migration script WIP
---
 .../versions/2e78904069a1_unify_metric_format.py   | 69 ++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/superset/migrations/versions/2e78904069a1_unify_metric_format.py b/superset/migrations/versions/2e78904069a1_unify_metric_format.py
new file mode 100644
index 0000000..93c9bb6
--- /dev/null
+++ b/superset/migrations/versions/2e78904069a1_unify_metric_format.py
@@ -0,0 +1,69 @@
+"""unify metric format
+
+Revision ID: 2e78904069a1
+Revises: 55e910a74826
+Create Date: 2018-12-11 11:08:43.574789
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '2e78904069a1'
+down_revision = '55e910a74826'
+
+import json
+
+from alembic import op
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy import Column, Integer, String, Text
+
+from superset import db
+from superset.legacy import cast_form_data
+
+METRIC_KEYS = [
+    'metric', 'metrics', 'percent_metrics', 'metric_2', 'secondary_metric',
+    'x', 'y', 'size',
+]
+
+Base = declarative_base()
+
+
+class Slice(Base):
+    """Declarative class to do query in upgrade"""
+    __tablename__ = 'slices'
+    id = Column(Integer, primary_key=True)
+    datasource_type = Column(String(200))
+    slice_name = Column(String(200))
+    params = Column(Text)
+
+
+def upgrade():
+    bind = op.get_bind()
+    session = db.Session(bind=bind)
+
+    slices = session.query(Slice).all()
+    slice_len = len(slices)
+    for i, slc in enumerate(slices):
+        try:
+            d = json.loads(slc.params or '{}')
+            for mkey in METRIC_KEYS:
+              val = fd.get(mkey)
+              if val:
+                  if not isinstance(val, list):
+                      val = [val]
+                  for o in val:
+                      label = self.get_metric_label(o)
+                      if isinstance(o, dict):
+                          o['label'] = label
+                      self.metric_dict[label] = o
+            slc.params = json.dumps(d, indent=2, sort_keys=True)
+            session.merge(slc)
+            session.commit()
+            print('Upgraded ({}/{}): {}'.format(i, slice_len, slc.slice_name))
+        except Exception as e:
+            print(slc.slice_name + ' error: ' + str(e))
+
+    session.close()
+
+
+def downgrade():
+    pass
\ No newline at end of file


Mime
View raw message