fix(migrator) : Return all data tables

This commit is contained in:
dino.ma 2021-11-10 19:58:16 +08:00
parent acde65b965
commit 587707bba0
3 changed files with 4 additions and 13 deletions

View File

@ -33,9 +33,6 @@ type ViewOption struct {
Query *DB Query *DB
} }
// ALLTables get all database tables
const ALLTables = "migrator:all_tables"
type ColumnType interface { type ColumnType interface {
Name() string Name() string
DatabaseTypeName() string DatabaseTypeName() string
@ -57,7 +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(tables ...string) (tableList []string, err error) GetTables() (tableList []string, err error)
// Columns // Columns
AddColumn(dst interface{}, field string) error AddColumn(dst interface{}, field string) error

View File

@ -21,8 +21,6 @@ var (
const ( const (
// allTableQuery query db table list // allTableQuery query db table list
allTableQuery = "SELECT TABLE_NAME FROM information_schema.tables where TABLE_SCHEMA=?" 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
@ -162,11 +160,8 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
return nil return nil
} }
func (m Migrator) GetTables(tables ...string) (tableList []string, err error) { func (m Migrator) GetTables() (tableList []string, 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(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 {

View File

@ -16,8 +16,7 @@ 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()
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)