from django.db import migrations, models


def backfill_szjilu_club_id(apps, schema_editor):
    Szjilu = apps.get_model('config', 'Szjilu')
    for row in Szjilu.objects.all():
        if not getattr(row, 'club_id', None):
            row.club_id = 'xq'
            row.save(update_fields=['club_id'])


class Migration(migrations.Migration):

    dependencies = [
        ('config', '0004_daily_stat_club_id'),
    ]

    operations = [
        migrations.AddField(
            model_name='szjilu',
            name='club_id',
            field=models.CharField(
                db_index=True,
                default='xq',
                max_length=16,
                verbose_name='俱乐部ID',
            ),
        ),
        migrations.RunPython(backfill_szjilu_club_id, migrations.RunPython.noop),
        migrations.AddConstraint(
            model_name='szjilu',
            constraint=models.UniqueConstraint(
                fields=('club_id',),
                name='uniq_szjilu_club_id',
            ),
        ),
    ]
