migrate_down.go 849 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 cmdMigrateDown = &cobra.Command{
  10. Args: cobra.NoArgs,
  11. Long: "Roll back the latest applied Kurz schema migration. Kurz version of 'goose down'.",
  12. Run: migrateDownHandler,
  13. Short: "Roll back latest migration",
  14. Use: "down",
  15. }
  16. func init() {
  17. cmdMigrate.AddCommand(cmdMigrateDown)
  18. }
  19. // migrateDownHandler implements the "kurzd migrate down" command.
  20. func migrateDownHandler(_ *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.Down(db, cwd)
  33. }