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") } diff --git a/scope.go b/scope.go index 29354133..9a4b263e 100644 --- a/scope.go +++ b/scope.go @@ -4,6 +4,7 @@ import ( "database/sql/driver" "errors" "fmt" + "log" "regexp" "strings" "time" @@ -361,6 +362,8 @@ func (scope *Scope) InstanceGet(name string) (interface{}, bool) { // Trace print sql log func (scope *Scope) Trace(t time.Time) { 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...) } }