Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 13 additions & 8 deletions pagebuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ func (b *Builder) firstOrCreateDemoContainers(ctx *web.EventContext, cons ...*Co
cons = b.containerBuilders
}
for _, con := range cons {
if err := con.firstOrCreate(slices.Concat(localeCodes)); err != nil {
if err := con.firstOrCreate(ctx, slices.Concat(localeCodes)); err != nil {
continue
}
}
Expand Down Expand Up @@ -1320,20 +1320,25 @@ func (b *ContainerBuilder) getContainerDataID(modelID int, primarySlug string) s
return fmt.Sprintf(inflection.Plural(strcase.ToKebab(b.name))+"_%v_%v", modelID, primarySlug)
}

func (b *ContainerBuilder) firstOrCreate(localeCodes []string) (err error) {
func (b *ContainerBuilder) firstOrCreate(ctx *web.EventContext, localeCodes []string) (err error) {
var (
db = b.builder.db
obj = b.mb.NewModel()
cons []*DemoContainer
m = &DemoContainer{}
db = b.builder.db
obj = b.mb.NewModel()
cons []*DemoContainer
m = &DemoContainer{}
saver = b.mb.Editing().Creating().Saver
)
if len(localeCodes) == 0 {
return
}
ctx.R.Form.Set(ParamContainerCreate, "1")
return db.Transaction(func(tx *gorm.DB) (vErr error) {
ctx.WithContextValue(gorm2op.CtxKeyDB{}, tx)
defer ctx.WithContextValue(gorm2op.CtxKeyDB{}, nil)
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context value is being set and then immediately cleared in the defer statement, which means the database transaction will not be available to the saver function. The defer should be moved after the saver calls or the context clearing should be removed.

Suggested change
defer ctx.WithContextValue(gorm2op.CtxKeyDB{}, nil)

Copilot uses AI. Check for mistakes.

tx.Where("model_name = ? and locale_code in ? ", b.name, localeCodes).Find(&cons)

if len(cons) == 0 {
if vErr = tx.Create(obj).Error; vErr != nil {
if vErr = saver(obj, "", ctx); vErr != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Nil Saver Causes Panic, Context Update Fails

The firstOrCreate function can panic if b.mb.Editing().Creating().Saver is nil. Additionally, ctx.WithContextValue doesn't update the ctx variable, preventing the transaction database context from being correctly propagated to the saver and cleared afterwards.

Fix in Cursor Fix in Web

Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The empty string parameter passed to saver is unclear. Consider using a named constant or adding a comment to explain what this parameter represents.

Copilot uses AI. Check for mistakes.

return
}
modelID := reflectutils.MustGet(obj, "ID").(uint)
Expand Down Expand Up @@ -1364,7 +1369,7 @@ func (b *ContainerBuilder) firstOrCreate(localeCodes []string) (err error) {
continue
}
obj = b.mb.NewModel()
if vErr = tx.Create(obj).Error; vErr != nil {
if vErr = saver(obj, "", ctx); vErr != nil {
return
}
modelID := reflectutils.MustGet(obj, "ID").(uint)
Expand Down
2 changes: 2 additions & 0 deletions pagebuilder/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
UpdateContainerEvent = "page_builder_UpdateContainerEvent"
ReloadAddContainersListEvent = "page_builder_ReloadAddContainersEvent"

ParamContainerCreate = "paramContainerCreate"

paramPageID = "pageID"
paramPageVersion = "pageVersion"
paramLocale = "locale"
Expand Down
8 changes: 4 additions & 4 deletions utils/testflow/gentool/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/testify v1.10.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/tools v0.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions utils/testflow/gentool/go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -20,6 +22,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qor5/web/v3 v3.0.12-0.20250225073451-8e876be98c21 h1:Ho7ZJ04vT96lqe6g7PJhc9eFEUsEgDGpHyPhqzwGF3E=
github.com/qor5/web/v3 v3.0.12-0.20250225073451-8e876be98c21/go.mod h1:32vdHHcZb2JimlcaclW9hLUyimdXjrllZDHTh3rl6d0=
github.com/qor5/web/v3 v3.0.12-0.20250322025751-d36834ab80b4/go.mod h1:zU8n7tDAwYgq3HWMpe0dgmywBZaZx3ENBfwmjwEwYPo=
Expand All @@ -38,6 +41,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
Expand Down