feat(migrator,migrator/migrator.go,tests/migrate_test.go) : Get multiple data tables for migrator.

This commit is contained in:
dino.ma 2021-11-06 16:57:20 +08:00
parent 4c8810a848
commit f676d504f3
3 changed files with 25 additions and 1 deletions

View File

@ -33,6 +33,14 @@ type ViewOption struct {
Query *DB Query *DB
} }
// ALLTables get all database tables
const ALLTables = "migrator:all_tables"
// Table Database table list info
type Table struct {
TableName string `gorm:"column:TABLE_NAME"`
}
type ColumnType interface { type ColumnType interface {
Name() string Name() string
DatabaseTypeName() string DatabaseTypeName() string
@ -54,7 +62,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(tables ...string) (tableList []Table, err error)
// Columns // Columns
AddColumn(dst interface{}, field string) error AddColumn(dst interface{}, field string) error
DropColumn(dst interface{}, field string) error DropColumn(dst interface{}, field string) error

View File

@ -18,6 +18,13 @@ var (
regFullDataType = regexp.MustCompile(`[^\d]*(\d+)[^\d]?`) regFullDataType = regexp.MustCompile(`[^\d]*(\d+)[^\d]?`)
) )
const (
// allTableQuery query db table list
allTableQuery = "SELECT TABLE_NAME FROM information_schema.tables where TABLE_SCHEMA=?"
// tablesQuery query tables is has
tablesQuery = "SELECT TABLE_NAME FROM information_schema.tables where TABLE_SCHEMA=? and TABLE_NAME in (?)"
)
// Migrator m struct // Migrator m struct
type Migrator struct { type Migrator struct {
Config Config
@ -155,6 +162,13 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
return nil return nil
} }
func (m Migrator) GetTables(tables ...string) (tableList []gorm.Table, err error) {
if len(tables) == 1 && tables[0] == gorm.ALLTables {
return tableList, m.DB.Raw(allTableQuery, m.CurrentDatabase()).Scan(&tableList).Error
}
return tableList, m.DB.Raw(tablesQuery, m.CurrentDatabase(), tables).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

@ -16,6 +16,8 @@ func TestMigrate(t *testing.T) {
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")
DB.Migrator().GetTables("user_speaks", "user_friends", "ccc")
DB.Migrator().GetTables(gorm.ALLTables)
if err := DB.Migrator().DropTable(allModels...); err != nil { if err := DB.Migrator().DropTable(allModels...); err != nil {
t.Fatalf("Failed to drop table, got error %v", err) t.Fatalf("Failed to drop table, got error %v", err)