parameters: (apps, schema_editor)
docstring: Create a stack group entry for each stack group class instance and link
      stacks linked to respective class instance to new stack group.
docstring_summary: Create a stack group entry for each stack group class instance and link
      stacks linked to respective class instance to new stack group.
docstring_tokens: Create a stack group entry for each stack group class instance and link stacks linked to respective class instance to new stack group .
function: def forward_update_stack_groups(apps, schema_editor):
      """Create a stack group entry for each stack group class instance and link
      stacks linked to respective class instance to new stack group.
      """
  
      ClassInstance = apps.get_model('catmaid', 'ClassInstance')
      StackGroup = apps.get_model('catmaid', 'StackGroup')
      existing_stackgroups = ClassInstance.objects.filter(
              class_column__class_name='stackgroup')
  
      sg_id_mapping = {}
      sg_names = set()
  
      for sg in existing_stackgroups:
          if sg.name in sg_names:
              name = f"{sg.name} ({sg.project.id})"
          else:
              name = sg.name
  
          new_sg = StackGroup.objects.create(title=name)
          sg_id_mapping[sg.id] = new_sg.id
  
      if not sg_id_mapping:
          return
  
      sg_pattern = ','.join([f"({k}, {v})" for k,v in sg_id_mapping.items()])
      cursor = connection.cursor()
      cursor.execute(f"""
          INSERT INTO stack_stack_group (stack_id, stack_group_id, group_relation_id, position)
          SELECT sci.stack_id, sg_mapping.new_id, sgr.id, 0
          FROM stack_class_instance sci
          JOIN (VALUES {sg_pattern}) sg_mapping(old_id, new_id)
          ON sci.class_instance_id = sg_mapping.old_id
          JOIN relation r
          ON sci.relation_id = r.id
          JOIN stack_group_relation sgr
          ON 'has_' || sgr.name = r.relation_name
      """)
function_tokens: def forward_update_stack_groups ( apps , schema_editor ) : ClassInstance = apps . get_model ( 'catmaid' , 'ClassInstance' ) StackGroup = apps . get_model ( 'catmaid' , 'StackGroup' ) existing_stackgroups = ClassInstance . objects . filter ( class_column__class_name = 'stackgroup' ) sg_id_mapping = { } sg_names = set ( ) for sg in existing_stackgroups : if sg . name in sg_names : name = f"{sg.name} ({sg.project.id})" else : name = sg . name new_sg = StackGroup . objects . create ( title = name ) sg_id_mapping [ sg . id ] = new_sg . id if not sg_id_mapping : return sg_pattern = ',' . join ( [ f"({k}, {v})" for k , v in sg_id_mapping . items ( ) ] ) cursor = connection . cursor ( ) cursor . execute ( f"""
          INSERT INTO stack_stack_group (stack_id, stack_group_id, group_relation_id, position)
          SELECT sci.stack_id, sg_mapping.new_id, sgr.id, 0
          FROM stack_class_instance sci
          JOIN (VALUES {sg_pattern}) sg_mapping(old_id, new_id)
          ON sci.class_instance_id = sg_mapping.old_id
          JOIN relation r
          ON sci.relation_id = r.id
          JOIN stack_group_relation sgr
          ON 'has_' || sgr.name = r.relation_name
      """ )