diff --git a/callbacks/update.go b/callbacks/update.go index 623d64fe..cfa8c86b 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -3,7 +3,6 @@ package callbacks import ( "reflect" "sort" - "time" "github.com/jinzhu/gorm" "github.com/jinzhu/gorm/clause" @@ -152,7 +151,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { if !stmt.DisableUpdateTime { for _, field := range stmt.Schema.FieldsByDBName { if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil { - now := time.Now() + now := stmt.DB.NowFunc() set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now}) assignValue(field, now) } @@ -168,7 +167,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { value, isZero := field.ValueOf(stmt.ReflectValue) if !stmt.DisableUpdateTime { if field.AutoUpdateTime > 0 { - value = time.Now() + value = stmt.DB.NowFunc() isZero = false } } diff --git a/dialects/mssql/migrator.go b/dialects/mssql/migrator.go index 4707a637..d1abd0e9 100644 --- a/dialects/mssql/migrator.go +++ b/dialects/mssql/migrator.go @@ -1,7 +1,10 @@ package mssql import ( + "fmt" + "github.com/jinzhu/gorm" + "github.com/jinzhu/gorm/clause" "github.com/jinzhu/gorm/migrator" ) @@ -20,6 +23,24 @@ func (m Migrator) HasTable(value interface{}) bool { return count > 0 } +func (m Migrator) HasColumn(value interface{}, field string) bool { + var count int64 + m.RunWithValue(value, func(stmt *gorm.Statement) error { + currentDatabase := m.DB.Migrator().CurrentDatabase() + name := field + if field := stmt.Schema.LookUpField(field); field != nil { + name = field.DBName + } + + return m.DB.Raw( + "SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_catalog = ? AND table_name = ? AND column_name = ?", + currentDatabase, stmt.Table, name, + ).Row().Scan(&count) + }) + + return count > 0 +} + func (m Migrator) HasIndex(value interface{}, name string) bool { var count int m.RunWithValue(value, func(stmt *gorm.Statement) error { @@ -35,6 +56,16 @@ func (m Migrator) HasIndex(value interface{}, name string) bool { return count > 0 } +func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error { + return m.RunWithValue(value, func(stmt *gorm.Statement) error { + + return m.DB.Exec( + "sp_rename @objname = ?, @newname = ?, @objtype = 'INDEX';", + fmt.Sprintf("%s.%s", stmt.Table, oldName), clause.Column{Name: newName}, + ).Error + }) +} + func (m Migrator) HasConstraint(value interface{}, name string) bool { var count int64 m.RunWithValue(value, func(stmt *gorm.Statement) error { diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index 8e309faf..3828c546 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -2,7 +2,6 @@ package mssql import ( "database/sql" - "database/sql/driver" "fmt" "regexp" "strconv" @@ -81,15 +80,6 @@ func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { var numericPlaceholder = regexp.MustCompile("@p(\\d+)") func (dialector Dialector) Explain(sql string, vars ...interface{}) string { - for idx, v := range vars { - if valuer, ok := v.(driver.Valuer); ok { - v, _ = valuer.Value() - } - - if v, ok := v.(bool); ok { - vars[idx] = strconv.FormatBool(v) - } - } return logger.ExplainSQL(sql, numericPlaceholder, `'`, vars...) } diff --git a/tests/tests_all.sh b/tests/tests_all.sh index 9435b2b1..243af787 100755 --- a/tests/tests_all.sh +++ b/tests/tests_all.sh @@ -11,9 +11,9 @@ for dialect in "${dialects[@]}" ; do if [ "$GORM_VERBOSE" = "" ] then - DEBUG=false GORM_DIALECT=${dialect} go test -race ./... + DEBUG=false GORM_DIALECT=${dialect} go test -race -count=1 ./... else - DEBUG=false GORM_DIALECT=${dialect} go test -race -v ./... + DEBUG=false GORM_DIALECT=${dialect} go test -race -count=1 -v ./... fi fi done