Skip to content

Commit

Permalink
#3: Change migration seq nums to timestamp generation
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Mar 30, 2023
1 parent 5ed323b commit 7b734bb
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/cloudspannerecosystem/wrench/pkg/spanner"
"github.com/spf13/cobra"

"github.com/cloudspannerecosystem/wrench/pkg/spanner"
)

const (
migrationsDirName = "migrations"
migrationTableName = "SchemaMigrations"
)

const (
createMigrationFileLayout = "20060102150405"
)

// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate",
Expand Down Expand Up @@ -225,37 +230,25 @@ func migrateSet(c *cobra.Command, args []string) error {

func createMigrationFile(dir string, name string, digits int) (string, error) {
if name != "" && !spanner.MigrationNameRegex.MatchString(name) {
return "", errors.New("Invalid migration file name.")
}

ms, err := spanner.LoadMigrations(dir)
if err != nil {
return "", err
}

var v uint = 1
if len(ms) > 0 {
v = ms[len(ms)-1].Version + 1
}
vStr := fmt.Sprint(v)

padding := digits - len(vStr)
if padding > 0 {
vStr = strings.Repeat("0", padding) + vStr
return "", errors.New("invalid migration file name")
}

fileTimestampStr := time.Now().Format(createMigrationFileLayout)
var filename string
if name == "" {
filename = filepath.Join(dir, fmt.Sprintf("%s.sql", vStr))
} else {
filename = filepath.Join(dir, fmt.Sprintf("%s_%s.sql", vStr, name))
filename = filepath.Join(dir, fmt.Sprintf("%s.sql", fileTimestampStr))
if name != "" {
filename = filepath.Join(dir, fmt.Sprintf("%s_%s.sql", fileTimestampStr, name))
}

fp, err := os.Create(filename)
defer func() {
if defErr := fp.Close(); defErr != nil {
err = defErr
}
}()
if err != nil {
return "", err
}
fp.Close()

return filename, nil
}

0 comments on commit 7b734bb

Please sign in to comment.