From 5ad93061467d598e662ece88c4f811ff0040b47b Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Tue, 5 Aug 2014 23:14:40 +0900 Subject: [PATCH 1/2] Add DropTableIfExists. --- main.go | 4 ++++ scope_private.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/main.go b/main.go index a13bf6f2..abea9263 100644 --- a/main.go +++ b/main.go @@ -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 } diff --git a/scope_private.go b/scope_private.go index ca6b7eb5..4f25532a 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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() } From b4fc3d97536b0d40f94806b14bd1569b0f6affe5 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Tue, 5 Aug 2014 23:16:22 +0900 Subject: [PATCH 2/2] add document about DropTableIfExists. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index fdb86396..11ced96f 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,9 @@ db.CreateTable(User{}) // Drop table db.DropTable(User{}) +// Drop table if exists +db.DropTableIfExists(User{}) + // Automating Migration db.AutoMigrate(User{})