go-latestver/internal/migrate/migrate.go
Knut Ahlers 0c11d24148
Make db migration tooling more flexible
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2023-10-15 23:17:41 +02:00

48 lines
1.3 KiB
Go

package main
import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/Luzifer/go-latestver/internal/database"
"github.com/Luzifer/rconfig/v2"
)
var cfg = struct {
FromStorage string `flag:"from-storage" description:"Storage type to migrate from" validate:"nonzero"`
FromStorageDSN string `flag:"from-storage-dsn" description:"DSN for the 'from' storage" validate:"nonzero"`
ToStorage string `flag:"to-storage" description:"Storage type to migrate to" validate:"nonzero"`
ToStorageDSN string `flag:"to-storage-dsn" description:"DSN for the 'to' storage" validate:"nonzero"`
}{}
func initApp() error {
rconfig.AutoEnv(true)
if err := rconfig.ParseAndValidate(&cfg); err != nil {
return errors.Wrap(err, "parsing commandline options")
}
return nil
}
func main() {
var err error
if err = initApp(); err != nil {
logrus.WithError(err).Fatal("initializing app")
}
src, err := database.NewClient(cfg.FromStorage, cfg.FromStorageDSN)
if err != nil {
logrus.WithError(err).Fatal("opening from database")
}
dest, err := database.NewClient(cfg.ToStorage, cfg.ToStorageDSN)
if err != nil {
logrus.WithError(err).Fatal("opening to database")
}
if err := src.Migrate(dest); err != nil {
logrus.WithError(err).Fatal("execute migration")
}
logrus.Info("your database has been migrated")
}