Fixed TestIndexes

This commit is contained in:
Tristan Rice 2016-01-29 15:01:46 -08:00
parent 597b31e969
commit d334c5ec52
3 changed files with 26 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package gorm
import ( import (
"fmt" "fmt"
"log"
"reflect" "reflect"
"time" "time"
) )
@ -32,6 +33,7 @@ func (cockroach) NewUniqueKey(scope *Scope) uint64 {
scope.Err(err) scope.Err(err)
return 0 return 0
} }
defer rows.Close()
var id int64 var id int64
for rows.Next() { for rows.Next() {
if err := rows.Scan(&id); err != nil { if err := rows.Scan(&id); err != nil {
@ -84,7 +86,10 @@ func (s cockroach) HasTable(scope *Scope, tableName string) bool {
defer rows.Close() defer rows.Close()
var name string var name string
for rows.Next() { for rows.Next() {
rows.Scan(&name) if err := rows.Scan(&name); err != nil {
scope.Err(err)
return false
}
if name == tableName { if name == tableName {
return true return true
} }
@ -101,7 +106,10 @@ func (s cockroach) HasColumn(scope *Scope, tableName string, columnName string)
defer rows.Close() defer rows.Close()
var column string var column string
for rows.Next() { for rows.Next() {
rows.Scan(&column) if err := rows.Scan(&column); err != nil {
scope.Err(err)
return false
}
if column == columnName { if column == columnName {
return true return true
} }
@ -110,20 +118,22 @@ func (s cockroach) HasColumn(scope *Scope, tableName string, columnName string)
} }
func (s cockroach) HasIndex(scope *Scope, tableName string, indexName string) bool { func (s cockroach) HasIndex(scope *Scope, tableName string, indexName string) bool {
/*
var count int
s.RawScanInt(scope, &count, fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND sql LIKE '%%INDEX %v ON%%'", indexName), tableName)
return count > 0
*/
rows, err := scope.NewDB().Raw(fmt.Sprintf("show index from %s", tableName)).Rows() rows, err := scope.NewDB().Raw(fmt.Sprintf("show index from %s", tableName)).Rows()
if err != nil { if err != nil {
scope.Err(err) scope.Err(err)
return false return false
} }
defer rows.Close() defer rows.Close()
var name string
var table, name, column, direction string
var unique, storing bool
var seq int
for rows.Next() { for rows.Next() {
rows.Scan(nil, &name) if err := rows.Scan(&table, &name, &unique, &seq, &column, &direction, &storing); err != nil {
scope.Err(err)
return false
}
log.Printf("HasIndex %#v %#v %#v ", table, name, indexName)
if name == indexName { if name == indexName {
return true return true
} }
@ -132,7 +142,7 @@ func (s cockroach) HasIndex(scope *Scope, tableName string, indexName string) bo
} }
func (cockroach) RemoveIndex(scope *Scope, indexName string) { func (cockroach) RemoveIndex(scope *Scope, indexName string) {
scope.Err(scope.NewDB().Raw(fmt.Sprintf("DROP INDEX %v@%v", scope.QuotedTableName(), indexName)).Error) scope.Err(scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v@%v", scope.TableName(), indexName)).Error)
} }
func (s cockroach) CurrentDatabase(scope *Scope) string { func (s cockroach) CurrentDatabase(scope *Scope) string {

View File

@ -467,7 +467,9 @@ func TestJoins(t *testing.T) {
DB.Save(&user) DB.Save(&user)
var result User var result User
DB.Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins").First(&result) if err := DB.Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins").First(&result).Error; err != nil {
t.Errorf("Error while joining: %s", err)
}
if result.Name != "joins" || result.Id != user.Id { if result.Name != "joins" || result.Id != user.Id {
t.Errorf("Should find all two emails with Join") t.Errorf("Should find all two emails with Join")
} }

View File

@ -4,6 +4,7 @@ import (
"database/sql/driver" "database/sql/driver"
"errors" "errors"
"fmt" "fmt"
"log"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -361,6 +362,8 @@ func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
// Trace print sql log // Trace print sql log
func (scope *Scope) Trace(t time.Time) { func (scope *Scope) Trace(t time.Time) {
if len(scope.Sql) > 0 { if len(scope.Sql) > 0 {
// TODO(d4l3k): Remove this line
log.Println("sql", scope.Sql, scope.SqlVars)
scope.db.slog(scope.Sql, t, scope.SqlVars...) scope.db.slog(scope.Sql, t, scope.SqlVars...)
} }
} }