This is an automated email from the ASF dual-hosted git repository.

klesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new ee1b60e32 feat(zentao): check db url when testing connections (#6258)
ee1b60e32 is described below

commit ee1b60e32b6dc333c1ee42e782f954dab20b3aeb
Author: Lynwee <[email protected]>
AuthorDate: Tue Oct 17 11:45:33 2023 +0800

    feat(zentao): check db url when testing connections (#6258)
    
    * feat(zentao): check db url when testing connections
    
    * fix(zentao): remove test codes
    
    * fix(zentao): remove test codes
---
 backend/core/runner/db.go                          | 35 ++++++++++++++++------
 config-ui/src/api/connection/index.ts              |  2 +-
 config-ui/src/api/connection/types.ts              |  2 ++
 .../plugins/components/connection-form/index.tsx   |  1 +
 config-ui/src/store/connections/context.tsx        |  3 ++
 config-ui/src/store/connections/types.ts           |  1 +
 6 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/backend/core/runner/db.go b/backend/core/runner/db.go
index 72febf504..af0a3386c 100644
--- a/backend/core/runner/db.go
+++ b/backend/core/runner/db.go
@@ -160,15 +160,32 @@ func getDbConnection(dbUrl string, conf *gorm.Config) 
(*gorm.DB, error) {
 }
 
 func CheckDbConnection(dbUrl string, d time.Duration) errors.Error {
-       db, err := getDbConnection(dbUrl, &gorm.Config{})
-       if err != nil {
-               return errors.Convert(err)
-       }
        ctx := context.Background()
-       if d > 0 {
-               var cancel context.CancelFunc
-               ctx, cancel = context.WithTimeout(context.Background(), d)
-               defer cancel()
+
+       result := make(chan errors.Error, 1)
+       done := make(chan struct{}, 1)
+       go func() {
+               db, err := getDbConnection(dbUrl, &gorm.Config{})
+               if err != nil {
+                       result <- errors.Convert(err)
+               }
+               if d > 0 {
+                       var cancel context.CancelFunc
+                       ctx, cancel = context.WithTimeout(context.Background(), 
d)
+                       defer cancel()
+               }
+               if err := db.WithContext(ctx).Exec("SELECT 1").Error; err != 
nil {
+                       done <- struct{}{}
+               } else {
+                       result <- errors.Convert(err)
+               }
+       }()
+       select {
+       case <-time.After(d):
+               return errors.Default.New("timeout")
+       case <-done:
+               return nil
+       case err := <-result:
+               return err
        }
-       return errors.Convert(db.WithContext(ctx).Exec("SELECT 1").Error)
 }
diff --git a/config-ui/src/api/connection/index.ts 
b/config-ui/src/api/connection/index.ts
index 138ea6b2e..c142d2879 100644
--- a/config-ui/src/api/connection/index.ts
+++ b/config-ui/src/api/connection/index.ts
@@ -41,6 +41,6 @@ export const test = (
   plugin: string,
   payload: Pick<
     T.ConnectionForm,
-    'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 
'secretKey' | 'proxy'
+    'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 
'secretKey' | 'proxy' | 'dbUrl'
   >,
 ): Promise<T.ConnectionTest> => request(`/plugins/${plugin}/test`, { method: 
'post', data: payload });
diff --git a/config-ui/src/api/connection/types.ts 
b/config-ui/src/api/connection/types.ts
index 933133e2d..a61f954a7 100644
--- a/config-ui/src/api/connection/types.ts
+++ b/config-ui/src/api/connection/types.ts
@@ -26,6 +26,7 @@ export type Connection = {
   password?: string;
   proxy: string;
   apiKey?: string;
+  dbUrl?: string;
 };
 
 export type ConnectionForm = {
@@ -40,6 +41,7 @@ export type ConnectionForm = {
   enableGraphql?: boolean;
   proxy: string;
   rateLimitPerHour?: number;
+  dbUrl?: string;
 };
 
 export type ConnectionTest = {
diff --git a/config-ui/src/plugins/components/connection-form/index.tsx 
b/config-ui/src/plugins/components/connection-form/index.tsx
index e64dd6ba4..4ef0a9573 100644
--- a/config-ui/src/plugins/components/connection-form/index.tsx
+++ b/config-ui/src/plugins/components/connection-form/index.tsx
@@ -73,6 +73,7 @@ export const ConnectionForm = ({ plugin, connectionId, 
onSuccess }: Props) => {
             'secretKey',
             'tenantId',
             'tenantType',
+            "dbUrl",
           ]),
         ),
       {
diff --git a/config-ui/src/store/connections/context.tsx 
b/config-ui/src/store/connections/context.tsx
index a41b238bc..42e213dea 100644
--- a/config-ui/src/store/connections/context.tsx
+++ b/config-ui/src/store/connections/context.tsx
@@ -72,6 +72,7 @@ export const ConnectionContextProvider = ({ children, 
...props }: Props) => {
     authMethod,
     secretKey,
     appId,
+    dbUrl,
   }: ConnectionItemType) => {
     try {
       const res = await API.connection.test(plugin, {
@@ -83,6 +84,7 @@ export const ConnectionContextProvider = ({ children, 
...props }: Props) => {
         authMethod,
         secretKey,
         appId,
+        dbUrl,
       });
       return res.success ? ConnectionStatusEnum.ONLINE : 
ConnectionStatusEnum.OFFLINE;
     } catch {
@@ -109,6 +111,7 @@ export const ConnectionContextProvider = ({ children, 
...props }: Props) => {
       authMethod: it.authMethod,
       secretKey: it.secretKey,
       appId: it.appId,
+      dbUrl: it.dbUrl,
     }));
   };
 
diff --git a/config-ui/src/store/connections/types.ts 
b/config-ui/src/store/connections/types.ts
index 68847166f..07475889a 100644
--- a/config-ui/src/store/connections/types.ts
+++ b/config-ui/src/store/connections/types.ts
@@ -41,4 +41,5 @@ export type ConnectionItemType = {
   authMethod?: string;
   appId?: string;
   secretKey?: string;
+  dbUrl?: string;
 };

Reply via email to