from django.db import migrations


def backfill_album_artist_to_artists(apps, schema_editor):
    Album = apps.get_model("music", "Album")
    AlbumArtist = Album.artists.through
    AlbumArtist.objects.bulk_create(
        [
            AlbumArtist(album_id=r["id"], artist_id=r["album_artist_id"])
            for r in Album.objects.filter(album_artist__isnull=False).values(
                "id", "album_artist_id"
            )
        ],
        ignore_conflicts=True,
    )


class Migration(migrations.Migration):

    dependencies = [
        ("music", "0033_backfill_track_artists"),
    ]

    operations = [
        migrations.RunPython(
            backfill_album_artist_to_artists,
            reverse_code=migrations.RunPython.noop,
        ),
    ]
