Files
archer/internal/cli/hash.go
T
voidandClaude Sonnet 4.6 bc4a6b504f Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr,
docs, build scripts and ARCHER_* environment variables.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-06 22:12:17 +08:00

103 lines
3.1 KiB
Go

package cli
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"sort"
"strconv"
"strings"
"github.com/go-sql-driver/mysql"
)
// Fingerprint returns a stable hash of every configuration input that can
// change scan or shard semantics, plus the normalized fields used to build it.
// The source and dest fields are standard MySQL DSNs that include every
// connection parameter the driver parsed (charset, collation, timeout, ...),
// so a resume notices any of them changing. The password is the one exclusion:
// rotating it must not break resume.
func (c *Config) Fingerprint() (string, map[string]string, error) {
src, err := c.SourceConfig()
if err != nil {
return "", nil, fmt.Errorf("source DSN: %w", err)
}
dst, err := c.DestConfig()
if err != nil {
return "", nil, fmt.Errorf("dest DSN: %w", err)
}
fields := map[string]string{
"source": dsnFingerprint(src),
"dest": dsnFingerprint(dst),
"table": c.Table,
"dest_table": c.DestTable,
"where": c.Where,
"columns": strings.Join(c.Columns, ","),
"purge": strconv.FormatBool(c.Purge),
"bulk_delete": strconv.FormatBool(c.BulkDelete),
"insert_mode": c.InsertMode,
"shard_by": c.ShardBy,
"shard_rows": strconv.Itoa(c.ShardRows),
"shard_column": c.ShardColumn,
"shard_column_type": c.ShardColumnType,
"shard_time_zone": c.ShardTimeZone,
"zero_date": c.ZeroDate,
"format": c.Format,
"compress": c.Compress,
"csv_null": c.CSVNull,
"safe_auto_increment": strconv.FormatBool(c.SafeAutoIncrement),
}
// The level only changes the output when compression is on, and omitting
// the default keeps fingerprints of tasks started before it existed
// byte-identical so they can resume.
if c.Compress != "" && c.Compress != "none" && c.CompressLevel != 3 {
fields["compress_level"] = strconv.Itoa(c.CompressLevel)
}
keys := make([]string, 0, len(fields))
for k := range fields {
keys = append(keys, k)
}
sort.Strings(keys)
var b strings.Builder
for _, k := range keys {
b.WriteString(k)
b.WriteByte('=')
b.WriteString(fields[k])
b.WriteByte('\n')
}
sum := sha256.Sum256([]byte(b.String()))
return hex.EncodeToString(sum[:]), fields, nil
}
// dsnFingerprint renders c as a standard MySQL DSN covering every parameter
// the driver parsed, except the password. Copying by value keeps the driver's
// unexported fields (charsets, timeTruncate, ...) intact, which is the only way
// FormatDSN can serialize them.
func dsnFingerprint(c *mysql.Config) string {
if c == nil {
return ""
}
redacted := *c
redacted.Passwd = ""
return redacted.FormatDSN()
}
// DiffFields lists keys whose values differ between two fingerprints.
func DiffFields(old, cur map[string]string) []string {
seen := map[string]bool{}
var out []string
for k, v := range cur {
seen[k] = true
if old[k] != v {
out = append(out, fmt.Sprintf("%s: %q -> %q", k, old[k], v))
}
}
for k, v := range old {
if !seen[k] {
out = append(out, fmt.Sprintf("%s: %q -> (absent)", k, v))
}
}
sort.Strings(out)
return out
}