Make db migration tooling more flexible

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-10-15 23:17:41 +02:00
parent 4886af2756
commit 0c11d24148
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5

View file

@ -2,21 +2,47 @@ package main
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/Luzifer/go-latestver/internal/database" "github.com/Luzifer/go-latestver/internal/database"
"github.com/Luzifer/rconfig/v2"
) )
func main() { var cfg = struct {
src, err := database.NewClient("sqlite", "") FromStorage string `flag:"from-storage" description:"Storage type to migrate from" validate:"nonzero"`
if err != nil { FromStorageDSN string `flag:"from-storage-dsn" description:"DSN for the 'from' storage" validate:"nonzero"`
panic(errors.Wrap(err, "opening src database")) 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")
} }
dest, err := database.NewClient("mysql", "")
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 { if err != nil {
panic(errors.Wrap(err, "opening dest database")) 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 { if err := src.Migrate(dest); err != nil {
panic(errors.Wrap(err, "migrating to dest database")) logrus.WithError(err).Fatal("execute migration")
} }
logrus.Info("your database has been migrated")
} }