Browse Source

fix: migrate ops adhoc and playbook unique_together error

pull/14132/head
wangruidong 2 months ago committed by Bryan
parent
commit
cd76294e81
  1. 29
      apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py
  2. 56
      apps/users/migrations/0001_initial.py

29
apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py

@ -2,16 +2,43 @@
from django.conf import settings from django.conf import settings
from django.db import migrations, models 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 = [ dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL), migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('ops', '0002_celerytask'), ('ops', '0002_celerytask'),
] ]
operations = [ operations = [
migrations.RunPython(migrate_ops_adhoc_and_playbook_name),
migrations.AlterUniqueTogether( migrations.AlterUniqueTogether(
name='adhoc', name='adhoc',
unique_together={('name', 'creator')}, unique_together={('name', 'creator')},

56
apps/users/migrations/0001_initial.py

@ -35,7 +35,6 @@ def add_default_admin(apps, schema_editor):
class Migration(migrations.Migration): class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
@ -50,7 +49,9 @@ class Migration(migrations.Migration):
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), ('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')), ('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')), ('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')), ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
('username', models.CharField(max_length=128, unique=True, verbose_name='Username')), ('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')), ('is_service_account', models.BooleanField(default=False, verbose_name='Is service account')),
('avatar', models.ImageField(null=True, upload_to='avatar', verbose_name='Avatar')), ('avatar', models.ImageField(null=True, upload_to='avatar', verbose_name='Avatar')),
('wechat', common.db.fields.EncryptCharField(blank=True, max_length=128, verbose_name='Wechat')), ('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')), ('phone',
('mfa_level', models.SmallIntegerField(choices=[(0, "Disabled"), (1, "Enabled"), (2, "Force enabled")], default=0, verbose_name='MFA')), common.db.fields.EncryptCharField(blank=True, max_length=128, null=True, verbose_name='Phone')),
('otp_secret_key', common.db.fields.EncryptCharField(blank=True, max_length=128, null=True, verbose_name='OTP secret key')), ('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')), ('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')), ('public_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Public key')),
('comment', models.TextField(blank=True, null=True, verbose_name='Comment')), ('comment', models.TextField(blank=True, null=True, verbose_name='Comment')),
('is_first_login', models.BooleanField(default=True, verbose_name='Is first login')), ('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')), ('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')), ('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')), ('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')), ('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')), ('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')), ('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')), ('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')), ('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')), ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
], ],
options={ options={
'verbose_name': 'User', 'verbose_name': 'User',
'ordering': ['username'], '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=[ managers=[
('objects', users.models.user.UserManager()), ('objects', users.models.user.UserManager()),
], ],
@ -97,7 +114,9 @@ class Migration(migrations.Migration):
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), ('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
('password', models.CharField(max_length=128)), ('password', models.CharField(max_length=128)),
('date_created', models.DateTimeField(auto_now_add=True, verbose_name='Date created')), ('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={ options={
'verbose_name': 'User password history', 'verbose_name': 'User password history',
@ -112,7 +131,8 @@ class Migration(migrations.Migration):
('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
('comment', models.TextField(blank=True, default='', verbose_name='Comment')), ('comment', models.TextField(blank=True, default='', verbose_name='Comment')),
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)), ('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')), ('name', models.CharField(max_length=128, verbose_name='Name')),
], ],
options={ options={
@ -124,12 +144,15 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='user', model_name='user',
name='groups', 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( migrations.AddField(
model_name='user', model_name='user',
name='user_permissions', 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( migrations.CreateModel(
name='Preference', name='Preference',
@ -139,7 +162,8 @@ class Migration(migrations.Migration):
('category', models.CharField(max_length=128, verbose_name='Category')), ('category', models.CharField(max_length=128, verbose_name='Category')),
('value', models.TextField(blank=True, default='', verbose_name='Value')), ('value', models.TextField(blank=True, default='', verbose_name='Value')),
('encrypted', models.BooleanField(default=False, verbose_name='Encrypted')), ('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={ options={
'verbose_name': 'Preference', 'verbose_name': 'Preference',

Loading…
Cancel
Save