mirror of https://github.com/go-gorm/gorm.git
Fix mssql rename index, has column
This commit is contained in:
parent
2b56fa0472
commit
58bc0f51c1
|
@ -3,7 +3,6 @@ package callbacks
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/jinzhu/gorm/clause"
|
"github.com/jinzhu/gorm/clause"
|
||||||
|
@ -152,7 +151,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
||||||
if !stmt.DisableUpdateTime {
|
if !stmt.DisableUpdateTime {
|
||||||
for _, field := range stmt.Schema.FieldsByDBName {
|
for _, field := range stmt.Schema.FieldsByDBName {
|
||||||
if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil {
|
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})
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now})
|
||||||
assignValue(field, now)
|
assignValue(field, now)
|
||||||
}
|
}
|
||||||
|
@ -168,7 +167,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
||||||
value, isZero := field.ValueOf(stmt.ReflectValue)
|
value, isZero := field.ValueOf(stmt.ReflectValue)
|
||||||
if !stmt.DisableUpdateTime {
|
if !stmt.DisableUpdateTime {
|
||||||
if field.AutoUpdateTime > 0 {
|
if field.AutoUpdateTime > 0 {
|
||||||
value = time.Now()
|
value = stmt.DB.NowFunc()
|
||||||
isZero = false
|
isZero = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package mssql
|
package mssql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/jinzhu/gorm/clause"
|
||||||
"github.com/jinzhu/gorm/migrator"
|
"github.com/jinzhu/gorm/migrator"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,6 +23,24 @@ func (m Migrator) HasTable(value interface{}) bool {
|
||||||
return count > 0
|
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 {
|
func (m Migrator) HasIndex(value interface{}, name string) bool {
|
||||||
var count int
|
var count int
|
||||||
m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
||||||
|
@ -35,6 +56,16 @@ func (m Migrator) HasIndex(value interface{}, name string) bool {
|
||||||
return count > 0
|
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 {
|
func (m Migrator) HasConstraint(value interface{}, name string) bool {
|
||||||
var count int64
|
var count int64
|
||||||
m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package mssql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"database/sql/driver"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -81,15 +80,6 @@ func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
|
||||||
var numericPlaceholder = regexp.MustCompile("@p(\\d+)")
|
var numericPlaceholder = regexp.MustCompile("@p(\\d+)")
|
||||||
|
|
||||||
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
|
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...)
|
return logger.ExplainSQL(sql, numericPlaceholder, `'`, vars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,9 @@ for dialect in "${dialects[@]}" ; do
|
||||||
|
|
||||||
if [ "$GORM_VERBOSE" = "" ]
|
if [ "$GORM_VERBOSE" = "" ]
|
||||||
then
|
then
|
||||||
DEBUG=false GORM_DIALECT=${dialect} go test -race ./...
|
DEBUG=false GORM_DIALECT=${dialect} go test -race -count=1 ./...
|
||||||
else
|
else
|
||||||
DEBUG=false GORM_DIALECT=${dialect} go test -race -v ./...
|
DEBUG=false GORM_DIALECT=${dialect} go test -race -count=1 -v ./...
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
Loading…
Reference in New Issue