From bb04a8b212a326e4f0fd283d179c6b3eed328628 Mon Sep 17 00:00:00 2001 From: Tristan Rice Date: Fri, 29 Jan 2016 15:01:46 -0800 Subject: [PATCH] Fixed TestIndexes --- cockroach.go | 30 ++++++++++++++++++++---------- main_test.go | 4 +++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cockroach.go b/cockroach.go index 828d7b0c..def2fcd7 100644 --- a/cockroach.go +++ b/cockroach.go @@ -2,6 +2,7 @@ package gorm import ( "fmt" + "log" "reflect" "time" ) @@ -32,6 +33,7 @@ func (cockroach) NewUniqueKey(scope *Scope) uint64 { scope.Err(err) return 0 } + defer rows.Close() var id int64 for rows.Next() { if err := rows.Scan(&id); err != nil { @@ -84,7 +86,10 @@ func (s cockroach) HasTable(scope *Scope, tableName string) bool { defer rows.Close() var name string for rows.Next() { - rows.Scan(&name) + if err := rows.Scan(&name); err != nil { + scope.Err(err) + return false + } if name == tableName { return true } @@ -101,7 +106,10 @@ func (s cockroach) HasColumn(scope *Scope, tableName string, columnName string) defer rows.Close() var column string for rows.Next() { - rows.Scan(&column) + if err := rows.Scan(&column); err != nil { + scope.Err(err) + return false + } if column == columnName { 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 { - /* - 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() if err != nil { scope.Err(err) return false } defer rows.Close() - var name string + + var table, name, column, direction string + var unique, storing bool + var seq int 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 { return true } @@ -132,7 +142,7 @@ func (s cockroach) HasIndex(scope *Scope, tableName string, indexName string) bo } 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 { diff --git a/main_test.go b/main_test.go index a1505126..28b110d1 100644 --- a/main_test.go +++ b/main_test.go @@ -467,7 +467,9 @@ func TestJoins(t *testing.T) { DB.Save(&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 { t.Errorf("Should find all two emails with Join") }