migrate_downto.go 915 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 cmdMigrateDownTo = &cobra.Command{
  10. Args: migrateVersionValidator,
  11. Long: "Roll the Kurz schema back to a specific version. Kurz version of 'goose down-to'.",
  12. Run: migrateDownToHandler,
  13. Short: "Roll back to specific version",
  14. Use: "down-to <version>",
  15. }
  16. func init() {
  17. cmdMigrate.AddCommand(cmdMigrateDownTo)
  18. }
  19. // migrateDownToHandler implements the "kurzd migrate down-to" command.
  20. func migrateDownToHandler(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.DownTo(db, cwd, migrateTargetVersion)
  33. }