github.com/resonatecoop/user-api@v1.0.0-13.0.20220915120639-05dc9c04014a/migrations/22052021010103_new_tables.go (about)

     1  package migrations
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/uptrace/bun"
     8  
     9  	"github.com/resonatecoop/user-api/model"
    10  )
    11  
    12  func init() {
    13  
    14  	// Drop and create tables.
    15  	models := []interface{}{
    16  		(*model.Credit)(nil),
    17  		(*model.UserMembership)(nil),
    18  		(*model.StripeUser)(nil),
    19  		(*model.ShareTransaction)(nil),
    20  		(*model.MembershipClass)(nil),
    21  	}
    22  
    23  	Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
    24  		fmt.Print(" [up migration] ")
    25  
    26  		for _, model := range models {
    27  			_, err := db.NewDropTable().Model(model).IfExists().Exec(ctx)
    28  			if err != nil {
    29  				panic(err)
    30  			}
    31  
    32  			_, err = db.NewCreateTable().Model(model).Exec(ctx)
    33  			if err != nil {
    34  				panic(err)
    35  			}
    36  		}
    37  
    38  		return nil
    39  	}, func(ctx context.Context, db *bun.DB) error {
    40  		fmt.Print(" [down migration] ")
    41  		for _, this_model := range models {
    42  			_, err := db.NewDropTable().Model(this_model).IfExists().Exec(ctx)
    43  			if err != nil {
    44  				panic(err)
    45  			}
    46  		}
    47  		return nil
    48  	})
    49  }