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) {
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",

24
main.go
View File

@ -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 {