github.com/kunlun-qilian/sqlx/v3@v3.0.0/migration/migration.go (about) 1 package migration 2 3 import ( 4 "context" 5 "io" 6 7 contextx "github.com/go-courier/x/context" 8 9 "github.com/kunlun-qilian/sqlx/v3" 10 "github.com/kunlun-qilian/sqlx/v3/enummeta" 11 ) 12 13 type contextKeyMigrationOutput struct{} 14 15 func MigrationOutputFromContext(ctx context.Context) io.Writer { 16 if opts, ok := ctx.Value(contextKeyMigrationOutput{}).(io.Writer); ok { 17 if opts != nil { 18 return opts 19 } 20 } 21 return nil 22 } 23 24 func MustMigrate(db sqlx.DBExecutor, w io.Writer) { 25 if err := Migrate(db, w); err != nil { 26 panic(err) 27 } 28 } 29 30 func Migrate(db sqlx.DBExecutor, output io.Writer) error { 31 ctx := contextx.WithValue(db.Context(), contextKeyMigrationOutput{}, output) 32 33 if err := db.(sqlx.Migrator).Migrate(ctx, db); err != nil { 34 return err 35 } 36 if output == nil { 37 if err := enummeta.SyncEnum(db); err != nil { 38 return err 39 } 40 } 41 return nil 42 }