From 33ded8b2b582c48481768bcf397e1bb4d92a0bc6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 12 Jun 2021 16:05:36 +0800 Subject: [PATCH] Add database alias table and fix wrong warning --- dialects/dialect.go | 6 ++++++ dialects/mysql.go | 15 +++++++++++++++ dialects/postgres.go | 12 ++++++++++++ integrations/session_schema_test.go | 13 +++++++++++++ schemas/type.go | 6 ++++++ session_schema.go | 6 ++++-- 6 files changed, 56 insertions(+), 2 deletions(-) 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 a169b901..b0ea7442 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -188,6 +188,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 52c88567..be391689 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -777,12 +777,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 fc02f015..37f94216 100644 --- a/schemas/type.go +++ b/schemas/type.go @@ -365,3 +365,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 { -- 2.40.1