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>
This commit is contained in:
commit
bc4a6b504f
99 files changed
+12752
No files matched your search
@@ -0,0 +1,352 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func baseArgs() []string {
|
||||
return []string{
|
||||
"--source", "u:p@tcp(127.0.0.1:3306)/db",
|
||||
"--table", "t",
|
||||
"--where", "id > 0",
|
||||
"--file", "out",
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaults(t *testing.T) {
|
||||
c, err := Parse(baseArgs(), &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.ChunkSize != 2000 || c.ShardBy != "none" || c.Format != "dump" || c.Compress != "none" {
|
||||
t.Fatalf("bad defaults: %+v", c)
|
||||
}
|
||||
if c.MaxLag.String() != "1s" || c.CheckInterval.String() != "1s" || c.Retries != 1 {
|
||||
t.Fatalf("bad defaults: %+v", c)
|
||||
}
|
||||
if !c.SafeAutoIncrement {
|
||||
t.Fatal("safe auto increment must default true")
|
||||
}
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLimitAlias(t *testing.T) {
|
||||
args := append(baseArgs(), "--limit", "500")
|
||||
c, err := Parse(args, &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.ChunkSize != 500 {
|
||||
t.Fatalf("got %d", c.ChunkSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseInsertModesMutuallyExclusive(t *testing.T) {
|
||||
args := append(baseArgs(), "--insert-ignore", "--replace")
|
||||
if _, err := Parse(args, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRequired(t *testing.T) {
|
||||
c, _ := Parse([]string{"--source", "u:p@tcp(h:3306)/db", "--table", "t", "--file", "out"}, &bytes.Buffer{})
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "--where") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNeedsTarget(t *testing.T) {
|
||||
c, _ := Parse([]string{"--source", "u:p@tcp(h:3306)/db", "--table", "t", "--where", "1=1"}, &bytes.Buffer{})
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "output target") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateShardRules(t *testing.T) {
|
||||
args := append(baseArgs(), "--shard-by", "count", "--shard-column", "created_at", "--shard-rows", "10")
|
||||
c, err := Parse(args, &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "--shard-column") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
|
||||
args = append(baseArgs(), "--shard-by", "count")
|
||||
c, _ = Parse(args, &bytes.Buffer{})
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "--shard-rows") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
|
||||
args = append(baseArgs(), "--shard-by", "date", "--shard-column", "c", "--bulk-delete", "--purge")
|
||||
c, _ = Parse(args, &bytes.Buffer{})
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "bulk-delete") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateBulkDeleteNeedsPurge(t *testing.T) {
|
||||
args := append(baseArgs(), "--bulk-delete")
|
||||
c, _ := Parse(args, &bytes.Buffer{})
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "requires --purge") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFingerprintExcludesPassword(t *testing.T) {
|
||||
a, _ := Parse([]string{"--source", "u:one@tcp(h:3306)/db", "--table", "t", "--where", "1=1", "--file", "out"}, &bytes.Buffer{})
|
||||
b, _ := Parse([]string{"--source", "u:two@tcp(h:3306)/db", "--table", "t", "--where", "1=1", "--file", "out"}, &bytes.Buffer{})
|
||||
ha, fa, err := a.Fingerprint()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hb, fb, err := b.Fingerprint()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ha != hb {
|
||||
t.Fatal("password must not affect the fingerprint")
|
||||
}
|
||||
if fa["source"] != fb["source"] {
|
||||
t.Fatalf("password must not affect the source field: %q vs %q", fa["source"], fb["source"])
|
||||
}
|
||||
}
|
||||
|
||||
func fingerprintFor(t *testing.T, dsn string) (string, map[string]string) {
|
||||
t.Helper()
|
||||
c, err := Parse([]string{"--source", dsn, "--table", "t", "--where", "id > 0", "--file", "out"}, &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h, f, err := c.Fingerprint()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return h, f
|
||||
}
|
||||
|
||||
func TestFingerprintTracksConnectionCharset(t *testing.T) {
|
||||
hBase, _ := fingerprintFor(t, "u:p@tcp(h:3306)/db")
|
||||
hCharset, fCharset := fingerprintFor(t, "u:p@tcp(h:3306)/db?charset=utf8mb4")
|
||||
if hBase == hCharset {
|
||||
t.Fatal("charset must affect the fingerprint")
|
||||
}
|
||||
if !strings.Contains(fCharset["source"], "charset=utf8mb4") {
|
||||
t.Fatalf("source field missing charset: %q", fCharset["source"])
|
||||
}
|
||||
|
||||
hCollation, _ := fingerprintFor(t, "u:p@tcp(h:3306)/db?charset=utf8mb4&collation=utf8mb4_bin")
|
||||
if hCharset == hCollation {
|
||||
t.Fatal("collation must affect the fingerprint")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDsnFingerprintRedactsPassword(t *testing.T) {
|
||||
c, err := mysql.ParseDSN("u:s3cr3t@tcp(h:3306)/db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := dsnFingerprint(c)
|
||||
if got != "u@tcp(h:3306)/db" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
if strings.Contains(got, "s3cr3t") {
|
||||
t.Fatal("password leaked into the fingerprint")
|
||||
}
|
||||
if c.Passwd != "s3cr3t" {
|
||||
t.Fatal("dsnFingerprint must not modify the input config")
|
||||
}
|
||||
if dsnFingerprint(nil) != "" {
|
||||
t.Fatal("nil config must yield an empty fragment")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFingerprintDestEmptyAndNoPassword(t *testing.T) {
|
||||
h, f := fingerprintFor(t, "u:s3cr3t@tcp(h:3306)/db")
|
||||
if f["dest"] != "" {
|
||||
t.Fatalf("dest must be empty without --dest: %q", f["dest"])
|
||||
}
|
||||
if f["source"] != "u@tcp(h:3306)/db" {
|
||||
t.Fatalf("source = %q", f["source"])
|
||||
}
|
||||
if strings.Contains(h, "s3cr3t") || strings.Contains(f["source"], "s3cr3t") {
|
||||
t.Fatal("password leaked into the fingerprint")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFingerprintDetectsTimezoneChange(t *testing.T) {
|
||||
a, _ := Parse(append(baseArgs(), "--shard-by", "date", "--shard-column", "c", "--shard-time-zone", "UTC"), &bytes.Buffer{})
|
||||
b, _ := Parse(append(baseArgs(), "--shard-by", "date", "--shard-column", "c", "--shard-time-zone", "Asia/Shanghai"), &bytes.Buffer{})
|
||||
ha, fa, _ := a.Fingerprint()
|
||||
hb, _, _ := b.Fingerprint()
|
||||
if ha == hb {
|
||||
t.Fatal("time zone must affect the fingerprint")
|
||||
}
|
||||
_ = fa
|
||||
}
|
||||
|
||||
func TestDiffFields(t *testing.T) {
|
||||
old := map[string]string{"a": "1", "b": "2"}
|
||||
cur := map[string]string{"a": "1", "b": "3", "c": "4"}
|
||||
d := DiffFields(old, cur)
|
||||
if len(d) != 2 {
|
||||
t.Fatalf("got %v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCompressLevel(t *testing.T) {
|
||||
for _, level := range []string{"0", "10"} {
|
||||
c, err := Parse(append(baseArgs(), "--compress", "gzip", "--compress-level", level), &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "between 1 and 9") {
|
||||
t.Fatalf("level %s: got %v", level, err)
|
||||
}
|
||||
}
|
||||
|
||||
c, _ := Parse(append(baseArgs(), "--compress", "none", "--compress-level", "5"), &bytes.Buffer{})
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "only valid when compression is enabled") {
|
||||
t.Fatalf("none + level 5: got %v", err)
|
||||
}
|
||||
|
||||
c, _ = Parse(append(baseArgs(), "--compress", "none", "--compress-level", "3"), &bytes.Buffer{})
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatalf("none + default level must be accepted: %v", err)
|
||||
}
|
||||
|
||||
for _, name := range []string{"gzip", "zstd", "xz"} {
|
||||
c, _ = Parse(append(baseArgs(), "--compress", name, "--compress-level", "9"), &bytes.Buffer{})
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatalf("%s level 9: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgressModes(t *testing.T) {
|
||||
cases := []struct {
|
||||
arg string
|
||||
mode ProgressMode
|
||||
rows int64
|
||||
valid bool
|
||||
}{
|
||||
{"", ProgressAuto, 0, true},
|
||||
{"auto", ProgressAuto, 0, true},
|
||||
{"on", ProgressOn, 0, true},
|
||||
{"off", ProgressOff, 0, true},
|
||||
{"0", ProgressOff, 0, true},
|
||||
{"100000", ProgressRows, 100000, true},
|
||||
{"1", ProgressRows, 1, true},
|
||||
{"abc", 0, 0, false},
|
||||
{"-1", 0, 0, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
args := baseArgs()
|
||||
if tc.arg != "" {
|
||||
args = append(args, "--progress", tc.arg)
|
||||
}
|
||||
c, err := Parse(args, &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatalf("%q: parse: %v", tc.arg, err)
|
||||
}
|
||||
err = c.Validate()
|
||||
if !tc.valid {
|
||||
if err == nil {
|
||||
t.Fatalf("%q: expected a configuration error", tc.arg)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("%q: validate: %v", tc.arg, err)
|
||||
}
|
||||
if c.Progress.Mode != tc.mode || c.Progress.Rows != tc.rows {
|
||||
t.Fatalf("%q: got mode=%d rows=%d", tc.arg, c.Progress.Mode, c.Progress.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
c, _ := Parse(baseArgs(), &bytes.Buffer{})
|
||||
if c.Progress.Mode != ProgressAuto {
|
||||
t.Fatalf("default progress mode = %d, want auto", c.Progress.Mode)
|
||||
}
|
||||
if c.ProgressInterval != time.Second {
|
||||
t.Fatalf("default progress interval = %s", c.ProgressInterval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgressEnabledDependsOnTTY(t *testing.T) {
|
||||
on, _ := Parse(append(baseArgs(), "--progress", "on"), &bytes.Buffer{})
|
||||
off, _ := Parse(append(baseArgs(), "--progress", "off"), &bytes.Buffer{})
|
||||
auto, _ := Parse(baseArgs(), &bytes.Buffer{})
|
||||
for _, c := range []*Config{on, off, auto} {
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if !on.Progress.Enabled(false) || !on.Progress.Enabled(true) {
|
||||
t.Fatal("on must always be enabled")
|
||||
}
|
||||
if off.Progress.Enabled(true) || off.Progress.Enabled(false) {
|
||||
t.Fatal("off must never be enabled")
|
||||
}
|
||||
if !auto.Progress.Enabled(true) {
|
||||
t.Fatal("auto must be enabled on a terminal")
|
||||
}
|
||||
if auto.Progress.Enabled(false) {
|
||||
t.Fatal("auto must be silent without a terminal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgressIntervalValidation(t *testing.T) {
|
||||
c, err := Parse(append(baseArgs(), "--progress-interval", "0s"), &bytes.Buffer{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Validate(); err == nil || !strings.Contains(err.Error(), "--progress-interval") {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFingerprintCompressLevel(t *testing.T) {
|
||||
implicit, _ := Parse(append(baseArgs(), "--compress", "gzip"), &bytes.Buffer{})
|
||||
explicit, _ := Parse(append(baseArgs(), "--compress", "gzip", "--compress-level", "3"), &bytes.Buffer{})
|
||||
hImplicit, fImplicit, err := implicit.Fingerprint()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hExplicit, _, err := explicit.Fingerprint()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if hImplicit != hExplicit {
|
||||
t.Fatal("the default level must not change the fingerprint")
|
||||
}
|
||||
if _, ok := fImplicit["compress_level"]; ok {
|
||||
t.Fatal("the default level must not be fingerprinted")
|
||||
}
|
||||
|
||||
five, _ := Parse(append(baseArgs(), "--compress", "gzip", "--compress-level", "5"), &bytes.Buffer{})
|
||||
hFive, fFive, err := five.Fingerprint()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if hFive == hImplicit {
|
||||
t.Fatal("a non-default level must change the fingerprint")
|
||||
}
|
||||
if fFive["compress_level"] != "5" {
|
||||
t.Fatalf("compress_level = %q", fFive["compress_level"])
|
||||
}
|
||||
|
||||
if diffs := DiffFields(fImplicit, fFive); len(diffs) != 1 || !strings.Contains(diffs[0], "compress_level") {
|
||||
t.Fatalf("raising the level: %v", diffs)
|
||||
}
|
||||
if diffs := DiffFields(fFive, fImplicit); len(diffs) != 1 || !strings.Contains(diffs[0], "compress_level") {
|
||||
t.Fatalf("lowering the level back to default: %v", diffs)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user