github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/soda/fizz/creator.go (about)

     1  package fizz
     2  
     3  import (
     4  	_ "embed"
     5  	"fmt"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/wawandco/oxpecker/internal/source"
    10  )
    11  
    12  var (
    13  	//go:embed templates/content.fizz.tmpl
    14  	fizzTemplate string
    15  )
    16  
    17  // FizzCreator model struct for fizz generation files
    18  type Creator struct{}
    19  
    20  // Name is the name of the migration type
    21  func (f Creator) Name() string {
    22  	return "fizz"
    23  }
    24  
    25  // Creates a type or not
    26  func (f Creator) Creates(mtype string) bool {
    27  	return mtype == "fizz"
    28  }
    29  
    30  // Create will create 2 .fizz files for the migration
    31  func (f Creator) Create(dir, name string, args []string) error {
    32  	g := generators.GeneratorFor(name)
    33  
    34  	up, down, err := g.GenerateFizz(name, args)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	timestamp := time.Now().UTC().Format("20060102150405")
    40  	fileName := fmt.Sprintf("%s_%s", timestamp, name)
    41  
    42  	upPath := filepath.Join(dir, fileName+".up.fizz")
    43  	downPath := filepath.Join(dir, fileName+".down.fizz")
    44  
    45  	// Build Up Fizz
    46  	if err := source.Build(upPath, fizzTemplate, up); err != nil {
    47  		return err
    48  	}
    49  
    50  	// Build Down Fizz
    51  	if err := source.Build(downPath, fizzTemplate, down); err != nil {
    52  		return err
    53  	}
    54  
    55  	return nil
    56  }