retention

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: GPL-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrphanRowPruner

type OrphanRowPruner struct {
	IsPKorFKUUID bool
	ReferencedBy []ReferencingRelation // Tables that may still reference this table's primary key.
	Interval     time.Duration

	// Referrers is a list of tables that reference this table's primary key, allowing for cascading deletes to
	// maintain referential integrity.
	//
	// This must not be confused with the ReferencedBy field, which is used to identify orphaned rows based on
	// whether any referencing table still has a row pointing at them. This field allows for additional tables to
	// be pruned in a cascading manner, ensuring that any related rows are also deleted.
	Referrers []ReferencingRowPruner
	// contains filtered or unexported fields
}

OrphanRowPruner defines the configuration for pruning rows from a table that are not referenced by any other table.

This struct is used to identify and delete orphaned rows based on the primary key of the main table and the foreign key columns of the referencing tables. A row is only considered orphaned, and thus eligible for deletion, if none of the configured ReferencedBy relations reference it.

func (*OrphanRowPruner) Exec

func (orp *OrphanRowPruner) Exec(ctx context.Context, db *database.DB, l *logging.Logger, _ types.UnixMilli, limit uint64) (uint64, error)

Exec prunes rows from the specified table that are not referenced by any other table.

It constructs a delete statement that identifies orphaned rows based on the primary key and the referencing table's foreign key. The method executes the delete statement within a transaction, ensuring that the operation is performed atomically and with the appropriate isolation level.

func (*OrphanRowPruner) IntervalAndPeriodOverrides

func (orp *OrphanRowPruner) IntervalAndPeriodOverrides() time.Duration

func (*OrphanRowPruner) TableName

func (orp *OrphanRowPruner) TableName() string

type Pruner

type Pruner interface {
	// TableName returns the name of the table to be pruned.
	TableName() string

	// IntervalAndPeriodOverrides returns the duration that overrides the global retention period and interval for this pruner.
	IntervalAndPeriodOverrides() time.Duration

	// Exec performs the pruning operation for the specific pruner.
	//
	// It takes a context, a database connection, a time threshold (olderThan), and a limit on the number of rows
	// to delete. It's up to the specific pruner whether the provided arguments are relevant for it or not.
	// The method returns the number of deleted rows and any error encountered during the operation.
	Exec(context.Context, *database.DB, *logging.Logger, types.UnixMilli, uint64) (uint64, error)
}

Pruner defines the interface for pruning rows from a database table.

type ReferencingRelation

type ReferencingRelation struct {
	Table string // The name of the table that references this table's primary key.
	FK    string // The name of the foreign key column in the referencing table that points to this table's primary key.
}

ReferencingRelation identifies a table and foreign key column that may reference another table's primary key.

type ReferencingRowPruner

type ReferencingRowPruner prunerCommon

ReferencingRowPruner defines the configuration for pruning rows that reference the primary keys of another table.

This struct is used to maintain referential integrity when pruning rows from a main table by ensuring that any related rows in other tables are also pruned accordingly. In other words, it allows to explicitly define cascading deletes for related tables without relying on database-level foreign key constraints.

Usually, this is used in conjunction with TimeBoundPruner or OrphanRowPruner but never on its own, as it relies on the primary keys of another table to identify which rows to delete.

func (ReferencingRowPruner) Exec

func (rrp ReferencingRowPruner) Exec(ctx context.Context, db *database.DB, executer database.TxOrDB, pks ...any) (uint64, error)

Exec performs the pruning operation by deleting rows from the specified table that reference the given primary keys.

type ResetPruner

type ResetPruner struct {
	TimeBoundPruner
	UpdateExpression string
}

ResetPruner is a specific TimeBoundPruner for updating affected rows instead of deleting them.

The UpdateExpression is used in the UPDATE query after "UPDATE __TABLE__ SET " and must be one or many assignments.

func (*ResetPruner) Exec

func (rp *ResetPruner) Exec(ctx context.Context, db *database.DB, l *logging.Logger, olderThan types.UnixMilli, limit uint64) (uint64, error)

Exec prunes rows from the specified table that are older than the given time threshold.

The ResetPruner understands pruning as a UPDATE query, which should alter the row to not be matched again. If Referrers are defined, it will remove the matching rows atomically in a transaction.

type Retention

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

Retention is responsible for periodically pruning old data from the database.

It uses a list of Pruner configurations to determine which tables to prune and how to maintain referential integrity with related tables. The retention process runs in a separate goroutine for each pruner, allowing for concurrent pruning of multiple tables without blocking one another. The retention intervals and limits are configurable, and the process can be gracefully stopped using the provided context.

func New

func New(db *database.DB, logger *logging.Logger) *Retention

New creates a new instance of the Retention struct with the provided database connection and logger.

func (*Retention) Run

func (r *Retention) Run(ctx context.Context) error

Run starts the retention process, which periodically prunes old data from the database.

For each configured pruner, it sets up a periodic task that executes the pruning logic at the specified intervals.

type TimeBoundPruner

type TimeBoundPruner struct {
	IsPKorFKUUID bool
	TimeColumn   string

	Referrers      []ReferencingRowPruner
	ExtraCondition string

	// OverridePeriodAndInterval, when non-zero, overrides the global retention period and interval to this value.
	OverridePeriodAndInterval time.Duration
	// contains filtered or unexported fields
}

TimeBoundPruner defines the configuration for pruning rows from a table based on a time column.

This struct is designed to be flexible and reusable for different tables with varying time columns and primary keys. It also supports maintaining referential integrity by allowing the definition of related tables that reference the primary keys of the main table, ensuring that any related rows are also pruned accordingly.

func (*TimeBoundPruner) Exec

func (tbp *TimeBoundPruner) Exec(ctx context.Context, db *database.DB, l *logging.Logger, olderThan types.UnixMilli, limit uint64) (uint64, error)

Exec prunes rows from the specified table that are older than the given time threshold.

If Referrers are defined, it first retrieves the primary keys of the rows to be deleted and then executes the corresponding DELETE statements for each ReferencingRowPruner before finally deleting the rows from the main table. If no Referrers are defined, it directly deletes rows based on the time column.

The method returns only the total number of deleted rows from the main table.

func (*TimeBoundPruner) IntervalAndPeriodOverrides

func (tbp *TimeBoundPruner) IntervalAndPeriodOverrides() time.Duration

func (*TimeBoundPruner) TableName

func (tbp *TimeBoundPruner) TableName() string

Jump to

Keyboard shortcuts

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