From 1b1b70e7bdfeb3b64896060d9fcc52b6842d236f Mon Sep 17 00:00:00 2001 From: feng626 <1304903146@qq.com> Date: Fri, 15 Jul 2022 16:30:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=84=E7=90=86=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E8=84=8F=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0023_auto_20220715_1556.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 apps/applications/migrations/0023_auto_20220715_1556.py diff --git a/apps/applications/migrations/0023_auto_20220715_1556.py b/apps/applications/migrations/0023_auto_20220715_1556.py new file mode 100644 index 000000000..03123efab --- /dev/null +++ b/apps/applications/migrations/0023_auto_20220715_1556.py @@ -0,0 +1,48 @@ +# Generated by Django 3.1.14 on 2022-07-15 07:56 +import time +from collections import defaultdict + +from django.db import migrations + + +def migrate_account_dirty_data(apps, schema_editor): + db_alias = schema_editor.connection.alias + account_model = apps.get_model('applications', 'Account') + + count = 0 + bulk_size = 1000 + + while True: + accounts = account_model.objects.using(db_alias) \ + .filter(org_id='')[count:count + bulk_size] + + if not accounts: + break + + accounts = list(accounts) + start = time.time() + for i in accounts: + if i.app: + org_id = i.app.org_id + elif i.systemuser: + org_id = i.systemuser.org_id + else: + org_id = '' + if org_id: + i.org_id = org_id + + account_model.objects.bulk_update(accounts, ['org_id', ]) + print("Update account org is empty: {}-{} using: {:.2f}s".format( + count, count + len(accounts), time.time() - start + )) + count += len(accounts) + + +class Migration(migrations.Migration): + dependencies = [ + ('applications', '0022_auto_20220714_1046'), + ] + + operations = [ + migrations.RunPython(migrate_account_dirty_data), + ]