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

Allow manager logging to set SQL #20064

Merged
merged 6 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 12 additions & 15 deletions cmd/manager_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,17 @@ var (
},
},
}, {
Name: "log-sql",
Usage: "Set LogSQL",
Name: "log-sql",
Usage: "Set LogSQL",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "debug",
}, cli.BoolFlag{
Name: "off",
6543 marked this conversation as resolved.
Show resolved Hide resolved
Usage: "Switch off SQL logging",
},
},
Action: runSetLogSQL,
}, {
Name: "no-log-sql",
Usage: "Switch off LogSQL",
Action: runUnSetLogSQL,
},
},
}
Expand Down Expand Up @@ -391,19 +395,12 @@ func runReleaseReopenLogging(c *cli.Context) error {
}

func runSetLogSQL(c *cli.Context) error {
return runSetLogSQLFlag(c, true)
}

func runUnSetLogSQL(c *cli.Context) error {
return runSetLogSQLFlag(c, false)
}

func runSetLogSQLFlag(c *cli.Context, set bool) error {
ctx, cancel := installSignals()
defer cancel()
setup("manager", c.Bool("debug"))

statusCode, msg := private.SetLogSQL(ctx, set)
off := c.Bool("off")
statusCode, msg := private.SetLogSQL(ctx, !off)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
switch statusCode {
case http.StatusInternalServerError:
return fail("InternalServerError", msg)
Expand Down
9 changes: 9 additions & 0 deletions models/db/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,12 @@ func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) {
_, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID)
return maxID, err
}

func SetLogSQL(ctx context.Context, on bool) {
e := GetEngine(ctx)
if x, ok := e.(*xorm.Engine); ok {
x.ShowSQL(on)
} else if sess, ok := e.(*xorm.Session); ok {
sess.Engine().ShowSQL(on)
}
}
9 changes: 1 addition & 8 deletions routers/private/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web"
"xorm.io/xorm"
)

// FlushQueues flushes all the Queues
Expand Down Expand Up @@ -71,13 +70,7 @@ func ReleaseReopenLogging(ctx *context.PrivateContext) {

// SetLogSQL re-sets database SQL logging
func SetLogSQL(ctx *context.PrivateContext) {
on := ctx.FormBool("on")
e := db.GetEngine(ctx)
if x, ok := e.(*xorm.Engine); ok {
x.ShowSQL(on)
} else if sess, ok := e.(*xorm.Session); ok {
sess.Engine().ShowSQL(on)
}
db.SetLogSQL(ctx, ctx.FormBool("on"))
ctx.PlainText(http.StatusOK, "success")
}

Expand Down