fix: migration 加唯一约束前自动去重,补充部署清理脚本
This commit is contained in:
@@ -1,4 +1,23 @@
|
||||
from django.db import migrations, models
|
||||
from django.db.models import Max
|
||||
|
||||
|
||||
def _dedupe_popuppage(apps, schema_editor):
|
||||
PopupPage = apps.get_model('config', 'PopupPage')
|
||||
for row in PopupPage.objects.values('club_id', 'page_key').annotate(max_id=Max('id')):
|
||||
PopupPage.objects.filter(
|
||||
club_id=row['club_id'],
|
||||
page_key=row['page_key'],
|
||||
).exclude(id=row['max_id']).delete()
|
||||
|
||||
|
||||
def _dedupe_tupianpeizhi(apps, schema_editor):
|
||||
Tupianpeizhi = apps.get_model('config', 'Tupianpeizhi')
|
||||
for row in Tupianpeizhi.objects.values('club_id', 'ImageType').annotate(max_id=Max('id')):
|
||||
Tupianpeizhi.objects.filter(
|
||||
club_id=row['club_id'],
|
||||
ImageType=row['ImageType'],
|
||||
).exclude(id=row['max_id']).delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@@ -45,10 +64,12 @@ class Migration(migrations.Migration):
|
||||
name='page_key',
|
||||
field=models.CharField(db_index=True, max_length=50, verbose_name='页面标识'),
|
||||
),
|
||||
migrations.RunPython(_dedupe_popuppage, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='popuppage',
|
||||
unique_together={('club_id', 'page_key')},
|
||||
),
|
||||
migrations.RunPython(_dedupe_tupianpeizhi, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='tupianpeizhi',
|
||||
unique_together={('club_id', 'ImageType')},
|
||||
|
||||
Reference in New Issue
Block a user