fix: migration 加唯一约束前自动去重,补充部署清理脚本
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
from django.db import migrations, models
|
||||
from django.db.models import Max
|
||||
|
||||
|
||||
def _dedupe_withdrawal_daily(apps, schema_editor):
|
||||
Model = apps.get_model('backend', 'WithdrawalDailyStats')
|
||||
for row in Model.objects.values('club_id', 'Date', 'WithdrawalType').annotate(max_id=Max('id')):
|
||||
Model.objects.filter(
|
||||
club_id=row['club_id'],
|
||||
Date=row['Date'],
|
||||
WithdrawalType=row['WithdrawalType'],
|
||||
).exclude(id=row['max_id']).delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@@ -17,6 +28,7 @@ class Migration(migrations.Migration):
|
||||
name='withdrawaldailystats',
|
||||
unique_together=set(),
|
||||
),
|
||||
migrations.RunPython(_dedupe_withdrawal_daily, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='withdrawaldailystats',
|
||||
unique_together={('club_id', 'Date', 'WithdrawalType')},
|
||||
|
||||
@@ -1,4 +1,34 @@
|
||||
from django.db import migrations, models
|
||||
from django.db.models import Max
|
||||
|
||||
|
||||
def _dedupe_role_daily(apps, schema_editor, model_name, user_field):
|
||||
Model = apps.get_model('backend', model_name)
|
||||
for row in Model.objects.values('club_id', user_field, 'Date').annotate(max_id=Max('id')):
|
||||
Model.objects.filter(
|
||||
club_id=row['club_id'],
|
||||
**{user_field: row[user_field], 'Date': row['Date']},
|
||||
).exclude(id=row['max_id']).delete()
|
||||
|
||||
|
||||
def _dedupe_merchant(apps, schema_editor):
|
||||
_dedupe_role_daily(apps, schema_editor, 'MerchantDailyStats', 'MerchantID')
|
||||
|
||||
|
||||
def _dedupe_player(apps, schema_editor):
|
||||
_dedupe_role_daily(apps, schema_editor, 'PlayerDailyStats', 'PlayerID')
|
||||
|
||||
|
||||
def _dedupe_leader(apps, schema_editor):
|
||||
_dedupe_role_daily(apps, schema_editor, 'LeaderDailyStats', 'LeaderID')
|
||||
|
||||
|
||||
def _dedupe_manager(apps, schema_editor):
|
||||
_dedupe_role_daily(apps, schema_editor, 'ManagerDailyStats', 'ManagerID')
|
||||
|
||||
|
||||
def _dedupe_manager_renewal(apps, schema_editor):
|
||||
_dedupe_role_daily(apps, schema_editor, 'ManagerRenewalDailyStats', 'ManagerID')
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@@ -33,22 +63,27 @@ class Migration(migrations.Migration):
|
||||
name='club_id',
|
||||
field=models.CharField(db_index=True, default='xq', max_length=16, verbose_name='俱乐部ID'),
|
||||
),
|
||||
migrations.RunPython(_dedupe_merchant, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='merchantdailystats',
|
||||
unique_together={('club_id', 'MerchantID', 'Date')},
|
||||
),
|
||||
migrations.RunPython(_dedupe_player, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='playerdailystats',
|
||||
unique_together={('club_id', 'PlayerID', 'Date')},
|
||||
),
|
||||
migrations.RunPython(_dedupe_leader, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='leaderdailystats',
|
||||
unique_together={('club_id', 'LeaderID', 'Date')},
|
||||
),
|
||||
migrations.RunPython(_dedupe_manager, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='managerdailystats',
|
||||
unique_together={('club_id', 'ManagerID', 'Date')},
|
||||
),
|
||||
migrations.RunPython(_dedupe_manager_renewal, migrations.RunPython.noop),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='managerrenewaldailystats',
|
||||
unique_together={('club_id', 'ManagerID', 'Date')},
|
||||
|
||||
Reference in New Issue
Block a user