Skip to content

Commit

Permalink
Fix remove index for sqlite and postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 29, 2014
1 parent cb7d545 commit b929a08
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions common_dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ func (s *commonDialect) HasColumn(scope *Scope, tableName string, columnName str
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
return count > 0
}

func (s *commonDialect) RemoveIndex(scope *Scope, indexName string) {
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
}
1 change: 1 addition & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Dialect interface {
Quote(key string) string
HasTable(scope *Scope, tableName string) bool
HasColumn(scope *Scope, tableName string, columnName string) bool
RemoveIndex(scope *Scope, indexName string)
}

func NewDialect(driver string) Dialect {
Expand Down
12 changes: 10 additions & 2 deletions migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestIndexes(t *testing.T) {
if err := db.Model(&Email{}).AddIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil {
t.Errorf("Got error when tried to create index: %+v", err)
}

if err := db.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil {
t.Errorf("Got error when tried to remove index: %+v", err)
}
Expand All @@ -76,8 +77,15 @@ func TestIndexes(t *testing.T) {
t.Errorf("Got error when tried to create index: %+v", err)
}

fmt.Println(db.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "[email protected]"}, {Email: "[email protected]"}, {Email: "[email protected]"}}}).Error)
if db.Debug().Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "[email protected]"}, {Email: "[email protected]"}, {Email: "[email protected]"}}}).Error == nil {
if db.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "[email protected]"}, {Email: "[email protected]"}, {Email: "[email protected]"}}}).Error == nil {
t.Errorf("Should get to create duplicate record when having unique index")
}

if err := db.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil {
t.Errorf("Got error when tried to remove index: %+v", err)
}

if db.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "[email protected]"}, {Email: "[email protected]"}}}).Error != nil {
t.Errorf("Should be able to create duplicated emails after remove unique index")
}
}
4 changes: 4 additions & 0 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ func (s *mysql) HasColumn(scope *Scope, tableName string, columnName string) boo
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
return count > 0
}

func (s *mysql) RemoveIndex(scope *Scope, indexName string) {
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
}
4 changes: 4 additions & 0 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (s *postgres) HasColumn(scope *Scope, tableName string, columnName string)
return count > 0
}

func (s *postgres) RemoveIndex(scope *Scope, indexName string) {
scope.Raw(fmt.Sprintf("DROP INDEX %v", indexName)).Exec()
}

var hstoreType = reflect.TypeOf(Hstore{})

type Hstore map[string]*string
Expand Down
2 changes: 1 addition & 1 deletion scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
}

func (scope *Scope) removeIndex(indexName string) {
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
scope.Dialect().RemoveIndex(scope, indexName)
}

func (scope *Scope) autoMigrate() *Scope {
Expand Down
4 changes: 4 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) b
scope.DB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = '%v' AND (sql LIKE '%%(\"%v\" %%' OR sql LIKE '%%,\"%v\" %%' OR sql LIKE '%%( %v %%' OR sql LIKE '%%, %v %%');\n", tableName, columnName, columnName, columnName, columnName)).Scan(&count)
return count > 0
}

func (s *sqlite3) RemoveIndex(scope *Scope, indexName string) {
scope.Raw(fmt.Sprintf("DROP INDEX %v", indexName)).Exec()
}
2 changes: 1 addition & 1 deletion structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type CreditCard struct {
type Email struct {
Id int16
UserId int
Email string `sql:"type:varchar(100); unique"`
Email string `sql:"type:varchar(100);"`
CreatedAt time.Time
UpdatedAt time.Time
}
Expand Down

0 comments on commit b929a08

Please sign in to comment.