forked from mirror/gorm
Fix remove index for sqlite and postgres
This commit is contained in:
parent
cb7d545ac0
commit
b929a082d7
|
@ -98,3 +98,7 @@ func (s *commonDialect) HasColumn(scope *Scope, tableName string, columnName str
|
||||||
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
return count > 0
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *commonDialect) RemoveIndex(scope *Scope, indexName string) {
|
||||||
|
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Dialect interface {
|
||||||
Quote(key string) string
|
Quote(key string) string
|
||||||
HasTable(scope *Scope, tableName string) bool
|
HasTable(scope *Scope, tableName string) bool
|
||||||
HasColumn(scope *Scope, tableName string, columnName string) bool
|
HasColumn(scope *Scope, tableName string, columnName string) bool
|
||||||
|
RemoveIndex(scope *Scope, indexName string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDialect(driver string) Dialect {
|
func NewDialect(driver string) Dialect {
|
||||||
|
|
|
@ -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 {
|
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)
|
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 {
|
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)
|
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)
|
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.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.comiii"}, {Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error == nil {
|
||||||
if db.Debug().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")
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
4
mysql.go
4
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)
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
return count > 0
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *mysql) RemoveIndex(scope *Scope, indexName string) {
|
||||||
|
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
|
||||||
|
}
|
||||||
|
|
|
@ -89,6 +89,10 @@ func (s *postgres) HasColumn(scope *Scope, tableName string, columnName string)
|
||||||
return count > 0
|
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{})
|
var hstoreType = reflect.TypeOf(Hstore{})
|
||||||
|
|
||||||
type Hstore map[string]*string
|
type Hstore map[string]*string
|
||||||
|
|
|
@ -445,7 +445,7 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) removeIndex(indexName 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 {
|
func (scope *Scope) autoMigrate() *Scope {
|
||||||
|
|
|
@ -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)
|
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
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sqlite3) RemoveIndex(scope *Scope, indexName string) {
|
||||||
|
scope.Raw(fmt.Sprintf("DROP INDEX %v", indexName)).Exec()
|
||||||
|
}
|
||||||
|
|
|
@ -101,7 +101,7 @@ type CreditCard struct {
|
||||||
type Email struct {
|
type Email struct {
|
||||||
Id int16
|
Id int16
|
||||||
UserId int
|
UserId int
|
||||||
Email string `sql:"type:varchar(100); unique"`
|
Email string `sql:"type:varchar(100);"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue