Fix tests with go v1.3

This commit is contained in:
Jinzhu 2014-07-02 11:56:37 +08:00
parent 1c1df2318c
commit 325e6c2ef1
3 changed files with 15 additions and 12 deletions

View File

@ -176,6 +176,9 @@ func (scope *Scope) AddToVars(value interface{}) string {
} }
// TableName get table name // TableName get table name
var pluralMapKeys = []*regexp.Regexp{regexp.MustCompile("ch$"), regexp.MustCompile("ss$"), regexp.MustCompile("sh$"), regexp.MustCompile("day$"), regexp.MustCompile("y$"), regexp.MustCompile("x$"), regexp.MustCompile("([^s])s?$")}
var pluralMapValues = []string{"ches", "sses", "shes", "days", "ies", "xes", "${1}s"}
func (scope *Scope) TableName() string { func (scope *Scope) TableName() string {
if scope.Search != nil && len(scope.Search.TableName) > 0 { if scope.Search != nil && len(scope.Search.TableName) > 0 {
return scope.Search.TableName return scope.Search.TableName
@ -201,11 +204,9 @@ func (scope *Scope) TableName() string {
str := toSnake(data.Type().Name()) str := toSnake(data.Type().Name())
if !scope.db.parent.singularTable { if !scope.db.parent.singularTable {
pluralMap := map[string]string{"ch": "ches", "ss": "sses", "sh": "shes", "day": "days", "y": "ies", "x": "xes", "s?": "s"} for index, reg := range pluralMapKeys {
for key, value := range pluralMap {
reg := regexp.MustCompile(key + "$")
if reg.MatchString(str) { if reg.MatchString(str) {
return reg.ReplaceAllString(str, value) return reg.ReplaceAllString(str, pluralMapValues[index])
} }
} }
} }

View File

@ -450,13 +450,16 @@ func (scope *Scope) removeIndex(indexName string) {
} }
func (scope *Scope) autoMigrate() *Scope { func (scope *Scope) autoMigrate() *Scope {
if !scope.Dialect().HasTable(scope, scope.TableName()) { tableName := scope.TableName()
quotedTableName := scope.QuotedTableName()
if !scope.Dialect().HasTable(scope, tableName) {
scope.createTable() scope.createTable()
} else { } else {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if !scope.Dialect().HasColumn(scope, scope.TableName(), field.DBName) { if !scope.Dialect().HasColumn(scope, tableName, field.DBName) {
if len(field.SqlTag) > 0 && !field.IsIgnored { if len(field.SqlTag) > 0 && !field.IsIgnored {
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", scope.QuotedTableName(), field.DBName, field.SqlTag)).Exec() scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, field.SqlTag)).Exec()
} }
} }
} }

View File

@ -62,13 +62,12 @@ func (s *sqlite3) Quote(key string) string {
func (s *sqlite3) HasTable(scope *Scope, tableName string) bool { func (s *sqlite3) HasTable(scope *Scope, tableName string) bool {
var count int var count int
newScope := scope.New(nil) scope.DB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='%v';", tableName)).Scan(&count)
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=%v", newScope.AddToVars(tableName)))
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
return count > 0 return count > 0
} }
func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) bool { func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) bool {
_, err := scope.DB().Exec(fmt.Sprintf("SELECT %v FROM %v LIMIT 1", columnName, tableName)) var count int
return err == nil scope.DB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = '%v' AND (sql LIKE '%%(\"%v\" %%' OR sql LIKE '%%,\"%v\" %%');\n", tableName, columnName, columnName)).Scan(&count)
return count > 0
} }