improve dry-run feature by adding global dry-run variable reading from environment or command options

This commit is contained in:
Jack
2020-07-30 23:08:27 +08:00
parent 0a5c6d832f
commit b396096721
2 changed files with 14 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/internal/cmdenv"
"time"
"github.com/gogf/gf/container/gvar"
@ -262,8 +263,17 @@ var (
// regularFieldNameRegPattern is the regular expression pattern for a string
// which is a regular field name of table.
regularFieldNameRegPattern = `^[\w\.\-]+$`
// allDryRun sets dry-run feature for all database connections.
// It is commonly used for command options for convenience.
allDryRun = false
)
func init() {
// allDryRun is initialized from environment or command options.
allDryRun = cmdenv.Get("gf.gdb.dryrun", false).Bool()
}
// Register registers custom database driver to gdb.
func Register(name string, driver Driver) error {
driverMap[name] = driver

View File

@ -186,6 +186,10 @@ func (c *Core) SetDryRun(dryrun bool) {
// GetDryRun returns the DryRun value.
func (c *Core) GetDryRun() bool {
if allDryRun {
// Globally set.
return true
}
return c.dryrun.Val()
}