github.com/eden-framework/sqlx@v0.0.2/migration/migration.go (about)

     1  package migration
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/eden-framework/sqlx"
     7  	"github.com/eden-framework/sqlx/enummeta"
     8  )
     9  
    10  type MigrationOpts struct {
    11  	DryRun bool
    12  }
    13  
    14  var contextKeyMigrationOpts = "#####MigrationOpts#####"
    15  
    16  func MigrationOptsFromContext(ctx context.Context) *MigrationOpts {
    17  	if opts, ok := ctx.Value(contextKeyMigrationOpts).(*MigrationOpts); ok {
    18  		if opts != nil {
    19  			return opts
    20  		}
    21  	}
    22  	return &MigrationOpts{}
    23  }
    24  
    25  func MustMigrate(db sqlx.DBExecutor, opts *MigrationOpts) {
    26  	if err := Migrate(db, opts); err != nil {
    27  		panic(err)
    28  	}
    29  }
    30  
    31  func Migrate(db sqlx.DBExecutor, opts *MigrationOpts) error {
    32  	ctx := context.WithValue(db.Context(), contextKeyMigrationOpts, opts)
    33  
    34  	if err := db.(sqlx.Migrator).Migrate(ctx, db); err != nil {
    35  		return err
    36  	}
    37  	if err := enummeta.SyncEnum(db); err != nil {
    38  		return err
    39  	}
    40  	return nil
    41  }