code: def upgrade():
      bind = op.get_bind()
      session = db.Session(bind=bind)
  
      # find dup records in dashboard_slices tbl
      dup_records = (
          session.query(
              DashboardSlices.dashboard_id,
              DashboardSlices.slice_id,
              db.func.count(DashboardSlices.id),
          )
          .group_by(
              DashboardSlices.dashboard_id, DashboardSlices.slice_id
          )
          .having(db.func.count(DashboardSlices.id) > 1)
          .all()
      )
  
      # remove dup entries
      for record in dup_records:
          print(
              "remove duplicates from dashboard {} slice {}".format(
                  record.dashboard_id, record.slice_id
              )
          )
  
          ids = [
              item.id
              for item in session.query(DashboardSlices.id)
              .filter(
                  and_(
                      DashboardSlices.slice_id == record.slice_id,
                      DashboardSlices.dashboard_id
                      == record.dashboard_id,
                  )
              )
              .offset(1)
          ]
          session.query(DashboardSlices).filter(
              DashboardSlices.id.in_(ids)
          ).delete(synchronize_session=False)
  
      # add unique constraint
      try:
          with op.batch_alter_table("dashboard_slices") as batch_op:
              batch_op.create_unique_constraint(
                  "uq_dashboard_slice", ["dashboard_id", "slice_id"]
              )
      except Exception as ex:
          logging.exception(ex)
  
file_path: superset/superset/migrations/versions/2019-07-15_12-00_190188938582_adding_unique_constraint_on_dashboard_slices_tbl.py
imports: import logging
  from alembic import op
  from sqlalchemy import and_, Column, ForeignKey, Integer, Table
  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