jumpserver/apps/applications/migrations/0023_auto_20220715_1556.py

48 lines
1.2 KiB
Python
Raw Normal View History

# Generated by Django 3.1.14 on 2022-07-15 07:56
2022-08-30 04:50:01 +00:00
import time
2022-08-18 03:15:17 +00:00
from django.db import migrations
2022-08-30 04:50:01 +00:00
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
2022-08-18 03:15:17 +00:00
2022-08-30 04:50:01 +00:00
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):
2022-08-18 03:15:17 +00:00
dependencies = [
('applications', '0022_auto_20220714_1046'),
2022-08-18 03:15:17 +00:00
]
operations = [
2022-08-30 04:50:01 +00:00
migrations.RunPython(migrate_account_dirty_data),
2022-08-18 03:15:17 +00:00
]