From 1d5f5f43f37a19d452a920447108de6e9e7b5e96 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 26 Aug 2015 11:54:07 +0800 Subject: [PATCH] Support create/drop multiple tables --- association_test.go | 9 ++------- main.go | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/association_test.go b/association_test.go index dfda46a5..fa018bdd 100644 --- a/association_test.go +++ b/association_test.go @@ -6,13 +6,8 @@ import ( ) func TestHasOneAndHasManyAssociation(t *testing.T) { - DB.DropTable(Category{}) - DB.DropTable(Post{}) - DB.DropTable(Comment{}) - - DB.CreateTable(Category{}) - DB.CreateTable(Post{}) - DB.CreateTable(Comment{}) + DB.DropTable(Category{}, Post{}, Comment{}) + DB.CreateTable(Category{}, Post{}, Comment{}) post := Post{ Title: "post 1", diff --git a/main.go b/main.go index 50aeb49c..657342e9 100644 --- a/main.go +++ b/main.go @@ -373,16 +373,28 @@ func (s *DB) RecordNotFound() bool { } // Migrations -func (s *DB) CreateTable(value interface{}) *DB { - return s.clone().NewScope(value).createTable().db +func (s *DB) CreateTable(values ...interface{}) *DB { + db := s.clone() + for _, value := range values { + db = db.NewScope(value).createTable().db + } + return db } -func (s *DB) DropTable(value interface{}) *DB { - return s.clone().NewScope(value).dropTable().db +func (s *DB) DropTable(values ...interface{}) *DB { + db := s.clone() + for _, value := range values { + db = db.NewScope(value).dropTable().db + } + return db } -func (s *DB) DropTableIfExists(value interface{}) *DB { - return s.clone().NewScope(value).dropTableIfExists().db +func (s *DB) DropTableIfExists(values ...interface{}) *DB { + db := s.clone() + for _, value := range values { + db = db.NewScope(value).dropTableIfExists().db + } + return db } func (s *DB) HasTable(value interface{}) bool {