From d5819023e79a62eb3623a854e762b35e860764a2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 25 Dec 2015 10:35:48 +0800 Subject: [PATCH] added varchar size increment auto sync for mysql in Sync2 --- session.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/session.go b/session.go index 07d3af1e..9d00b7e7 100644 --- a/session.go +++ b/session.go @@ -4011,10 +4011,26 @@ func (s *Session) Sync2(beans ...interface{}) error { engine.LogWarnf("Table %s column %s db type is %s, struct type is %s\n", table.Name, col.Name, curType, expectedType) } + } else if strings.HasPrefix(curType, core.Varchar) && strings.HasPrefix(expectedType, core.Varchar) { + if engine.dialect.DBType() == core.MYSQL { + if oriCol.Length < col.Length { + engine.LogInfof("Table %s column %s change type from varchar(%d) to varchar(%d)\n", + table.Name, col.Name, oriCol.Length, col.Length) + _, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col)) + } + } } else { engine.LogWarnf("Table %s column %s db type is %s, struct type is %s", table.Name, col.Name, curType, expectedType) } + } else if expectedType == core.Varchar { + if engine.dialect.DBType() == core.MYSQL { + if oriCol.Length < col.Length { + engine.LogInfof("Table %s column %s change type from varchar(%d) to varchar(%d)\n", + table.Name, col.Name, oriCol.Length, col.Length) + _, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col)) + } + } } if col.Default != oriCol.Default { engine.LogWarnf("Table %s Column %s db default is %s, struct default is %s",