Support create/drop multiple tables

This commit is contained in:
Jinzhu 2015-08-26 11:54:07 +08:00
parent 9303e070c8
commit 1d5f5f43f3
2 changed files with 20 additions and 13 deletions

View File

@ -6,13 +6,8 @@ import (
) )
func TestHasOneAndHasManyAssociation(t *testing.T) { func TestHasOneAndHasManyAssociation(t *testing.T) {
DB.DropTable(Category{}) DB.DropTable(Category{}, Post{}, Comment{})
DB.DropTable(Post{}) DB.CreateTable(Category{}, Post{}, Comment{})
DB.DropTable(Comment{})
DB.CreateTable(Category{})
DB.CreateTable(Post{})
DB.CreateTable(Comment{})
post := Post{ post := Post{
Title: "post 1", Title: "post 1",

24
main.go
View File

@ -373,16 +373,28 @@ func (s *DB) RecordNotFound() bool {
} }
// Migrations // Migrations
func (s *DB) CreateTable(value interface{}) *DB { func (s *DB) CreateTable(values ...interface{}) *DB {
return s.clone().NewScope(value).createTable().db db := s.clone()
for _, value := range values {
db = db.NewScope(value).createTable().db
}
return db
} }
func (s *DB) DropTable(value interface{}) *DB { func (s *DB) DropTable(values ...interface{}) *DB {
return s.clone().NewScope(value).dropTable().db db := s.clone()
for _, value := range values {
db = db.NewScope(value).dropTable().db
}
return db
} }
func (s *DB) DropTableIfExists(value interface{}) *DB { func (s *DB) DropTableIfExists(values ...interface{}) *DB {
return s.clone().NewScope(value).dropTableIfExists().db db := s.clone()
for _, value := range values {
db = db.NewScope(value).dropTableIfExists().db
}
return db
} }
func (s *DB) HasTable(value interface{}) bool { func (s *DB) HasTable(value interface{}) bool {