github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/internal/plugins/fizz/setup/setup.go (about)

     1  package setup
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/gobuffalo/buffalo-cli/v2/cli/internal/plugins/pop/setup"
    10  	"github.com/gobuffalo/plugins"
    11  	"github.com/gobuffalo/pop/v5"
    12  )
    13  
    14  var _ plugins.Plugin = &Setup{}
    15  var _ setup.Migrater = &Setup{}
    16  
    17  type Setup struct{}
    18  
    19  func (s *Setup) PluginName() string {
    20  	return "fizz/setup"
    21  }
    22  
    23  func (s *Setup) MigrateDB(ctx context.Context, root string, args []string) error {
    24  	if err := pop.LoadConfigFile(); err != nil {
    25  		return err
    26  	}
    27  
    28  	env := os.Getenv("GO_ENV")
    29  	if len(env) == 0 {
    30  		env = "development"
    31  	}
    32  
    33  	conn, ok := pop.Connections[env]
    34  	if !ok {
    35  		return fmt.Errorf("no connection found for %s", env)
    36  	}
    37  
    38  	mg := filepath.Join(root, "migrations")
    39  	mig, err := pop.NewFileMigrator(mg, conn)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	return mig.Up()
    44  }