utils

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package utils contains some common utilities used by all other packages.

Index

Constants

View Source
const (
	PrimaryKeySeparator = "-#-" // used to hash a composite primary key

	// MaxTableNameLength is the maximum table name length in MySQL.
	MaxTableNameLength = 64
)
View Source
const (
	// NameFormatTimestamp is the time.Format layout used in the timestamped
	// _<table>_old_<timestamp> name when SkipDropAfterCutover is set.
	NameFormatTimestamp = "20060102_150405"
)

Variables

This section is empty.

Functions

func AuxTableName added in v0.13.0

func AuxTableName(tableName, suffix string) string

AuxTableName builds a deterministic auxiliary table name for the given original table name and suffix (e.g. "_chkpnt", "_new", "_old"). The returned name is `_<table><suffix>`, with the table-name portion deterministically truncated when needed and the result hard-capped at MySQL's 64-character identifier limit (a defensive guard against an abusively long suffix; in practice callers pass small suffixes).

Truncation is deterministic: the same (tableName, suffix) input always produces the same output. Two distinct table names that share a long common prefix can collide; callers must record the original table name out-of-band (e.g. in the checkpoint) so collisions can be detected.

func CheckpointTableName added in v0.13.0

func CheckpointTableName(tableName string) string

CheckpointTableName returns the auxiliary checkpoint table name for the given original table.

func CloseAndLog added in v0.10.2

func CloseAndLog(closer Closer)

CloseAndLog closes a resource and logs any error. This is useful for defer statements where the error cannot be meaningfully handled except by logging. Example: defer utils.CloseAndLog(db)

func CloseAndLogWithContext added in v0.10.2

func CloseAndLogWithContext(ctx context.Context, closer ContextCloser)

CloseAndLogWithContext closes a resource that requires context and logs any error. This is useful for defer statements on resources like table locks. Example: defer utils.CloseAndLogWithContext(ctx, lock)

func HashKey

func HashKey(key []any) string

HashKey is used to convert a composite key into a string so that it can be placed in a map.

func NewTableName added in v0.13.0

func NewTableName(tableName string) string

NewTableName returns the auxiliary _new table name for the given original table.

func OldTableName added in v0.13.0

func OldTableName(tableName string) string

OldTableName returns the auxiliary _old table name for the given original table.

func OldTableNameWithTimestamp added in v0.13.0

func OldTableNameWithTimestamp(tableName, timestamp string) string

OldTableNameWithTimestamp returns the auxiliary _old_<timestamp> table name for the given original table and timestamp string. Used when SkipDropAfterCutover is set so the renamed-away table is preserved with a unique name across multiple migrations.

func TruncateTableName added in v0.13.0

func TruncateTableName(tableName string, suffixLength int) string

TruncateTableName truncates a table name to fit within MySQL's 64-character limit, reserving space for a suffix of the given length. If the table name already fits, it is returned unchanged.

func UnhashKeyToString

func UnhashKeyToString(key string) string

UnhashKeyToString converts a hashed key to a string that can be used in a query.

Types

type Closer added in v0.10.2

type Closer interface {
	Close() error
}

Closer is an interface for types that have a Close() method. This is compatible with io.Closer and many other types in the codebase.

type ContextCloser added in v0.10.2

type ContextCloser interface {
	Close(context.Context) error
}

ContextCloser is an interface for types that have a Close(context.Context) method. This is used for resources like table locks that need context for cleanup.

Jump to

Keyboard shortcuts

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