Merge pull request #184 from shirou/master

Add DropTableIfExists
This commit is contained in:
Jinzhu 2014-08-06 16:42:03 +08:00
commit 131b504941
3 changed files with 12 additions and 0 deletions

View File

@ -124,6 +124,9 @@ db.CreateTable(User{})
// Drop table
db.DropTable(User{})
// Drop table if exists
db.DropTableIfExists(User{})
// Automating Migration
db.AutoMigrate(User{})

View File

@ -340,6 +340,10 @@ func (s *DB) DropTable(value interface{}) *DB {
return s.clone().NewScope(value).dropTable().db
}
func (s *DB) DropTableIfExists(value interface{}) *DB {
return s.clone().NewScope(value).dropTableIfExists().db
}
func (s *DB) AutoMigrate(value interface{}) *DB {
return s.clone().NewScope(value).autoMigrate().db
}

View File

@ -495,6 +495,11 @@ func (scope *Scope) dropTable() *Scope {
return scope
}
func (scope *Scope) dropTableIfExists() *Scope {
scope.Raw(fmt.Sprintf("DROP TABLE IF EXISTS %v", scope.QuotedTableName())).Exec()
return scope
}
func (scope *Scope) modifyColumn(column string, typ string) {
scope.Raw(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", scope.QuotedTableName(), scope.Quote(column), typ)).Exec()
}