mirror of https://github.com/go-gorm/gorm.git
feat(migrator,migrator/migrator.go,tests/migrate_test.go) : Get multiple data tables for migrator. (#4841)
* feat(migrator,migrator/migrator.go,tests/migrate_test.go) : Get multiple data tables for migrator. * feat(migrator.go and migrator/migrator.go) : remove Table Struct replace with []string * fix(migrator) : Return all data tables * Update migrator.go * fix(migrator/migrator.go):remove var sql * feat(migrate_test.go/go.mod):update sqlserver,sqlite,postgres,pq version and add getTables test * fix(migrate_test.go):change GetTables Method Test,use intersection Co-authored-by: dino.ma <mashengjie03@baidu.com>
This commit is contained in:
parent
33bc56cbb5
commit
5e64ac7de9
|
@ -54,6 +54,7 @@ type Migrator interface {
|
|||
DropTable(dst ...interface{}) error
|
||||
HasTable(dst interface{}) bool
|
||||
RenameTable(oldName, newName interface{}) error
|
||||
GetTables() (tableList []string, err error)
|
||||
|
||||
// Columns
|
||||
AddColumn(dst interface{}, field string) error
|
||||
|
|
|
@ -155,6 +155,10 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (m Migrator) GetTables() (tableList []string, err error) {
|
||||
return tableList, m.DB.Raw("SELECT TABLE_NAME FROM information_schema.tables where TABLE_SCHEMA=?", m.CurrentDatabase()).Scan(&tableList).Error
|
||||
}
|
||||
|
||||
func (m Migrator) CreateTable(values ...interface{}) error {
|
||||
for _, value := range m.ReorderModels(values, false) {
|
||||
tx := m.DB.Session(&gorm.Session{})
|
||||
|
|
|
@ -5,11 +5,11 @@ go 1.14
|
|||
require (
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jinzhu/now v1.1.2
|
||||
github.com/lib/pq v1.10.3
|
||||
github.com/lib/pq v1.10.4
|
||||
gorm.io/driver/mysql v1.1.3
|
||||
gorm.io/driver/postgres v1.2.1
|
||||
gorm.io/driver/sqlite v1.2.3
|
||||
gorm.io/driver/sqlserver v1.2.0
|
||||
gorm.io/driver/postgres v1.2.2
|
||||
gorm.io/driver/sqlite v1.2.4
|
||||
gorm.io/driver/sqlserver v1.2.1
|
||||
gorm.io/gorm v1.22.2
|
||||
)
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ func TestMigrate(t *testing.T) {
|
|||
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
|
||||
|
||||
DB.Migrator().DropTable("user_speaks", "user_friends", "ccc")
|
||||
|
||||
if err := DB.Migrator().DropTable(allModels...); err != nil {
|
||||
|
@ -25,6 +24,23 @@ func TestMigrate(t *testing.T) {
|
|||
t.Fatalf("Failed to auto migrate, but got error %v", err)
|
||||
}
|
||||
|
||||
if tables, err := DB.Migrator().GetTables(); err != nil {
|
||||
t.Fatalf("Failed to get database all tables, but got error %v", err)
|
||||
} else {
|
||||
for _, t1 := range []string{"users", "accounts", "pets", "companies", "toys", "languages"} {
|
||||
hasTable := false
|
||||
for _, t2 := range tables {
|
||||
if t2 == t1 {
|
||||
hasTable = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasTable {
|
||||
t.Fatalf("Failed to get table %v when GetTables", t1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, m := range allModels {
|
||||
if !DB.Migrator().HasTable(m) {
|
||||
t.Fatalf("Failed to create table for %#v---", m)
|
||||
|
|
Loading…
Reference in New Issue