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:
dino.ma 2021-11-13 14:03:33 +08:00 committed by GitHub
parent 33bc56cbb5
commit 5e64ac7de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 5 deletions

View File

@ -54,6 +54,7 @@ type Migrator interface {
DropTable(dst ...interface{}) error DropTable(dst ...interface{}) error
HasTable(dst interface{}) bool HasTable(dst interface{}) bool
RenameTable(oldName, newName interface{}) error RenameTable(oldName, newName interface{}) error
GetTables() (tableList []string, err error)
// Columns // Columns
AddColumn(dst interface{}, field string) error AddColumn(dst interface{}, field string) error

View File

@ -155,6 +155,10 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
return nil 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 { func (m Migrator) CreateTable(values ...interface{}) error {
for _, value := range m.ReorderModels(values, false) { for _, value := range m.ReorderModels(values, false) {
tx := m.DB.Session(&gorm.Session{}) tx := m.DB.Session(&gorm.Session{})

View File

@ -5,11 +5,11 @@ go 1.14
require ( require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/jinzhu/now v1.1.2 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/mysql v1.1.3
gorm.io/driver/postgres v1.2.1 gorm.io/driver/postgres v1.2.2
gorm.io/driver/sqlite v1.2.3 gorm.io/driver/sqlite v1.2.4
gorm.io/driver/sqlserver v1.2.0 gorm.io/driver/sqlserver v1.2.1
gorm.io/gorm v1.22.2 gorm.io/gorm v1.22.2
) )

View File

@ -14,7 +14,6 @@ func TestMigrate(t *testing.T) {
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}} allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] }) rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
DB.Migrator().DropTable("user_speaks", "user_friends", "ccc") DB.Migrator().DropTable("user_speaks", "user_friends", "ccc")
if err := DB.Migrator().DropTable(allModels...); err != nil { 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) 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 { for _, m := range allModels {
if !DB.Migrator().HasTable(m) { if !DB.Migrator().HasTable(m) {
t.Fatalf("Failed to create table for %#v---", m) t.Fatalf("Failed to create table for %#v---", m)