def METHOD_NAME(apps, schema_editor):
    Event = apps.get_model('pretixbase', 'Event')
    Organizer = apps.get_model('pretixbase', 'Organizer')
    Team = apps.get_model('pretixbase', 'Team')
    TeamInvite = apps.get_model('pretixbase', 'TeamInvite')
    EventPermission = apps.get_model('pretixbase', 'EventPermission')
    OrganizerPermission = apps.get_model('pretixbase', 'OrganizerPermission')
    for o in Organizer.objects.prefetch_related('events'):
        for e in o.events.all():
            teams = {}
            for p in e.user_perms.all():
                pkey = (p.can_change_settings, p.can_change_items, p.can_view_orders,
                        p.can_change_permissions, p.can_change_orders, p.can_view_vouchers,
                        p.can_change_vouchers)
                if pkey not in teams:
                    team = Team()
                    team.can_change_event_settings = p.can_change_settings
                    team.can_change_items = p.can_change_items
                    team.can_view_orders = p.can_view_orders
                    team.can_change_orders = p.can_change_orders
                    team.can_view_vouchers = p.can_view_vouchers
                    team.can_change_vouchers = p.can_change_vouchers
                    team.organizer = o
                    team.name = '{} Team {}'.format(
                        str(e.name), len(teams) + 1
                    )
                    team.save()
                    team.limit_events.add(e)
                    teams[pkey] = team
                if p.user:
                    teams[pkey].members.add(p.user)
                else:
                    teams[pkey].invites.create(email=p.invite_email, token=p.invite_token)
        teams = {}
        for p in o.user_perms.all():
            pkey = (p.can_create_events, p.can_change_permissions)
            if pkey not in teams:
                team = Team()
                team.can_change_organizer_settings = True
                team.can_create_events = p.can_create_events
                team.can_change_teams = p.can_change_permissions
                team.organizer = o
                team.name = '{} Team {}'.format(
                    str(o.name), len(teams) + 1
                )
                team.save()
                teams[pkey] = team
            if p.user:
                teams[pkey].members.add(p.user)
            else:
                teams[pkey].invites.create(email=p.invite_email, token=p.invite_token)