From cd76294e81cf688472144e8feee3654c1c0128ac Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Thu, 12 Sep 2024 16:00:55 +0800 Subject: [PATCH] fix: migrate ops adhoc and playbook unique_together error --- ...03_alter_adhoc_unique_together_and_more.py | 29 +++++++++- apps/users/migrations/0001_initial.py | 56 +++++++++++++------ 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py b/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py index 610e19330..cbe7233a1 100644 --- a/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py +++ b/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py @@ -2,16 +2,43 @@ from django.conf import settings from django.db import migrations, models +from orgs.models import Organization -class Migration(migrations.Migration): +def migrate_ops_adhoc_and_playbook_name(apps, schema_editor): + Adhoc = apps.get_model('ops', 'adhoc') + Playbook = apps.get_model('ops', 'playbook') + adhocs_to_update = Adhoc.objects.exclude(org_id=Organization.DEFAULT_ID) + for adhoc in adhocs_to_update: + try: + org = Organization.objects.get(id=adhoc.org_id) + suffix = f'({org.name})' + except Exception as e: + suffix = f'({str(adhoc.id)[:6]})' + + adhoc.name = f'{adhoc.name}{suffix}' + Adhoc.objects.bulk_update(adhocs_to_update, ['name']) + + playbooks_to_update = Playbook.objects.exclude(org_id=Organization.DEFAULT_ID) + for playbook in playbooks_to_update: + try: + org = Organization.objects.get(id=playbook.org_id) + suffix = f'({org.name})' + except Exception as e: + suffix = f'({str(playbook.id)[:6]})' + playbook.name = f'{playbook.name}{suffix}' + playbook.save() + Playbook.objects.bulk_update(playbooks_to_update, ['name']) + +class Migration(migrations.Migration): dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('ops', '0002_celerytask'), ] operations = [ + migrations.RunPython(migrate_ops_adhoc_and_playbook_name), migrations.AlterUniqueTogether( name='adhoc', unique_together={('name', 'creator')}, diff --git a/apps/users/migrations/0001_initial.py b/apps/users/migrations/0001_initial.py index 4a9ced4ba..5b47304b2 100644 --- a/apps/users/migrations/0001_initial.py +++ b/apps/users/migrations/0001_initial.py @@ -35,7 +35,6 @@ def add_default_admin(apps, schema_editor): class Migration(migrations.Migration): - initial = True dependencies = [ @@ -50,7 +49,9 @@ class Migration(migrations.Migration): ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), - ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('is_active', models.BooleanField(default=True, + help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', + verbose_name='active')), ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), ('username', models.CharField(max_length=128, unique=True, verbose_name='Username')), @@ -60,33 +61,49 @@ class Migration(migrations.Migration): ('is_service_account', models.BooleanField(default=False, verbose_name='Is service account')), ('avatar', models.ImageField(null=True, upload_to='avatar', verbose_name='Avatar')), ('wechat', common.db.fields.EncryptCharField(blank=True, max_length=128, verbose_name='Wechat')), - ('phone', common.db.fields.EncryptCharField(blank=True, max_length=128, null=True, verbose_name='Phone')), - ('mfa_level', models.SmallIntegerField(choices=[(0, "Disabled"), (1, "Enabled"), (2, "Force enabled")], default=0, verbose_name='MFA')), - ('otp_secret_key', common.db.fields.EncryptCharField(blank=True, max_length=128, null=True, verbose_name='OTP secret key')), + ('phone', + common.db.fields.EncryptCharField(blank=True, max_length=128, null=True, verbose_name='Phone')), + ('mfa_level', + models.SmallIntegerField(choices=[(0, "Disabled"), (1, "Enabled"), (2, "Force enabled")], default=0, + verbose_name='MFA')), + ('otp_secret_key', common.db.fields.EncryptCharField(blank=True, max_length=128, null=True, + verbose_name='OTP secret key')), ('private_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Private key')), ('public_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Public key')), ('comment', models.TextField(blank=True, null=True, verbose_name='Comment')), ('is_first_login', models.BooleanField(default=True, verbose_name='Is first login')), - ('date_expired', models.DateTimeField(blank=True, db_index=True, default=common.utils.django.date_expired_default, null=True, verbose_name='Date expired')), + ('date_expired', + models.DateTimeField(blank=True, db_index=True, default=common.utils.django.date_expired_default, + null=True, verbose_name='Date expired')), ('created_by', models.CharField(blank=True, default='', max_length=30, verbose_name='Created by')), ('updated_by', models.CharField(blank=True, default='', max_length=30, verbose_name='Updated by')), - ('date_password_last_updated', models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date password last updated')), + ('date_password_last_updated', + models.DateTimeField(auto_now_add=True, null=True, verbose_name='Date password last updated')), ('need_update_password', models.BooleanField(default=False, verbose_name='Need update password')), - ('source', models.CharField(choices=[('local', 'Local'), ('ldap', 'LDAP/AD'), ('openid', 'OpenID'), ('radius', 'Radius'), ('cas', 'CAS'), ('saml2', 'SAML2'), ('oauth2', 'OAuth2'), ('wecom', 'WeCom'), ('dingtalk', 'DingTalk'), ('feishu', 'FeiShu'), ('lark', 'Lark'), ('slack', 'Slack'), ('custom', 'Custom')], default='local', max_length=30, verbose_name='Source')), + ('source', models.CharField( + choices=[('local', 'Local'), ('ldap', 'LDAP/AD'), ('ldap_ha', 'LDAP/AD (HA)'), ('openid', 'OpenID'), + ('radius', 'Radius'), ('cas', 'CAS'), ('saml2', 'SAML2'), ('oauth2', 'OAuth2'), + ('wecom', 'WeCom'), ('dingtalk', 'DingTalk'), ('feishu', 'FeiShu'), ('lark', 'Lark'), + ('slack', 'Slack'), ('custom', 'Custom')], default='local', max_length=30, + verbose_name='Source')), ('wecom_id', models.CharField(default=None, max_length=128, null=True, verbose_name='WeCom')), ('dingtalk_id', models.CharField(default=None, max_length=128, null=True, verbose_name='DingTalk')), ('feishu_id', models.CharField(default=None, max_length=128, null=True, verbose_name='FeiShu')), ('lark_id', models.CharField(default=None, max_length=128, null=True, verbose_name='Lark')), ('slack_id', models.CharField(default=None, max_length=128, null=True, verbose_name='Slack')), - ('date_api_key_last_used', models.DateTimeField(blank=True, null=True, verbose_name='Date api key used')), + ('date_api_key_last_used', + models.DateTimeField(blank=True, null=True, verbose_name='Date api key used')), ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), ], options={ 'verbose_name': 'User', 'ordering': ['username'], - 'permissions': [('invite_user', 'Can invite user'), ('remove_user', 'Can remove user'), ('match_user', 'Can match user')], + 'permissions': [('invite_user', 'Can invite user'), ('remove_user', 'Can remove user'), + ('match_user', 'Can match user')], }, - bases=(users.models.user.AuthMixin, users.models.user.SourceMixin, users.models.user.TokenMixin, users.models.user.RoleMixin, users.models.user.MFAMixin, users.models.user.JSONFilterMixin, models.Model), + bases=(users.models.user.AuthMixin, users.models.user.SourceMixin, users.models.user.TokenMixin, + users.models.user.RoleMixin, users.models.user.MFAMixin, users.models.user.JSONFilterMixin, + models.Model), managers=[ ('objects', users.models.user.UserManager()), ], @@ -97,7 +114,9 @@ class Migration(migrations.Migration): ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), ('password', models.CharField(max_length=128)), ('date_created', models.DateTimeField(auto_now_add=True, verbose_name='Date created')), - ('user', models.ForeignKey(on_delete=common.db.models.CASCADE_SIGNAL_SKIP, related_name='history_passwords', to=settings.AUTH_USER_MODEL, verbose_name='User')), + ('user', + models.ForeignKey(on_delete=common.db.models.CASCADE_SIGNAL_SKIP, related_name='history_passwords', + to=settings.AUTH_USER_MODEL, verbose_name='User')), ], options={ 'verbose_name': 'User password history', @@ -112,7 +131,8 @@ class Migration(migrations.Migration): ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), ('comment', models.TextField(blank=True, default='', verbose_name='Comment')), ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), - ('org_id', models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')), + ('org_id', + models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')), ('name', models.CharField(max_length=128, verbose_name='Name')), ], options={ @@ -124,12 +144,15 @@ class Migration(migrations.Migration): migrations.AddField( model_name='user', name='groups', - field=models.ManyToManyField(blank=True, related_name='users', to='users.usergroup', verbose_name='User group'), + field=models.ManyToManyField(blank=True, related_name='users', to='users.usergroup', + verbose_name='User group'), ), migrations.AddField( model_name='user', name='user_permissions', - field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions'), + field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', + related_name='user_set', related_query_name='user', to='auth.permission', + verbose_name='user permissions'), ), migrations.CreateModel( name='Preference', @@ -139,7 +162,8 @@ class Migration(migrations.Migration): ('category', models.CharField(max_length=128, verbose_name='Category')), ('value', models.TextField(blank=True, default='', verbose_name='Value')), ('encrypted', models.BooleanField(default=False, verbose_name='Encrypted')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='preferences', to=settings.AUTH_USER_MODEL, verbose_name='Users')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='preferences', + to=settings.AUTH_USER_MODEL, verbose_name='Users')), ], options={ 'verbose_name': 'Preference',