github.com/wawandco/oxpecker-plugins@v0.1.1/tools/pop/test.go (about)

     1  package pop
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/gobuffalo/pop/v5"
    10  )
    11  
    12  func (p *Plugin) RunBeforeTest(ctx context.Context, root string, args []string) error {
    13  	_, err := os.Stat(filepath.Join(root, "config", "database.yml"))
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	db, err := pop.Connect("test")
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	fmt.Println(">>> Resetting Database")
    24  	err = db.Dialect.DropDB()
    25  	if err != nil {
    26  		fmt.Printf("could not drop `%v` database, continuing.", db.Dialect.Name())
    27  	}
    28  
    29  	err = db.Dialect.CreateDB()
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	// Running migrations
    35  	fmt.Println(">>> Running migrations")
    36  	ms := filepath.Join(root, "migrations")
    37  	fm, err := pop.NewFileMigrator(ms, db)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	return fm.Up()
    43  }