From 23ee5e75b9ff1d732ab1268d630638d49c81f428 Mon Sep 17 00:00:00 2001
From: pycook <pycook@126.com>
Date: Thu, 27 Jun 2024 21:33:19 +0800
Subject: [PATCH] chore: update docker compose

---
 cmdb-api/api/commands/click_cmdb.py | 75 +++++++++++++++--------------
 docker-compose.yml                  |  1 +
 2 files changed, 40 insertions(+), 36 deletions(-)

diff --git a/cmdb-api/api/commands/click_cmdb.py b/cmdb-api/api/commands/click_cmdb.py
index e71606c..c6316f1 100644
--- a/cmdb-api/api/commands/click_cmdb.py
+++ b/cmdb-api/api/commands/click_cmdb.py
@@ -515,44 +515,47 @@ def cmdb_patch(version):
 
     version = version[1:] if version.lower().startswith("v") else version
 
-    if version >= '2.4.6':
+    try:
+        if version >= '2.4.6':
 
-        from api.models.cmdb import CITypeRelation
-        for cr in CITypeRelation.get_by(to_dict=False):
-            if hasattr(cr, 'parent_attr_id') and cr.parent_attr_id and not cr.parent_attr_ids:
-                parent_attr_ids, child_attr_ids = [cr.parent_attr_id], [cr.child_attr_id]
-                cr.update(parent_attr_ids=parent_attr_ids, child_attr_ids=child_attr_ids, commit=False)
-        db.session.commit()
+            from api.models.cmdb import CITypeRelation
+            for cr in CITypeRelation.get_by(to_dict=False):
+                if hasattr(cr, 'parent_attr_id') and cr.parent_attr_id and not cr.parent_attr_ids:
+                    parent_attr_ids, child_attr_ids = [cr.parent_attr_id], [cr.child_attr_id]
+                    cr.update(parent_attr_ids=parent_attr_ids, child_attr_ids=child_attr_ids, commit=False)
+            db.session.commit()
 
-        from api.models.cmdb import AutoDiscoveryCIType, AutoDiscoveryCITypeRelation
-        from api.lib.cmdb.cache import CITypeCache, AttributeCache
-        for adt in AutoDiscoveryCIType.get_by(to_dict=False):
-            if adt.relation:
-                if not AutoDiscoveryCITypeRelation.get_by(ad_type_id=adt.type_id):
-                    peer_type = CITypeCache.get(list(adt.relation.values())[0]['type_name'])
-                    peer_type_id = peer_type and peer_type.id
-                    peer_attr = AttributeCache.get(list(adt.relation.values())[0]['attr_name'])
-                    peer_attr_id = peer_attr and peer_attr.id
-                    if peer_type_id and peer_attr_id:
-                        AutoDiscoveryCITypeRelation.create(ad_type_id=adt.type_id,
-                                                           ad_key=list(adt.relation.keys())[0],
-                                                           peer_type_id=peer_type_id,
-                                                           peer_attr_id=peer_attr_id,
-                                                           commit=False)
-            if hasattr(adt, 'interval') and adt.interval and not adt.cron:
-                adt.cron = "*/{} * * * *".format(adt.interval // 60 or 1)
+            from api.models.cmdb import AutoDiscoveryCIType, AutoDiscoveryCITypeRelation
+            from api.lib.cmdb.cache import CITypeCache, AttributeCache
+            for adt in AutoDiscoveryCIType.get_by(to_dict=False):
+                if adt.relation:
+                    if not AutoDiscoveryCITypeRelation.get_by(ad_type_id=adt.type_id):
+                        peer_type = CITypeCache.get(list(adt.relation.values())[0]['type_name'])
+                        peer_type_id = peer_type and peer_type.id
+                        peer_attr = AttributeCache.get(list(adt.relation.values())[0]['attr_name'])
+                        peer_attr_id = peer_attr and peer_attr.id
+                        if peer_type_id and peer_attr_id:
+                            AutoDiscoveryCITypeRelation.create(ad_type_id=adt.type_id,
+                                                               ad_key=list(adt.relation.keys())[0],
+                                                               peer_type_id=peer_type_id,
+                                                               peer_attr_id=peer_attr_id,
+                                                               commit=False)
+                if hasattr(adt, 'interval') and adt.interval and not adt.cron:
+                    adt.cron = "*/{} * * * *".format(adt.interval // 60 or 1)
 
-        db.session.commit()
+            db.session.commit()
 
-    if version >= "2.4.7":
-        from api.lib.cmdb.auto_discovery.const import DEFAULT_INNER
-        from api.models.cmdb import AutoDiscoveryRule
-        for i in DEFAULT_INNER:
-            existed = AutoDiscoveryRule.get_by(name=i['name'], first=True, to_dict=False)
-            if existed is not None:
-                if "en" in i['option'] and 'en' not in (existed.option or {}):
-                    option = copy.deepcopy(existed.option)
-                    option['en'] = i['option']['en']
-                    existed.update(option=option, commit=False)
+        if version >= "2.4.7":
+            from api.lib.cmdb.auto_discovery.const import DEFAULT_INNER
+            from api.models.cmdb import AutoDiscoveryRule
+            for i in DEFAULT_INNER:
+                existed = AutoDiscoveryRule.get_by(name=i['name'], first=True, to_dict=False)
+                if existed is not None:
+                    if "en" in i['option'] and 'en' not in (existed.option or {}):
+                        option = copy.deepcopy(existed.option)
+                        option['en'] = i['option']['en']
+                        existed.update(option=option, commit=False)
 
-        db.session.commit()
+            db.session.commit()
+    except Exception as e:
+        print("cmdb patch failed: {}".format(e))
diff --git a/docker-compose.yml b/docker-compose.yml
index 8c05dc9..f327297 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -71,6 +71,7 @@ services:
         flask cmdb-init-acl
         flask init-import-user-from-acl
         flask init-department
+        flask cmdb-patch -v 2.4.7
         flask cmdb-counter > counter.log 2>&1
     networks:
       new: