# Generated manually — Role.club_id + 放宽 tupianpeizhi 唯一约束(关注快手可多图)

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('jituan', '0002_admin_audit_operator_widen'),
        ('config', '0006_display_club_id'),
    ]

    operations = [
        migrations.RunSQL(
            sql="""
            ALTER TABLE Role ADD COLUMN club_id VARCHAR(16) NOT NULL DEFAULT '';
            CREATE INDEX role_club_id_idx ON Role (club_id);
            UPDATE Role SET club_id = '' WHERE RoleName = '管理员';
            UPDATE Role SET club_id = 'xq' WHERE club_id = '' AND RoleName <> '管理员';
            """,
            reverse_sql="ALTER TABLE Role DROP COLUMN club_id;",
        ),
        migrations.RunSQL(
            sql="""
            SET @idx_exists := (
                SELECT COUNT(1) FROM information_schema.statistics
                WHERE table_schema = DATABASE()
                  AND table_name = 'tupianpeizhi'
                  AND index_name = 'tupianpeizhi_club_id_ImageType_uniq'
            );
            SET @sql := IF(@idx_exists > 0,
                'ALTER TABLE tupianpeizhi DROP INDEX tupianpeizhi_club_id_ImageType_uniq',
                'SELECT 1');
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql=migrations.RunSQL.noop,
        ),
    ]
