Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #2552 #3300

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_issue_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading