Merge pull request #1490 from smallstep/dry-run-migration

Add option to dry-run the migration
This commit is contained in:
Mariano Cano 2023-07-24 16:39:39 -07:00 committed by GitHub
commit c9df65ebae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,6 +50,16 @@ var (
} }
) )
type DB interface {
CreateTable([]byte) error
Set(bucket, key, value []byte) error
}
type dryRunDB struct{}
func (*dryRunDB) CreateTable([]byte) error { return nil }
func (*dryRunDB) Set(bucket, key, value []byte) error { return nil }
func usage(fs *flag.FlagSet) { func usage(fs *flag.FlagSet) {
name := filepath.Base(os.Args[0]) name := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "%s is a tool to migrate data from BadgerDB to MySQL or PostgreSQL.\n", name) fmt.Fprintf(os.Stderr, "%s is a tool to migrate data from BadgerDB to MySQL or PostgreSQL.\n", name)
@ -57,14 +67,15 @@ func usage(fs *flag.FlagSet) {
fmt.Fprintf(os.Stderr, " %s [-v1|-v2] -dir=<path> [-value-dir=<path>] -type=type -database=<source>\n", name) fmt.Fprintf(os.Stderr, " %s [-v1|-v2] -dir=<path> [-value-dir=<path>] -type=type -database=<source>\n", name)
fmt.Fprintln(os.Stderr, "\nExamples:") fmt.Fprintln(os.Stderr, "\nExamples:")
fmt.Fprintf(os.Stderr, " %s -v1 -dir /var/lib/step-ca/db -type=mysql -database \"user@unix/step_ca\"\n", name) fmt.Fprintf(os.Stderr, " %s -v1 -dir /var/lib/step-ca/db -type=mysql -database \"user@unix/step_ca\"\n", name)
fmt.Fprintf(os.Stderr, " %s -v2 -dir /var/lib/step-ca/db -type=mysql -database \"user:password@tcp(localhost:3306)/step_ca\"\n", name) fmt.Fprintf(os.Stderr, " %s -v1 -dir /var/lib/step-ca/db -type=mysql -database \"user:password@tcp(localhost:3306)/step_ca\"\n", name)
fmt.Fprintf(os.Stderr, " %s -v2 -dir /var/lib/step-ca/db -type=postgresql -database \"user=postgres dbname=step_ca\"\n", name) fmt.Fprintf(os.Stderr, " %s -v2 -dir /var/lib/step-ca/db -type=postgresql -database \"user=postgres dbname=step_ca\"\n", name)
fmt.Fprintf(os.Stderr, " %s -v2 -dir /var/lib/step-ca/db -dry-run\"\n", name)
fmt.Fprintln(os.Stderr, "\nOptions:") fmt.Fprintln(os.Stderr, "\nOptions:")
fs.PrintDefaults() fs.PrintDefaults()
} }
func main() { func main() {
var v1, v2 bool var v1, v2, dryRun bool
var dir, valueDir string var dir, valueDir string
var typ, database string var typ, database string
var key string var key string
@ -78,6 +89,7 @@ func main() {
fs.StringVar(&typ, "type", "", "the destination database type to use") fs.StringVar(&typ, "type", "", "the destination database type to use")
fs.StringVar(&database, "database", "", "the destination driver-specific data source name") fs.StringVar(&database, "database", "", "the destination driver-specific data source name")
fs.StringVar(&key, "key", "", "the key used to resume the migration") fs.StringVar(&key, "key", "", "the key used to resume the migration")
fs.BoolVar(&dryRun, "dry-run", false, "runs the migration scripts without writing anything")
fs.Usage = func() { usage(fs) } fs.Usage = func() { usage(fs) }
fs.Parse(os.Args[1:]) fs.Parse(os.Args[1:])
@ -86,9 +98,9 @@ func main() {
fatal("flag -v1 or -v2 are required") fatal("flag -v1 or -v2 are required")
case dir == "": case dir == "":
fatal("flag -dir is required") fatal("flag -dir is required")
case typ != "postgresql" && typ != "mysql": case typ != "postgresql" && typ != "mysql" && !dryRun:
fatal(`flag -type must be "postgresql" or "mysql"`) fatal(`flag -type must be "postgresql" or "mysql"`)
case database == "": case database == "" && !dryRun:
fatal("flag --database required") fatal("flag --database required")
} }
@ -115,10 +127,15 @@ func main() {
} }
} }
db, err := nosql.New(typ, database) var db DB
if dryRun {
db = &dryRunDB{}
} else {
db, err = nosql.New(typ, database)
if err != nil { if err != nil {
fatal("error opening %s database: %v", typ, err) fatal("error opening %s database: %v", typ, err)
} }
}
allTables := append([]string{}, authorityTables...) allTables := append([]string{}, authorityTables...)
allTables = append(allTables, acmeTables...) allTables = append(allTables, acmeTables...)