# Generated by Django 3.1.7 on 2021-11-04 20:32

from typing import Iterable, List, TypeVar

from django.db import migrations

T = TypeVar("T")


def batch(batch_size: int, iterable: Iterable[T]) -> Iterable[List[T]]:
    collected = []
    for entry in iterable:
        collected.append(entry)
        if len(collected) >= batch_size:
            yield collected
            collected = []
    if len(collected) > 0:
        yield collected


def forwards(apps, schema_editor):
    Namespace = apps.get_model("repository", "Namespace")
    Team = apps.get_model("repository", "Team")
    for entry in batch(
        2000,
        map(
            lambda x: Namespace(team_id=x["id"], name=x["name"]),
            Team.objects.values("id", "name"),
        ),
    ):
        Namespace.objects.bulk_create(entry)


class Migration(migrations.Migration):

    dependencies = [
        ("repository", "0028_namespace"),
    ]

    operations = [
        migrations.RunPython(forwards, migrations.RunPython.noop),
    ]
