Skip to content

Commit

Permalink
fix issue #2552 (#3300)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Feb 5, 2024
1 parent cc79d23 commit 1456856
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
92 changes: 92 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_issue_test.go
Expand Up @@ -999,3 +999,95 @@ func Test_Issue3218(t *testing.T) {
})
})
}

// https://github.com/gogf/gf/issues/2552
func Test_Issue2552_ClearTableFieldsAll(t *testing.T) {
table := createTable()
defer dropTable(table)

showTableKey := `SHOW FULL COLUMNS FROM`
gtest.C(t, func(t *gtest.T) {
ctx := context.Background()
sqlArray, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
_, err := db.Model(table).Ctx(ctx).Insert(g.Map{
"passport": guid.S(),
"password": guid.S(),
"nickname": guid.S(),
"create_time": gtime.NewFromStr(CreateTime).String(),
})
return err
})
t.AssertNil(err)
t.Assert(gstr.Contains(gstr.Join(sqlArray, "|"), showTableKey), true)

ctx = context.Background()
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
one, err := db.Model(table).Ctx(ctx).One()
t.Assert(len(one), 5)
return err
})
t.AssertNil(err)
t.Assert(gstr.Contains(gstr.Join(sqlArray, "|"), showTableKey), false)

_, err = db.Exec(ctx, fmt.Sprintf("alter table %s drop column `nickname`", table))
t.AssertNil(err)

err = db.GetCore().ClearTableFieldsAll(ctx)
t.AssertNil(err)

ctx = context.Background()
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
one, err := db.Model(table).Ctx(ctx).One()
t.Assert(len(one), 4)
return err
})
t.AssertNil(err)
t.Assert(gstr.Contains(gstr.Join(sqlArray, "|"), showTableKey), true)
})
}

// https://github.com/gogf/gf/issues/2552
func Test_Issue2552_ClearTableFields(t *testing.T) {
table := createTable()
defer dropTable(table)

showTableKey := `SHOW FULL COLUMNS FROM`
gtest.C(t, func(t *gtest.T) {
ctx := context.Background()
sqlArray, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
_, err := db.Model(table).Ctx(ctx).Insert(g.Map{
"passport": guid.S(),
"password": guid.S(),
"nickname": guid.S(),
"create_time": gtime.NewFromStr(CreateTime).String(),
})
return err
})
t.AssertNil(err)
t.Assert(gstr.Contains(gstr.Join(sqlArray, "|"), showTableKey), true)

ctx = context.Background()
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
one, err := db.Model(table).Ctx(ctx).One()
t.Assert(len(one), 5)
return err
})
t.AssertNil(err)
t.Assert(gstr.Contains(gstr.Join(sqlArray, "|"), showTableKey), false)

_, err = db.Exec(ctx, fmt.Sprintf("alter table %s drop column `nickname`", table))
t.AssertNil(err)

err = db.GetCore().ClearTableFields(ctx, table)
t.AssertNil(err)

ctx = context.Background()
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
one, err := db.Model(table).Ctx(ctx).One()
t.Assert(len(one), 4)
return err
})
t.AssertNil(err)
t.Assert(gstr.Contains(gstr.Join(sqlArray, "|"), showTableKey), true)
})
}
2 changes: 1 addition & 1 deletion database/gdb/gdb_core_utility.go
Expand Up @@ -155,7 +155,7 @@ func (c *Core) ClearTableFields(ctx context.Context, table string, schema ...str
func (c *Core) ClearTableFieldsAll(ctx context.Context) (err error) {
var (
keys = tableFieldsMap.Keys()
cachePrefix = fmt.Sprintf(`%s@%s`, cachePrefixTableFields, c.db.GetGroup())
cachePrefix = fmt.Sprintf(`%s%s`, cachePrefixTableFields, c.db.GetGroup())
removedKeys = make([]string, 0)
)
for _, key := range keys {
Expand Down
1 change: 1 addition & 0 deletions database/gdb/gdb_driver_wrapper_db.go
Expand Up @@ -68,6 +68,7 @@ func (d *DriverWrapperDB) TableFields(
)
}
var (
// prefix:group@schema#table
cacheKey = fmt.Sprintf(
`%s%s@%s#%s`,
cachePrefixTableFields,
Expand Down

0 comments on commit 1456856

Please sign in to comment.