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

     1  package fizz
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/gobuffalo/fizz"
     7  	"github.com/gobuffalo/flect"
     8  )
     9  
    10  type createTable struct{}
    11  
    12  func (ct *createTable) match(name string) bool {
    13  	return strings.HasPrefix(name, "create_table_")
    14  }
    15  
    16  func (ct *createTable) GenerateFizz(name string, args []string) (string, string, error) {
    17  	var up, down string
    18  	name = strings.TrimPrefix(name, "create_table_")
    19  	if name == "" {
    20  		return up, down, ErrNoTableName
    21  	}
    22  
    23  	table := fizz.NewTable(name, map[string]interface{}{
    24  		"timestamps": false,
    25  	})
    26  
    27  	for _, arg := range args[0:] {
    28  		slice := strings.Split(arg, ":")
    29  		if len(slice) == 1 {
    30  			slice = append(slice, "string")
    31  		}
    32  
    33  		o := fizz.Options{}
    34  		name := flect.Underscore(slice[0])
    35  		colType := columnType(slice[1])
    36  
    37  		if name == "id" {
    38  			o["primary"] = true
    39  		}
    40  
    41  		if strings.HasPrefix(strings.ToLower(slice[1]), "nulls.") {
    42  			o["null"] = true
    43  		}
    44  
    45  		if err := table.Column(name, colType, o); err != nil {
    46  			return up, down, err
    47  		}
    48  	}
    49  
    50  	up = table.Fizz()
    51  	down = table.UnFizz()
    52  
    53  	return up, down, nil
    54  }