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

camilleteruel 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 e84b73050 fix: Support explicit auto-increment fields in Python (#5785)
e84b73050 is described below

commit e84b73050b108e074a0e6357574337a6b1277021
Author: Keon Amini <[email protected]>
AuthorDate: Wed Aug 2 01:22:54 2023 +0330

    fix: Support explicit auto-increment fields in Python (#5785)
---
 backend/core/models/dynamic_tabler.go              | 48 ++++++++++++++++++++++
 .../plugins/azuredevops/azuredevops/migrations.py  |  2 +-
 backend/python/pydevlake/pydevlake/__init__.py     | 10 ++++-
 backend/python/pydevlake/pydevlake/model.py        |  2 +-
 .../server/services/remote/models/conversion.go    |  4 ++
 backend/server/services/remote/models/migration.go |  4 ++
 6 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/backend/core/models/dynamic_tabler.go 
b/backend/core/models/dynamic_tabler.go
index d9c525c51..bd3c6c444 100644
--- a/backend/core/models/dynamic_tabler.go
+++ b/backend/core/models/dynamic_tabler.go
@@ -133,3 +133,51 @@ func UnwrapObject(ifc any) any {
        }
        return ifc
 }
+
+// DumpInfo Useful function for debugging purposes - to see what's in the 
struct at runtime
+func DumpInfo(tbl DynamicTabler) map[string]any {
+       typ := reflect.TypeOf(tbl.Unwrap())
+       type typeInfo struct {
+               Type string
+               Tags string
+       }
+       var fn func(t reflect.Type) map[string]any
+       fn = func(t reflect.Type) map[string]any {
+               infoMap := map[string]any{}
+               if t.Kind() == reflect.Pointer {
+                       t = t.Elem()
+               }
+               if t.Kind() != reflect.Struct {
+                       return infoMap
+               }
+               for i := 0; i < t.NumField(); i++ {
+                       f := t.Field(i)
+                       if f.Anonymous {
+                               subMap := fn(f.Type)
+                               infoMap[f.Name] = subMap
+                       } else {
+                               if t.Kind() == reflect.Pointer {
+                                       t = t.Elem()
+                               }
+                               if t.Kind() == reflect.Struct {
+                                       subMap := fn(f.Type)
+                                       if len(subMap) == 0 {
+                                               infoMap[f.Name] = typeInfo{
+                                                       Type: f.Type.Name(),
+                                                       Tags: string(f.Tag),
+                                               }
+                                       } else {
+                                               infoMap[f.Name] = subMap
+                                       }
+                               } else {
+                                       infoMap[f.Name] = typeInfo{
+                                               Type: f.Type.Name(),
+                                               Tags: string(f.Tag),
+                                       }
+                               }
+                       }
+               }
+               return infoMap
+       }
+       return fn(typ)
+}
diff --git a/backend/python/plugins/azuredevops/azuredevops/migrations.py 
b/backend/python/plugins/azuredevops/azuredevops/migrations.py
index 615bb2049..bac187be1 100644
--- a/backend/python/plugins/azuredevops/azuredevops/migrations.py
+++ b/backend/python/plugins/azuredevops/azuredevops/migrations.py
@@ -94,7 +94,7 @@ def init_schemas(b: MigrationScriptBuilder):
             PartiallySucceeded = "partiallySucceeded"
             Succeeded = "succeeded"
 
-        id: int = Field(primary_key=True)
+        id: int = Field(primary_key=True, auto_increment=False)
         name: str
         start_time: Optional[datetime.datetime]
         finish_time: Optional[datetime.datetime]
diff --git a/backend/python/pydevlake/pydevlake/__init__.py 
b/backend/python/pydevlake/pydevlake/__init__.py
index c3e759e6f..73ddeb620 100644
--- a/backend/python/pydevlake/pydevlake/__init__.py
+++ b/backend/python/pydevlake/pydevlake/__init__.py
@@ -16,20 +16,24 @@
 from typing import Optional
 
 import pytest
+
 pytest.register_assert_rewrite('pydevlake.testing')
 
 from sqlmodel import Field as _Field
 
 
-def Field(*args, primary_key: bool=False, source: Optional[str]=None, 
**kwargs):
+def Field(*args, primary_key: bool = False, auto_increment: Optional[bool] = 
None, source: Optional[str] = None,
+          **kwargs):
     """
     A wrapper around sqlmodel.Field that adds a source parameter.
     """
     schema_extra = kwargs.get('schema_extra', {})
-    if source:
+    if source is not None:
         schema_extra['source'] = source
     if primary_key:
         schema_extra['primaryKey'] = True
+    if auto_increment is not None:
+        schema_extra['autoIncrement'] = auto_increment
     return _Field(*args, **kwargs, primary_key=primary_key, 
schema_extra=schema_extra)
 
 
@@ -42,6 +46,8 @@ from .context import Context
 
 # the debugger hangs on startup during plugin registration (reason unknown), 
hence this workaround
 import sys
+
 if not sys.argv.__contains__('startup'):
     from pydevlake.helpers import debugger
+
     debugger.init()
diff --git a/backend/python/pydevlake/pydevlake/model.py 
b/backend/python/pydevlake/pydevlake/model.py
index 4f12ea794..105b2aeca 100644
--- a/backend/python/pydevlake/pydevlake/model.py
+++ b/backend/python/pydevlake/pydevlake/model.py
@@ -128,7 +128,7 @@ class NoPKModel(RawDataOrigin):
 
 
 class ToolModel(ToolTable, NoPKModel):
-    connection_id: Optional[int] = Field(primary_key=True)
+    connection_id: Optional[int] = Field(primary_key=True, 
auto_increment=False)
 
     def domain_id(self):
         """
diff --git a/backend/server/services/remote/models/conversion.go 
b/backend/server/services/remote/models/conversion.go
index f5ec727ba..c3a3e0c6d 100644
--- a/backend/server/services/remote/models/conversion.go
+++ b/backend/server/services/remote/models/conversion.go
@@ -212,6 +212,10 @@ func getGormTag(schema utils.JsonObject, goType 
reflect.Type) string {
        if err == nil && primaryKey {
                gormTags = append(gormTags, "primaryKey")
        }
+       autoIncrement, err := utils.GetProperty[bool](schema, "autoIncrement")
+       if err == nil {
+               gormTags = append(gormTags, fmt.Sprintf("autoIncrement:%v", 
autoIncrement))
+       }
        if goType == stringType {
                maxLength, err := utils.GetProperty[float64](schema, 
"maxLength")
                maxLengthInt := int(maxLength)
diff --git a/backend/server/services/remote/models/migration.go 
b/backend/server/services/remote/models/migration.go
index c06b1d22f..e84b6e4bd 100644
--- a/backend/server/services/remote/models/migration.go
+++ b/backend/server/services/remote/models/migration.go
@@ -22,6 +22,7 @@ import (
        "github.com/apache/incubator-devlake/core/context"
        "github.com/apache/incubator-devlake/core/dal"
        "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/models"
        "github.com/apache/incubator-devlake/core/models/common"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -121,6 +122,9 @@ func (o CreateTableOperation) Execute(basicRes 
context.BasicRes) errors.Error {
        if err != nil {
                return err
        }
+       // uncomment to debug "modelDump" as needed
+       modelDump := models.DumpInfo(model.New())
+       _ = modelDump
        err = api.CallDB(db.AutoMigrate, model.New())
        if err != nil {
                return err

Reply via email to