Documentation
¶
Overview ¶
Package utils contains some common utilities used by all other packages.
Index ¶
- Constants
- func AuxTableName(tableName, suffix string) string
- func CheckpointTableName(tableName string) string
- func CloseAndLog(closer Closer)
- func CloseAndLogWithContext(ctx context.Context, closer ContextCloser)
- func DBLevelGrantCoversSchema(grant, schemaName string) bool
- func HashKey(key []any) string
- func MySQLLikeMatch(pattern, name string) bool
- func NewTableName(tableName string) string
- func OldTableName(tableName string) string
- func OldTableNameWithTimestamp(tableName, timestamp string) string
- func TruncateTableName(tableName string, suffixLength int) string
- type Closer
- type ContextCloser
Constants ¶
const ( PrimaryKeySeparator = "-#-" // used to hash a composite primary key // MaxTableNameLength is the maximum table name length in MySQL. MaxTableNameLength = 64 )
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
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
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 DBLevelGrantCoversSchema ¶ added in v0.15.0
DBLevelGrantCoversSchema reports whether a single SHOW GRANTS line is a database-level grant that confers the privileges spirit needs on schemaName. Unlike a literal substring match, it expands MySQL wildcard patterns in the granted database name (see MySQLLikeMatch), so a grant on `strata_%`.* is recognized as covering strata_boardgames_sharded_n80. The previous literal match handled only exact and escaped-underscore database names.
func HashKey ¶
HashKey converts a composite key into a string so that it can be used as a map key. The result is an opaque map key only — it is never reversed back into SQL (the applier builds DELETE literals from the original typed values, not from this string). Each component is escaped before joining so that values containing the separator cannot collide with key boundaries: without escaping, ("a-#-b", "c") and ("a", "b-#-c") would hash to the same string, causing one row's buffered change to silently overwrite another's.
func MySQLLikeMatch ¶ added in v0.15.0
MySQLLikeMatch reports whether name matches the given MySQL LIKE-style pattern. As in MySQL pattern matching, '%' matches any sequence of characters (including the empty string), '_' matches any single character, and a backslash escapes the following character so that it is treated as a literal (so `\%` and `\_` match a literal '%' and '_').
This mirrors how MySQL evaluates the database-name portion of a database-level GRANT. A grant on `strata_%`.* applies to a database named strata_boardgames, even though SHOW GRANTS reports the pattern verbatim. Privilege checks therefore cannot compare the granted database name to the target schema with a plain string match; they must expand the pattern.
func NewTableName ¶ added in v0.13.0
NewTableName returns the auxiliary _new table name for the given original table.
func OldTableName ¶ added in v0.13.0
OldTableName returns the auxiliary _old table name for the given original table.
func OldTableNameWithTimestamp ¶ added in v0.13.0
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
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.
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
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.