Help us shape the future of Go! Take the 2026 Go Developer Survey

prune

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 7 Imported by: 0

README

Bounded model pruning

Register handwritten ORM selections with ORMModel for ordinary models or ORMSoftModel for soft-delete models:

registry, err := prune.New(
    prune.ORMModel("app.logs", Logs.Query().Where(orm.ColumnEq(Logs.Expired, true)), prune.Lifecycle),
    prune.ORMSoftModel("app.users", Users.Query().OnlyTrashed(), Users.Soft, prune.Mass),
)
if err != nil {
    return err
}
result, err := registry.Run(ctx, db, prune.DefaultOptions())

Each batch is atomic and bounded by orm.MaxPerModelWriteRows. Selection preserves scopes, filters and visibility, locks candidates in primary-key order, and closes rows before callbacks. Lifecycle invokes each model's deletion lifecycle; Mass acknowledges skipping hooks, observers and audit. Soft models require the token from the exact table declaration. A failed batch contributes no confirmed removals; earlier committed batches remain in Result.Counts. Unknown transaction outcomes remain errors and are never retried automatically.

Registrations use the ORM functions above; the registry and its bounds share the native query owners. The generated-query adapter has been removed.

Documentation

Overview

Package prune removes obsolete models in bounded batches, like Laravel's Prunable and MassPrunable models. Applications declare each prunable model with a typed selection; the registry runs them explicitly (for example from a scheduled command). Nothing prunes during boot.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Count

type Count struct {
	Name    Name  `json:"name"`
	Removed int64 `json:"removed"`
	// Remaining is true when MaxBatches stopped the run before a short batch.
	Remaining bool `json:"remaining,omitempty"`
}

Count reports one declaration's committed removals.

type Mode

type Mode uint8

Mode selects how selected models are removed.

const (
	// Lifecycle removes each model through its ordinary (force) delete
	// lifecycle, running hooks and observers.
	Lifecycle Mode = iota + 1
	// Mass removes each batch with one DELETE and no per-model hooks.
	Mass
)

type Name

type Name string

Name identifies one prunable declaration, such as app.sessions.

type Options

type Options struct {
	BatchSize  int
	MaxBatches int
}

Options bounds one run. Each batch is its own transaction.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions removes up to 100 batches of 500 models per declaration.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds validated declarations in name order.

func New

func New(targets ...Target) (*Registry, error)

New validates unique semantic names and known modes.

func (*Registry) Names

func (r *Registry) Names() []Name

Names lists declarations in name order.

func (*Registry) Run

func (r *Registry) Run(ctx context.Context, db database.Transactor, options Options, selected ...Name) (Result, error)

Run prunes the selected declarations (all when none are named) in name order, batch by batch, until a batch removes fewer than BatchSize models or MaxBatches is reached. A failure stops the run and reports confirmed work.

type Result

type Result struct {
	Counts    []Count `json:"counts"`
	StoppedAt *Name   `json:"stopped_at,omitempty"`
}

Result lists committed removals. StoppedAt names the declaration whose batch failed; earlier batches, including its own, remain committed.

type Target

type Target struct {
	// contains filtered or unexported fields
}

Target is one prunable model declaration; use ORMModel or ORMSoftModel.

func ORMModel

func ORMModel[M any](name Name, selection orm.Query[M], mode Mode) Target

ORMModel registers an ordinary ORM model for bounded lifecycle or mass pruning. Example: prune.ORMModel("app.logs", Logs.Query(), prune.Lifecycle).

func ORMSoftModel

func ORMSoftModel[M any](name Name, selection orm.Query[M], soft orm.SoftDeletable[M], mode Mode) Target

ORMSoftModel registers physical pruning with the exact soft-model declaration token. Example: prune.ORMSoftModel("app.users", Users.Query().OnlyTrashed(), Users.Soft, prune.Mass).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL