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

     1  package sql
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/wawandco/oxpecker/internal/log"
    11  )
    12  
    13  // Creator model struct for fizz generation files
    14  type Creator struct{}
    15  
    16  // Name is the name of the migration type
    17  func (s Creator) Name() string {
    18  	return "sql"
    19  }
    20  
    21  // Creates a type or not
    22  func (f Creator) Creates(mtype string) bool {
    23  	return mtype == "sql"
    24  }
    25  
    26  // Create will create 2 .sql empty files for the migration
    27  func (s Creator) Create(dir, name string, args []string) error {
    28  	timestamp := time.Now().UTC().Format("20060102150405")
    29  	fileName := fmt.Sprintf("%s_%s", timestamp, name)
    30  
    31  	if err := s.createFile(dir, fileName, "up"); err != nil {
    32  		return err
    33  	}
    34  
    35  	if err := s.createFile(dir, fileName, "down"); err != nil {
    36  		return err
    37  	}
    38  
    39  	log.Infof("generated: %s/%s.up.sql", dir, fileName)
    40  	log.Infof("generated: %s/%s.down.sql", dir, fileName)
    41  
    42  	return nil
    43  }
    44  
    45  func (s Creator) createFile(dir, name, runFlag string) error {
    46  	fileName := fmt.Sprintf("%s.%s.sql", name, runFlag)
    47  	file, err := os.Create(filepath.Join(dir, fileName))
    48  	if err != nil {
    49  		return errors.Wrap(err, "error creating file")
    50  	}
    51  
    52  	defer file.Close()
    53  
    54  	return err
    55  }