From 5e64ac7de9765319da7a588a13bc06d67f7416c9 Mon Sep 17 00:00:00 2001 From: "dino.ma" Date: Sat, 13 Nov 2021 14:03:33 +0800 Subject: [PATCH] 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 --- migrator.go | 1 + migrator/migrator.go | 4 ++++ tests/go.mod | 8 ++++---- tests/migrate_test.go | 18 +++++++++++++++++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/migrator.go b/migrator.go index 7dddcabf..2a8b4254 100644 --- a/migrator.go +++ b/migrator.go @@ -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 diff --git a/migrator/migrator.go b/migrator/migrator.go index 30586a8c..95a708de 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -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{}) diff --git a/tests/go.mod b/tests/go.mod index b4c5d79d..e321d3d8 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -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 ) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index f0467c5b..789a5e45 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -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)