1
0

func_migration.go 872 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package migres
  2. // FuncMigration enables creating a functional migration using callback functions.
  3. //
  4. // import "github.com/annybs/migres"
  5. //
  6. // type MyBackend struct{}
  7. //
  8. // func (mb *MyBackend) Module() migres.Module {
  9. // return migres.Module{
  10. // "1.0.0": migres.Func(mb.upgradeV1, mb.downgradeV1),
  11. // "2.0.0": migres.Func(mb.upgradeV2, mb.downgradeV2),
  12. // }
  13. // }
  14. //
  15. // In this example MyModule can be defined with multiple upgrade/downgrade functions.
  16. // This may be simpler than defining separate migration structs in many cases.
  17. type FuncMigration struct {
  18. D func() error
  19. U func() error
  20. }
  21. func (fm *FuncMigration) Downgrade() error {
  22. return fm.D()
  23. }
  24. func (fm *FuncMigration) Upgrade() error {
  25. return fm.U()
  26. }
  27. // Func creates a functional migration.
  28. func Func(up, down func() error) *FuncMigration {
  29. return &FuncMigration{
  30. D: down,
  31. U: up,
  32. }
  33. }