Files
voidandClaude Sonnet 4.6 366d61ca25 Add archer import and allow date-mode bulk delete
archer import replays a sealed archive into an existing table: shard
selection (`--select`), SHA-256 pre-verification, idempotent modes
(`ignore`/`insert`/`replace`), and both LOAD DATA and batched-INSERT
engines. It reads manifest.jsonl strictly so corruption is reported
instead of skipped, and supports --dry-run, --allow-partial, and
per-shard workers.

--bulk-delete no longer rejects --shard-by date. Range deletion now
derives its bounds from the chunk's actual keys (date scanners rewrite
LastKey into a resumption cursor) and keeps safe-auto-increment
semantics by excluding the current max key. A startup warning documents
the monotonic-shard-column assumption.

Also splits CLI/format/sharding/progress/safety reference docs out of
the README, and adds the corresponding OpenSpec change artifacts.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-08 21:14:23 +08:00

117 lines
3.1 KiB
Go

package cli
import (
"strings"
"time"
"archer/internal/archererr"
)
// Validate checks required flags and the inter-parameter dependency rules.
// The shard column's existence and type are checked later, after metadata is
// loaded, so the error can list candidate columns.
func (c *Config) Validate() error {
if c.SourceDSN == "" {
return cfgErr("--source is required")
}
if c.Table == "" {
return cfgErr("--table is required")
}
if strings.TrimSpace(c.Where) == "" {
return cfgErr("--where is required")
}
if !c.HasFile() && c.DestDSN == "" && !c.Purge {
return cfgErr("at least one output target is required: --file, --dest or --purge")
}
if c.ChunkSize <= 0 {
return cfgErr("--chunk-size must be greater than 0")
}
if c.Retries < 0 {
return cfgErr("--retries must be 0 or greater")
}
if err := c.Progress.resolve(); err != nil {
return cfgErr("%v", err)
}
if c.ProgressInterval <= 0 {
return cfgErr("--progress-interval must be greater than 0")
}
switch c.ShardBy {
case "none", "count", "date":
default:
return cfgErr("--shard-by must be one of none, count, date")
}
switch c.ShardBy {
case "date":
if c.ShardRows < 0 {
return cfgErr("--shard-rows must be 0 or greater")
}
default:
if c.ShardColumn != "" {
return cfgErr("--shard-column is only valid with --shard-by date")
}
if c.ShardColumnType != "" {
return cfgErr("--shard-column-type is only valid with --shard-by date")
}
if c.ZeroDate != "" {
return cfgErr("--zero-date is only valid with --shard-by date")
}
}
if c.ShardBy == "none" && c.ShardRows != 0 {
return cfgErr("--shard-rows is only valid with --shard-by count or date")
}
if c.ShardBy == "count" && c.ShardRows == 0 {
return cfgErr("--shard-rows must be greater than 0 for --shard-by count")
}
if c.BulkDelete && !c.Purge {
return cfgErr("--bulk-delete requires --purge")
}
switch c.Format {
case "dump", "csv", "jsonl":
default:
return cfgErr("--format must be one of dump, csv, jsonl")
}
switch c.Compress {
case "none", "gzip", "zstd", "xz":
default:
return cfgErr("--compress must be one of none, gzip, zstd, xz")
}
if c.CompressLevel < 1 || c.CompressLevel > 9 {
return cfgErr("--compress-level must be between 1 and 9")
}
if c.Compress == "none" && c.CompressLevel != 3 {
return cfgErr("--compress-level is only valid when compression is enabled")
}
switch c.ZeroDate {
case "", "bucket":
default:
return cfgErr("--zero-date must be empty or bucket")
}
if _, err := time.LoadLocation(c.ShardTimeZone); err != nil {
return cfgErr("--shard-time-zone %q is not a valid location: %v", c.ShardTimeZone, err)
}
if _, err := c.SourceConfig(); err != nil {
return cfgErr("--source: %v", err)
}
if _, err := c.DestConfig(); err != nil {
return cfgErr("--dest: %v", err)
}
if c.CheckReplicaLag != "" {
if err := validateDSN(c.CheckReplicaLag); err != nil {
return cfgErr("--check-replica-lag: %v", err)
}
}
return nil
}
func validateDSN(dsn string) error {
_, err := normalize(dsn)
return err
}
func cfgErr(format string, args ...any) error {
return archererr.Newf(archererr.KindConfig, format, args...)
}