Sessions, context & Debug
Per-call configuration with Session, WithContext, and Debug — and what each one clones.
Session, WithContext, and Debug are the three ways to configure a
single chain without touching the root. Each returns a derivative with a
copied Config.
The Session struct
// Session returns a derivative configured per config. Flags on config are
// one-way overrides onto a copied Config; the receiver's Config is never
// mutated. With NewDB false (default) the derivative inherits the current
// chain's statement; with Context set the statement is cloned first so the
// new context cannot leak back.
func (db *DB) Session(config *Session) *DB| Field | Type | What it does |
|---|---|---|
DryRun | bool | Turns the derivative into a SQL-capturing session — statements assemble and record SQL/vars but never execute. |
NewDB | bool | Resets the chain: the derivative starts with a fresh Statement instead of inheriting the receiver's clauses. |
Initialized | bool | Forces the derivative through getInstance immediately. |
SkipDefaultTransaction | bool | Overrides the config flag for this session only. |
DisableNestedTransaction | bool | Overrides the config flag for this session only. |
AllowGlobalUpdate | bool | Permits no-WHERE Update/Delete for this session only — the per-call escape hatch for the guard. |
Context | context.Context | Replaces the derivative's execution context. |
Logger | logger.Interface | Replaces the derivative's logger (this is how Debug works). |
NowFunc | func() time.Time | Replaces the derivative's timestamp source. |
FullSaveAssociations | bool | Saves associated records with Save instead of Create on this derivative. |
All overrides are one-way: they set flags that are currently false; they never unset flags set on the root config.
WithContext
WithContext propagates ctx to every statement on the derivative — every
I/O call made through the statement's context honors cancellation and
deadlines.
// WithContext returns a derivative that runs every statement under ctx.ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
var users []User
err := db.WithContext(ctx).Model(&User{}).Where("active = ?", true).Find(&users).ErrorA nil context becomes context.TODO() — never a panic.
Debug
Debug returns a derivative whose logger is the session's logger switched to
Info level, so every SQL trace shows up.
// Debug returns a derivative that logs every SQL trace at Info level.db.Debug().Model(&User{}).Where("name = ?", "Ada").Find(&users)Because it's a derivative, only this chain is verbose — the root keeps its normal level.
Patterns
Capture SQL for logging or tests without a round-trip:
tx := db.Session(&fusion.Session{DryRun: true}).
Model(&User{}).
Where("name = ?", "Ada")
// tx.Statement.SQL / tx.Statement.Vars now hold what would runAllow a one-off global update while the root stays guarded:
err := db.Session(&fusion.Session{AllowGlobalUpdate: true}).
Model(&User{}).
Update("active", false).ErrorGet a fresh statement that ignores inherited clauses:
fresh := db.Session(&fusion.Session{NewDB: true})What a derivative inherits
With NewDB: false (the default), a derivative inherits the receiver's
statement — clauses, model, and all. Use NewDB: true when you want a
pristine chain that shares only the pool, caches, and callback processors.