mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-14 19:21:46 +03:00
Set default transaction mode to read only with --readonly flag
This commit is contained in:
parent
e3e1d7b012
commit
661fed0dbb
@ -225,7 +225,29 @@ func (client *Client) Query(query string) (*Result, error) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (client *Client) SetReadOnlyMode() error {
|
||||
var value string
|
||||
if err := client.db.Get(&value, "SHOW default_transaction_read_only;"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if value == "off" {
|
||||
_, err = client.db.Exec("SET default_transaction_read_only=on;")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
|
||||
// We're going to force-set transaction mode on every query.
|
||||
// This is needed so that default mode could not be changed by user.
|
||||
if command.Opts.ReadOnly {
|
||||
if err := client.SetReadOnlyMode(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
action := strings.ToLower(strings.Split(query, " ")[0])
|
||||
if action == "update" || action == "delete" {
|
||||
res, err := client.db.Exec(query, args...)
|
||||
|
@ -24,6 +24,7 @@ type Options struct {
|
||||
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
|
||||
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
|
||||
Prefix string `long:"prefix" description:"Add a url prefix"`
|
||||
ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"`
|
||||
}
|
||||
|
||||
var Opts Options
|
||||
|
Loading…
Reference in New Issue
Block a user