code: def upgrade():
      bind = op.get_bind()
      session = db.Session(bind=bind)
  
      slices = (
          session.query(Slice)
          .filter(Slice.viz_type == "pivot_table_v2")
          .all()
      )
      for slc in slices:
          try:
              params = json.loads(slc.params)
              legacy_order_by = params.pop("legacy_order_by", None)
              if legacy_order_by:
                  params["series_limit_metric"] = legacy_order_by
              slc.params = json.dumps(params, sort_keys=True)
          except Exception as e:
              logger.exception(
                  f"An error occurred: parsing params for slice {slc.id} failed."
                  f"You need to fix it before upgrading your DB."
              )
              raise e
  
      session.commit()
      session.close()
  
file_path: superset/superset/migrations/versions/2021-12-17_16-56_31bb738bd1d2_move_pivot_table_v2_legacy_order_by_to_.py
imports: import json
  import logging
  from alembic import op
  from sqlalchemy import Column, Integer, String, Text
  from sqlalchemy.ext.declarative import declarative_base
  from superset import db
label_desc: Use image node_id 1 for calling a global function with example usage: upgrade() without return types