2021-03-02 06:57:48 +00:00
|
|
|
# Generated by Django 3.1 on 2021-02-19 04:41
|
|
|
|
|
|
|
|
import time
|
2021-03-02 11:45:44 +00:00
|
|
|
import sys
|
|
|
|
|
2021-03-02 06:57:48 +00:00
|
|
|
from django.db import migrations
|
|
|
|
|
|
|
|
|
2021-03-16 12:17:13 +00:00
|
|
|
default_id = '00000000-0000-0000-0000-000000000002'
|
2021-03-02 06:57:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_default_org(apps, schema_editor):
|
|
|
|
org_cls = apps.get_model('orgs', 'Organization')
|
2021-03-17 03:08:20 +00:00
|
|
|
defaults = {'name': 'Default', 'id': default_id}
|
2021-03-02 06:57:48 +00:00
|
|
|
org_cls.objects.get_or_create(defaults=defaults, id=default_id)
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_default_org_id(apps, schema_editor):
|
|
|
|
org_app_models = [
|
|
|
|
('applications', ['Application']),
|
|
|
|
('assets', [
|
|
|
|
'AdminUser', 'Asset', 'AuthBook', 'CommandFilter',
|
|
|
|
'CommandFilterRule', 'Domain', 'Gateway', 'GatheredUser',
|
|
|
|
'Label', 'Node', 'SystemUser'
|
|
|
|
]),
|
|
|
|
('audits', ['FTPLog', 'OperateLog']),
|
|
|
|
('ops', ['AdHoc', 'AdHocExecution', 'CommandExecution', 'Task']),
|
|
|
|
('perms', ['ApplicationPermission', 'AssetPermission', 'UserAssetGrantedTreeNodeRelation']),
|
|
|
|
('terminal', ['Session', 'Command']),
|
|
|
|
('tickets', ['Ticket']),
|
|
|
|
('users', ['UserGroup']),
|
|
|
|
]
|
|
|
|
print("")
|
|
|
|
for app, models_name in org_app_models:
|
|
|
|
for model_name in models_name:
|
|
|
|
t_start = time.time()
|
|
|
|
print("Migrate model org id: {}".format(model_name), end='')
|
2021-03-02 11:45:44 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
2021-03-02 06:57:48 +00:00
|
|
|
model_cls = apps.get_model(app, model_name)
|
|
|
|
model_cls.objects.filter(org_id='').update(org_id=default_id)
|
|
|
|
interval = round((time.time() - t_start) * 1000, 2)
|
|
|
|
print(" done, use {} ms".format(interval))
|
|
|
|
|
|
|
|
|
2021-03-02 11:45:44 +00:00
|
|
|
def add_all_user_to_default_org(apps, schema_editor):
|
2022-02-18 06:28:28 +00:00
|
|
|
user_model = apps.get_model('users', 'User')
|
|
|
|
org_model = apps.get_model('orgs', 'Organization')
|
|
|
|
org_members_model = apps.get_model('orgs', 'OrganizationMember')
|
2021-03-02 11:45:44 +00:00
|
|
|
|
2022-02-18 06:28:28 +00:00
|
|
|
users_qs = user_model.objects.all()
|
|
|
|
default_org = org_model.objects.get(id=default_id)
|
2021-03-02 11:45:44 +00:00
|
|
|
|
|
|
|
t_start = time.time()
|
2021-03-11 03:09:47 +00:00
|
|
|
count = users_qs.count()
|
|
|
|
print(f'Will add users to default org: {count}')
|
2021-03-02 11:45:44 +00:00
|
|
|
|
|
|
|
batch_size = 1000
|
|
|
|
for i in range(0, count, batch_size):
|
2021-03-11 03:09:47 +00:00
|
|
|
users = list(users_qs[i:i + batch_size])
|
2022-02-18 06:28:28 +00:00
|
|
|
members = [org_members_model(user=user, org=default_org) for user in users]
|
|
|
|
org_members_model.objects.bulk_create(members, ignore_conflicts=True)
|
2021-03-11 03:09:47 +00:00
|
|
|
print(f'Add users to default org: {i+1}-{i+len(users)}')
|
2021-03-02 11:45:44 +00:00
|
|
|
interval = round((time.time() - t_start) * 1000, 2)
|
|
|
|
print(f'done, use {interval} ms')
|
|
|
|
|
|
|
|
|
2021-03-02 06:57:48 +00:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
2021-03-11 02:46:57 +00:00
|
|
|
('tickets', '0007_auto_20201224_1821'),
|
|
|
|
('audits', '0011_userloginlog_backend'),
|
|
|
|
('ops', '0019_adhocexecution_celery_task_id'),
|
2021-03-05 07:14:07 +00:00
|
|
|
('perms', '0018_auto_20210208_1515'),
|
2021-03-11 02:46:57 +00:00
|
|
|
('applications', '0008_auto_20210104_0435'),
|
|
|
|
('terminal', '0031_auto_20210113_1356'),
|
|
|
|
('users', '0031_auto_20201118_1801'),
|
|
|
|
('assets', '0066_auto_20210208_1802'),
|
|
|
|
('orgs', '0009_auto_20201023_1628'),
|
2021-03-02 06:57:48 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(add_default_org),
|
2021-03-02 11:45:44 +00:00
|
|
|
migrations.RunPython(migrate_default_org_id),
|
|
|
|
migrations.RunPython(add_all_user_to_default_org)
|
2021-03-02 06:57:48 +00:00
|
|
|
]
|