perf: 优化一波 migrations

pull/9610/head
ibuler 2023-02-17 16:54:51 +08:00 committed by Jiangjie.Bai
parent 23238ca8e0
commit 98355ce468
16 changed files with 86 additions and 143 deletions

View File

@ -1,16 +0,0 @@
from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _
from ..application_category import DBSerializer
__all__ = ['ClickHouseSerializer']
class ClickHouseSerializer(DBSerializer):
port = serializers.IntegerField(
default=9000, label=_('Port'), allow_null=True,
help_text=_(
'Typically, the port is 9000'
'the HTTP interface and the native interface use different ports'
),
)

View File

@ -117,6 +117,9 @@ class Migration(migrations.Migration):
models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True,
primary_key=True, serialize=False, to='assets.asset')),
],
options={
'verbose_name': 'Host',
},
),
migrations.CreateModel(
name='Database',
@ -145,6 +148,7 @@ class Migration(migrations.Migration):
],
options={
'abstract': False,
'verbose_name': 'Device',
},
bases=('assets.asset',),
),
@ -157,6 +161,7 @@ class Migration(migrations.Migration):
],
options={
'abstract': False,
'verbose_name': 'Cloud',
},
bases=('assets.asset',),
),
@ -178,6 +183,7 @@ class Migration(migrations.Migration):
],
options={
'abstract': False,
'verbose_name': 'Web',
},
bases=('assets.asset',),
),

View File

@ -1,3 +1,4 @@
from django.utils.translation import gettext_lazy as _
from .common import Asset
@ -5,3 +6,6 @@ from .common import Asset
class Cloud(Asset):
def __str__(self):
return self.name
class Meta:
verbose_name = _("Cloud")

View File

@ -1,5 +1,8 @@
from django.utils.translation import gettext_lazy as _
from .common import Asset
class Device(Asset):
pass
class Meta:
verbose_name = _("Device")

View File

@ -1,7 +1,8 @@
from .common import Asset
from django.utils.translation import ugettext_lazy as _
__all__ = ['Host']
from .common import Asset
class Host(Asset):
pass
class Meta:
verbose_name = _("Host")

View File

@ -11,3 +11,6 @@ class Web(Asset):
password_selector = models.CharField(max_length=128, blank=True, default='', verbose_name=_("Password selector"))
submit_selector = models.CharField(max_length=128, blank=True, default='', verbose_name=_("Submit selector"))
script = models.JSONField(blank=True, default=list, verbose_name=_("Script"))
class Meta:
verbose_name = _("Web")

View File

@ -1,2 +0,0 @@
# -*- coding: utf-8 -*-
#

View File

@ -87,11 +87,10 @@ class OperateLogSerializer(serializers.ModelSerializer):
fields_mini = ["id"]
fields_small = fields_mini + [
"user", "action", "resource_type",
"resource_type_display", "resource",
"remote_addr", "datetime", "org_id",
"resource", "remote_addr", "datetime",
"org_id",
]
fields = fields_small
extra_kwargs = {"resource_type_display": {"label": _("Resource Type")}}
class PasswordChangeLogSerializer(serializers.ModelSerializer):

View File

@ -3,8 +3,8 @@
import uuid
from django.apps import apps
from django.dispatch import receiver
from django.db.models.signals import post_save, pre_save, m2m_changed, pre_delete
from django.dispatch import receiver
from audits.handler import (
get_instance_current_with_cache_diff, cache_instance_before_data,
@ -13,10 +13,8 @@ from audits.handler import (
from audits.utils import model_to_dict_for_operate_log as model_to_dict
from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR, SKIP_SIGNAL
from common.signals import django_ready
from ..const import MODELS_NEED_RECORD, ActionChoices
M2M_ACTION = {
POST_ADD: ActionChoices.create,
POST_REMOVE: ActionChoices.delete,
@ -169,6 +167,7 @@ def on_django_start_set_operate_log_monitor_models(sender, **kwargs):
'PermedAsset', 'PermedAccount', 'MenuPermission',
'Permission', 'TicketSession', 'ApplyLoginTicket',
'ApplyCommandTicket', 'ApplyLoginAssetTicket',
'FavoriteAsset',
}
for i, app in enumerate(apps.get_models(), 1):
app_name = app._meta.app_label

View File

@ -1,5 +1,7 @@
# Generated by Django 3.2.14 on 2022-12-28 10:03
import private_storage.fields
import private_storage.storage.files
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
@ -100,7 +102,7 @@ class Migration(migrations.Migration):
models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization')),
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
('name', models.CharField(max_length=128, null=True, verbose_name='Name')),
('path', models.FileField(upload_to='playbooks/')),
('path', private_storage.fields.PrivateFileField(storage=private_storage.storage.files.PrivateFileSystemStorage(), upload_to='playbooks/')),
('comment',
models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='Comment')),
('creator',
@ -109,6 +111,7 @@ class Migration(migrations.Migration):
],
options={
'abstract': False,
'ordering': ['date_created']
},
),
migrations.CreateModel(
@ -242,4 +245,16 @@ class Migration(migrations.Migration):
},
bases=('ops.jobexecution',),
),
migrations.AlterUniqueTogether(
name='adhoc',
unique_together={('name', 'org_id', 'creator')},
),
migrations.AlterUniqueTogether(
name='job',
unique_together={('name', 'org_id', 'creator')},
),
migrations.AlterUniqueTogether(
name='playbook',
unique_together={('name', 'org_id', 'creator')},
),
]

View File

@ -1,8 +1,26 @@
# Generated by Django 3.2.16 on 2022-12-30 08:08
import django.db.models
from django.db import migrations, models
def compatible(apps, schema_editor):
"""
兼容旧版本的数据
"""
model = apps.get_model("ops", "JobExecution")
for obj in model.objects.all():
if obj.job:
if obj.job.type == 'adhoc':
obj.material = "{}:{}".format(obj.job.module, obj.job.args)
if obj.job.type == 'playbook':
obj.material = "{}:{}:{}".format(obj.job.org.name, obj.job.creator.name, obj.job.playbook.name)
obj.job_type = obj.job.type
obj.save()
else:
obj.delete()
class Migration(migrations.Migration):
dependencies = [
@ -20,4 +38,31 @@ class Migration(migrations.Migration):
name='name',
field=models.CharField(max_length=1024, verbose_name='Name'),
),
migrations.AddField(
model_name='playbook',
name='create_method',
field=models.CharField(choices=[('blank', 'Blank'), ('vcs', 'VCS')], default='blank', max_length=128, verbose_name='CreateMethod'),
),
migrations.AddField(
model_name='playbook',
name='vcs_url',
field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='VCS URL'),
),
migrations.AlterField(
model_name='jobexecution',
name='job',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='executions', to='ops.job'),
),
migrations.AddField(
model_name='jobexecution',
name='job_type',
field=models.CharField(choices=[('adhoc', 'Adhoc'), ('playbook', 'Playbook')], default='adhoc',
max_length=128, verbose_name='Material Type'),
),
migrations.AddField(
model_name='jobexecution',
name='material',
field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='Material'),
),
migrations.RunPython(compatible),
]

View File

@ -1,23 +0,0 @@
# Generated by Django 3.2.14 on 2023-01-17 03:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ops', '0024_alter_celerytask_date_last_publish'),
]
operations = [
migrations.AddField(
model_name='playbook',
name='create_method',
field=models.CharField(choices=[('blank', 'Blank'), ('vcs', 'VCS')], default='blank', max_length=128, verbose_name='CreateMethod'),
),
migrations.AddField(
model_name='playbook',
name='vcs_url',
field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='VCS URL'),
),
]

View File

@ -1,19 +0,0 @@
# Generated by Django 3.2.14 on 2023-02-03 08:40
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('ops', '0025_auto_20230117_1130'),
]
operations = [
migrations.AlterField(
model_name='jobexecution',
name='job',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='executions', to='ops.job'),
),
]

View File

@ -1,41 +0,0 @@
# Generated by Django 3.2.16 on 2023-02-06 11:27
from django.db import migrations, models
def compatible(apps, schema_editor):
"""
兼容旧版本的数据
"""
model = apps.get_model("ops", "JobExecution")
for obj in model.objects.all():
if obj.job:
if obj.job.type == 'adhoc':
obj.material = "{}:{}".format(obj.job.module, obj.job.args)
if obj.job.type == 'playbook':
obj.material = "{}:{}:{}".format(obj.job.org.name, obj.job.creator.name, obj.job.playbook.name)
obj.job_type = obj.job.type
obj.save()
else:
obj.delete()
class Migration(migrations.Migration):
dependencies = [
('ops', '0026_alter_jobexecution_job'),
]
operations = [
migrations.AddField(
model_name='jobexecution',
name='job_type',
field=models.CharField(choices=[('adhoc', 'Adhoc'), ('playbook', 'Playbook')], default='adhoc',
max_length=128, verbose_name='Material Type'),
),
migrations.AddField(
model_name='jobexecution',
name='material',
field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='Material'),
),
migrations.RunPython(compatible),
]

View File

@ -1,31 +0,0 @@
# Generated by Django 3.2.16 on 2023-02-13 07:03
from django.conf import settings
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('ops', '0027_auto_20230206_1927'),
]
operations = [
migrations.AlterModelOptions(
name='playbook',
options={'ordering': ['date_created']},
),
migrations.AlterUniqueTogether(
name='adhoc',
unique_together={('name', 'org_id', 'creator')},
),
migrations.AlterUniqueTogether(
name='job',
unique_together={('name', 'org_id', 'creator')},
),
migrations.AlterUniqueTogether(
name='playbook',
unique_together={('name', 'org_id', 'creator')},
),
]