func_migration.go 839 B

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