Skip to content

Instantly share code, notes, and snippets.

@bastianccm
Created February 26, 2020 13:09
Show Gist options
  • Select an option

  • Save bastianccm/108b4feeb95320dff438024e7e2b1a0f to your computer and use it in GitHub Desktop.

Select an option

Save bastianccm/108b4feeb95320dff438024e7e2b1a0f to your computer and use it in GitHub Desktop.
package db
import (
"log"
"flamingo.me/dingo"
"github.com/jinzhu/gorm"
"github.com/spf13/cobra"
)
type Model interface{}
type Module struct{}
func (*Module) Configure(injector *dingo.Injector) {
injector.Bind(new(gorm.DB)).ToProvider(func() *gorm.DB {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic(err)
}
return db
}).In(dingo.ChildSingleton)
injector.BindMulti(new(cobra.Command)).ToProvider(func(models []Model, db *gorm.DB) *cobra.Command {
return &cobra.Command{
Use: "migrate",
Run: func(cmd *cobra.Command, args []string) {
for _, model := range models {
log.Printf("Migrating %T", model)
db.AutoMigrate(model)
}
},
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment