This commit is contained in:
John Guo 2024-02-05 10:29:43 +08:00 committed by GitHub
parent cc79d23334
commit 14568562e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 1 deletions

View File

@ -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)
})
}

View File

@ -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 {

View File

@ -68,6 +68,7 @@ func (d *DriverWrapperDB) TableFields(
)
}
var (
// prefix:group@schema#table
cacheKey = fmt.Sprintf(
`%s%s@%s#%s`,
cachePrefixTableFields,