Skip to content

Commit

Permalink
fix(backend): sqlserver数据迁移返回数据补充 #5164
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 10609
  • Loading branch information
ygcyao authored and jinquantianxia committed Jun 28, 2024
1 parent ba690bb commit 6af99f9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
23 changes: 23 additions & 0 deletions dbm-ui/backend/db_meta/migrations/0039_auto_20240626_1602.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-06-26 08:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("db_meta", "0038_auto_20240621_1226"),
]

operations = [
migrations.AddField(
model_name="sqlserverdtsinfo",
name="db_list",
field=models.JSONField(blank=True, default=list, help_text="库正则", null=True),
),
migrations.AddField(
model_name="sqlserverdtsinfo",
name="ignore_db_list",
field=models.JSONField(blank=True, default=list, help_text="忽略库正则", null=True),
),
]
17 changes: 17 additions & 0 deletions dbm-ui/backend/db_meta/models/sqlserver_dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import logging
from datetime import datetime

from django.db import models
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -47,6 +48,8 @@ class SqlserverDtsInfo(AuditedModel):
bk_biz_id = models.IntegerField(default=0, help_text=_("关联的业务id,对应cmdb"))
source_cluster_id = models.IntegerField(default=0, help_text=_("源集群ID"))
target_cluster_id = models.IntegerField(default=0, help_text=_("目标集群ID"))
db_list = models.JSONField(default=list, blank=True, null=True, help_text=_("库正则"))
ignore_db_list = models.JSONField(default=list, blank=True, null=True, help_text=_("忽略库正则"))
dts_mode = models.CharField(max_length=64, choices=SqlserverDtsMode.get_choices(), help_text=_("迁移类型"))
ticket_id = models.PositiveIntegerField(default=0, help_text=_("关联的单据id"))
root_id = models.CharField(max_length=64, default="", help_text=_("关联root_id"))
Expand All @@ -63,3 +66,17 @@ class Meta:
"source_cluster_id",
"target_cluster_id",
)

def to_dict(self):
"""重写model_to_dict()方法转字典"""

opts = self._meta
data = {}
for f in opts.concrete_fields:
value = f.value_from_object(self)
if isinstance(value, datetime):
value = value.strftime("%Y-%m-%d %H:%M:%S")
elif isinstance(f, models.FileField):
value = value.url if value else None
data[f.name] = value
return data
3 changes: 1 addition & 2 deletions dbm-ui/backend/db_services/sqlserver/data_migrate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import itertools
from typing import Dict, List

from django.forms.models import model_to_dict
from django.utils.translation import ugettext as _
from rest_framework import status
from rest_framework.decorators import action
Expand Down Expand Up @@ -70,7 +69,7 @@ def force_failed_migrate(self, request, *args, **kwargs):
def query_migrate_records(self, request, bk_biz_id):
data = self.params_validate(self.get_serializer_class())
# (不分页)获取全量的迁移记录
migrate_records = [model_to_dict(dts) for dts in SqlserverDtsInfo.objects.filter(bk_biz_id=bk_biz_id)]
migrate_records = [dts.to_dict() for dts in SqlserverDtsInfo.objects.filter(bk_biz_id=bk_biz_id)]
cluster_tuple_ids = [[record["source_cluster_id"], record["target_cluster_id"]] for record in migrate_records]
clusters = Cluster.objects.filter(id__in=list(set(itertools.chain(*cluster_tuple_ids))))
cluster_id__cluster_domain = {cluster.id: cluster.immute_domain for cluster in clusters}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def create_dts_infos(self):
ticket_id=self.ticket.id,
status=DtsStatus.ToDo,
dts_config=dts_config,
db_list=info["db_list"],
ignore_db_list=info["ignore_db_list"],
)
dts_infos.append(dts_info)

Expand Down

0 comments on commit 6af99f9

Please sign in to comment.