From b929a082d7bf6543e65c250591f50fe405f693f9 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Jul 2014 12:02:03 +0800 Subject: [PATCH] Fix remove index for sqlite and postgres --- common_dialect.go | 4 ++++ dialect.go | 1 + migration_test.go | 12 ++++++++++-- mysql.go | 4 ++++ postgres.go | 4 ++++ scope_private.go | 2 +- sqlite3.go | 4 ++++ structs_test.go | 2 +- 8 files changed, 29 insertions(+), 4 deletions(-) diff --git a/common_dialect.go b/common_dialect.go index 7d5d76fa..ec5fe7ec 100644 --- a/common_dialect.go +++ b/common_dialect.go @@ -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() +} diff --git a/dialect.go b/dialect.go index 20ba32d4..702660ae 100644 --- a/dialect.go +++ b/dialect.go @@ -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 { diff --git a/migration_test.go b/migration_test.go index 3315a031..0d3ecee2 100644 --- a/migration_test.go +++ b/migration_test.go @@ -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) } @@ -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: "user1@example.comiii"}, {Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error) - if db.Debug().Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.comiii"}, {Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error == nil { + if db.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.comiii"}, {Email: "user1@example.com"}, {Email: "user1@example.com"}}}).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: "user1@example.com"}, {Email: "user1@example.com"}}}).Error != nil { + t.Errorf("Should be able to create duplicated emails after remove unique index") + } } diff --git a/mysql.go b/mysql.go index 366a6775..afb748a7 100644 --- a/mysql.go +++ b/mysql.go @@ -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() +} diff --git a/postgres.go b/postgres.go index 14b4a329..b91b1309 100644 --- a/postgres.go +++ b/postgres.go @@ -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 diff --git a/scope_private.go b/scope_private.go index 0fe406c0..da851fb1 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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 { diff --git a/sqlite3.go b/sqlite3.go index 00408782..795ee2bd 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -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() +} diff --git a/structs_test.go b/structs_test.go index c183c3d5..5445bca4 100644 --- a/structs_test.go +++ b/structs_test.go @@ -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 }