fix(migrator) : Return all data tables
This commit is contained in:
parent
acde65b965
commit
587707bba0
@ -33,9 +33,6 @@ type ViewOption struct {
|
||||
Query *DB
|
||||
}
|
||||
|
||||
// ALLTables get all database tables
|
||||
const ALLTables = "migrator:all_tables"
|
||||
|
||||
type ColumnType interface {
|
||||
Name() string
|
||||
DatabaseTypeName() string
|
||||
@ -57,7 +54,7 @@ type Migrator interface {
|
||||
DropTable(dst ...interface{}) error
|
||||
HasTable(dst interface{}) bool
|
||||
RenameTable(oldName, newName interface{}) error
|
||||
GetTables(tables ...string) (tableList []string, err error)
|
||||
GetTables() (tableList []string, err error)
|
||||
|
||||
// Columns
|
||||
AddColumn(dst interface{}, field string) error
|
||||
|
@ -21,8 +21,6 @@ var (
|
||||
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
|
||||
@ -162,11 +160,8 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Migrator) GetTables(tables ...string) (tableList []string, err error) {
|
||||
if len(tables) == 1 && tables[0] == gorm.ALLTables {
|
||||
func (m Migrator) GetTables() (tableList []string, err 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 {
|
||||
|
@ -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] })
|
||||
|
||||
DB.Migrator().DropTable("user_speaks", "user_friends", "ccc")
|
||||
DB.Migrator().GetTables("user_speaks", "user_friends", "ccc")
|
||||
DB.Migrator().GetTables(gorm.ALLTables)
|
||||
DB.Migrator().GetTables()
|
||||
|
||||
if err := DB.Migrator().DropTable(allModels...); err != nil {
|
||||
t.Fatalf("Failed to drop table, got error %v", err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user