github.com/dolanor/pop@v4.13.0+incompatible/genny/fizz/cempty/options.go (about)

     1  package cempty
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/gobuffalo/flect/name"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  var nowFunc = time.Now
    12  
    13  type nameable interface {
    14  	Name() string
    15  }
    16  
    17  // Options for the empty migration generator.
    18  type Options struct {
    19  	// TableName is the name of the table.
    20  	TableName string
    21  	// Name is the name of the generated file.
    22  	Name string
    23  	// Path is the dir path where to generate the migration files.
    24  	Path string
    25  	// Translator is a Fizz translator to use when asking for SQL migrations.
    26  	Translator nameable
    27  	// Type is the type of migration to generate (sql or fizz).
    28  	Type string
    29  }
    30  
    31  // Validate that options are usuable
    32  func (opts *Options) Validate() error {
    33  	if len(opts.TableName) == 0 {
    34  		return errors.New("you must set a name for your table")
    35  	}
    36  	if len(opts.Path) == 0 {
    37  		opts.Path = "migrations"
    38  	}
    39  	if len(opts.Name) == 0 {
    40  		timestamp := nowFunc().UTC().Format("20060102150405")
    41  		opts.Name = fmt.Sprintf("%s_create_%s", timestamp, name.New(opts.TableName).Tableize())
    42  	}
    43  	if len(opts.Type) == 0 {
    44  		opts.Type = "fizz"
    45  	}
    46  	if opts.Type != "fizz" && opts.Type != "sql" {
    47  		return errors.Errorf("%s migration type is not allowed", opts.Type)
    48  	}
    49  	if opts.Type == "sql" && opts.Translator == nil {
    50  		return errors.New("sql migrations require a fizz translator")
    51  	}
    52  	return nil
    53  }