diff --git a/dialects/dialect.go b/dialects/dialect.go index 325836b4..b3d374cc 100644 --- a/dialects/dialect.go +++ b/dialects/dialect.go @@ -43,6 +43,7 @@ type Dialect interface { Init(*URI) error URI() *URI SQLType(*schemas.Column) string + Alias(string) string // return what a sql type's alias of FormatBytes(b []byte) string Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error) @@ -80,6 +81,11 @@ type Base struct { quoter schemas.Quoter } +// Alias returned col itself +func (db *Base) Alias(col string) string { + return col +} + // Quoter returns the current database Quoter func (db *Base) Quoter() schemas.Quoter { return db.quoter diff --git a/dialects/mysql.go b/dialects/mysql.go index a341ce05..da19b820 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -190,6 +190,21 @@ func (db *mysql) Init(uri *URI) error { return db.Base.Init(db, uri) } +var ( + mysqlColAliases = map[string]string{ + "numeric": "decimal", + } +) + +// Alias returns a alias of column +func (db *mysql) Alias(col string) string { + v, ok := mysqlColAliases[strings.ToLower(col)] + if ok { + return v + } + return col +} + func (db *mysql) Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error) { rows, err := queryer.QueryContext(ctx, "SELECT @@VERSION") if err != nil { diff --git a/dialects/postgres.go b/dialects/postgres.go index fd6d871c..9f3c7275 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -778,12 +778,24 @@ var ( var ( // DefaultPostgresSchema default postgres schema DefaultPostgresSchema = "public" + postgresColAliases = map[string]string{ + "numeric": "decimal", + } ) type postgres struct { Base } +// Alias returns a alias of column +func (db *postgres) Alias(col string) string { + v, ok := postgresColAliases[strings.ToLower(col)] + if ok { + return v + } + return col +} + func (db *postgres) Init(uri *URI) error { db.quoter = postgresQuoter return db.Base.Init(db, uri) diff --git a/integrations/session_schema_test.go b/integrations/session_schema_test.go index 28c75119..9cbebcbf 100644 --- a/integrations/session_schema_test.go +++ b/integrations/session_schema_test.go @@ -286,6 +286,19 @@ func TestSyncTable3(t *testing.T) { } } +func TestSyncTable4(t *testing.T) { + type SyncTable6 struct { + Id int64 + Qty float64 `xorm:"numeric(36,2)"` + } + + assert.NoError(t, PrepareEngine()) + + assert.NoError(t, testEngine.Sync2(new(SyncTable6))) + + assert.NoError(t, testEngine.Sync2(new(SyncTable6))) +} + func TestIsTableExist(t *testing.T) { assert.NoError(t, PrepareEngine()) diff --git a/schemas/type.go b/schemas/type.go index 3846b5ee..f49348be 100644 --- a/schemas/type.go +++ b/schemas/type.go @@ -368,3 +368,9 @@ func SQLType2Type(st SQLType) reflect.Type { return reflect.TypeOf("") } } + +// SQLTypeName returns sql type name +func SQLTypeName(tp string) string { + fields := strings.Split(tp, "(") + return fields[0] +} diff --git a/session_schema.go b/session_schema.go index 7d36ae7f..7cfcb626 100644 --- a/session_schema.go +++ b/session_schema.go @@ -336,8 +336,10 @@ func (session *Session) Sync2(beans ...interface{}) error { } } else { if !(strings.HasPrefix(curType, expectedType) && curType[len(expectedType)] == '(') { - engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s", - tbNameWithSchema, col.Name, curType, expectedType) + if !strings.EqualFold(schemas.SQLTypeName(curType), engine.dialect.Alias(schemas.SQLTypeName(expectedType))) { + engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s", + tbNameWithSchema, col.Name, curType, expectedType) + } } } } else if expectedType == schemas.Varchar {