migrate_upto.go 903 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "code.osinet.fr/fgm/kurz/infrastructure"
  4. "os"
  5. _ "code.osinet.fr/fgm/kurz/migrations"
  6. "github.com/pressly/goose"
  7. "github.com/spf13/cobra"
  8. )
  9. var cmdMigrateUpTo = &cobra.Command{
  10. Args: migrateVersionValidator,
  11. Long: "Migrate the Kurz schema to the most recent version available. Kurz version of 'goose up'.",
  12. Run: migrateUpToHandler,
  13. Short: "Migrate to latest version",
  14. Use: "up-to <version>",
  15. }
  16. func init() {
  17. cmdMigrate.AddCommand(cmdMigrateUpTo)
  18. }
  19. // migrateUpToHandler implements the "kurzd migrate up-to" command.
  20. func migrateUpToHandler(cmd *cobra.Command, args []string) {
  21. dbDriver, dbDsn := infrastructure.ParseDbCred()
  22. db, err := infrastructure.DbDial(dbDriver, dbDsn)
  23. if err != nil {
  24. panic(err)
  25. }
  26. defer db.Close()
  27. cwd, err := os.Getwd()
  28. if err != nil {
  29. panic(err)
  30. }
  31. goose.SetDialect(dbDriver)
  32. goose.UpTo(db, cwd, migrateTargetVersion)
  33. }