diff --git a/migrator/migrator.go b/migrator/migrator.go index 18b2593d..a98f7fe3 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -24,6 +24,10 @@ type Config struct { gorm.Dialector } +type GormDataTypeInterface interface { + GormDBDataType(*gorm.DB, *schema.Field) string +} + func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error) error { stmt := &gorm.Statement{DB: m.DB} if m.DB.Statement != nil { @@ -44,6 +48,13 @@ func (m Migrator) DataTypeOf(field *schema.Field) string { return field.DBDataType } + fieldValue := reflect.New(field.IndirectFieldType) + if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok { + if dataType := dataTyper.GormDBDataType(m.DB, field); dataType != "" { + return dataType + } + } + return m.Dialector.DataTypeOf(field) } diff --git a/schema/field.go b/schema/field.go index 854ec520..e0d49e2f 100644 --- a/schema/field.go +++ b/schema/field.go @@ -220,6 +220,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } } + if dataTyper, ok := fieldValue.Interface().(GormDataTypeInterface); ok { + field.DataType = DataType(dataTyper.GormDataType()) + } + if v, ok := field.TagSettings["AUTOCREATETIME"]; ok || (field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { if strings.ToUpper(v) == "NANO" { field.AutoCreateTime = UnixNanosecond diff --git a/schema/interfaces.go b/schema/interfaces.go new file mode 100644 index 00000000..f5d07843 --- /dev/null +++ b/schema/interfaces.go @@ -0,0 +1,23 @@ +package schema + +import "gorm.io/gorm/clause" + +type GormDataTypeInterface interface { + GormDataType() string +} + +type CreateClausesInterface interface { + CreateClauses() []clause.Interface +} + +type QueryClausesInterface interface { + QueryClauses() []clause.Interface +} + +type UpdateClausesInterface interface { + UpdateClauses() []clause.Interface +} + +type DeleteClausesInterface interface { + DeleteClauses() []clause.Interface +} diff --git a/schema/schema.go b/schema/schema.go index d2c4d08b..5b360f5e 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -42,22 +42,6 @@ type Schema struct { cacheStore *sync.Map } -type CreateClausesInterface interface { - CreateClauses() []clause.Interface -} - -type QueryClausesInterface interface { - QueryClauses() []clause.Interface -} - -type UpdateClausesInterface interface { - UpdateClauses() []clause.Interface -} - -type DeleteClausesInterface interface { - DeleteClauses() []clause.Interface -} - func (schema Schema) String() string { if schema.ModelType.Name() == "" { return fmt.Sprintf("%v(%v)", schema.Name, schema.Table) diff --git a/tests/tests_all.sh b/tests/tests_all.sh index 92a28f3b..affb1847 100755 --- a/tests/tests_all.sh +++ b/tests/tests_all.sh @@ -17,14 +17,21 @@ for dialect in "${dialects[@]}" ; do if [ "$GORM_VERBOSE" = "" ] then - DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 ./... - cd tests - DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 ./... + GORM_DIALECT=${dialect} go test $race -count=1 ./... + if [ -d tests ] + then + cd tests + GORM_DIALECT=${dialect} go test $race -count=1 ./... + cd .. + fi else - DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 -v ./... - cd tests - DEBUG=false GORM_DIALECT=${dialect} go test $race -count=1 -v ./... + GORM_DIALECT=${dialect} go test $race -count=1 -v ./... + if [ -d tests ] + then + cd tests + GORM_DIALECT=${dialect} go test $race -count=1 -v ./... + cd .. + fi fi - cd .. fi done