github.com/Accefy/pop@v0.0.0-20230428174248-e9f677eab5b9/genny/fizz/cempty/options.go (about)

     1  package cempty
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/gobuffalo/flect/name"
     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  	// Deprecated: use Name directly since TableName doesn't make sense in this generator.
    21  	TableName string
    22  	// Name is the name of the generated file.
    23  	Name string
    24  	// Path is the dir path where to generate the migration files.
    25  	Path string
    26  	// Translator is a Fizz translator to use when asking for SQL migrations.
    27  	Translator nameable
    28  	// Type is the type of migration to generate (sql or fizz).
    29  	Type string
    30  }
    31  
    32  // Validate that options are usable
    33  func (opts *Options) Validate() error {
    34  	if len(opts.Name) == 0 {
    35  		return errors.New("you must set a name for your migration")
    36  	}
    37  	if len(opts.Path) == 0 {
    38  		opts.Path = "migrations"
    39  	}
    40  	timestamp := nowFunc().UTC().Format("20060102150405")
    41  	opts.Name = fmt.Sprintf("%s_%s", timestamp, name.New(opts.Name).Underscore())
    42  
    43  	if len(opts.Type) == 0 {
    44  		opts.Type = "fizz"
    45  	}
    46  	if opts.Type != "fizz" && opts.Type != "sql" {
    47  		return fmt.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  }