|
| 1 | +// Copyright 2018 Sergey Novichkov. All rights reserved. |
| 2 | +// For the full copyright and license information, please view the LICENSE |
| 3 | +// file that was distributed with this source code. |
| 4 | + |
| 5 | +package command |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/gozix/di" |
| 18 | + "github.com/spf13/cobra" |
| 19 | + "go.uber.org/zap" |
| 20 | +) |
| 21 | + |
| 22 | +const migrationFileExtension = "sql" |
| 23 | + |
| 24 | +// NewMigrateCreate is subcommand constructor. |
| 25 | +func NewMigrateCreate(ctn di.Container) *cobra.Command { |
| 26 | + return &cobra.Command{ |
| 27 | + Use: "create", |
| 28 | + Short: "Create named migration", |
| 29 | + Long: `Normally each migration is run within a transaction. |
| 30 | + However some SQL commands (for example creating an index concurrently in PostgreSQL) |
| 31 | + cannot be executed inside a transaction. In order to execute such a command in a migration, |
| 32 | + the migration can be run using the notransaction option https://github.com/rubenv/sql-migrate`, |
| 33 | + Args: cobra.ExactArgs(1), |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + return ctn.Call(func(ctx context.Context, logger *zap.Logger) (err error) { |
| 36 | + var ( |
| 37 | + name = args[0] |
| 38 | + path = cmd.Flag("path").Value.String() |
| 39 | + ) |
| 40 | + if !filepath.IsAbs(path) { |
| 41 | + var appPath, ok = ctx.Value("app.path").(string) |
| 42 | + if !ok { |
| 43 | + return errors.New("app.path is undefined") |
| 44 | + } |
| 45 | + |
| 46 | + path = filepath.Join(appPath, path) |
| 47 | + } |
| 48 | + |
| 49 | + if _, err = create(path, name, migrationFileExtension); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + logger.Info( |
| 54 | + "Created migration", |
| 55 | + zap.String("migration", filepath.Join(path, name+"."+migrationFileExtension)), |
| 56 | + ) |
| 57 | + |
| 58 | + return nil |
| 59 | + }) |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func create(migrationDir string, name string, extension string) (string, error) { |
| 65 | + var ( |
| 66 | + fileName string |
| 67 | + fileContent string |
| 68 | + file *os.File |
| 69 | + err error |
| 70 | + ) |
| 71 | + |
| 72 | + fileName = filepath.Join( |
| 73 | + filepath.Clean(migrationDir), |
| 74 | + fmt.Sprintf("%s_%s.%s", |
| 75 | + // version |
| 76 | + strconv.FormatInt(time.Now().Unix(), 10), |
| 77 | + name, |
| 78 | + extension, |
| 79 | + ), |
| 80 | + ) |
| 81 | + |
| 82 | + fileContent = strings.Join([]string{ |
| 83 | + "-- +migrate Up", |
| 84 | + "-- SQL in section 'Up' is executed when this migration is applied", |
| 85 | + "", |
| 86 | + "", |
| 87 | + "", |
| 88 | + "-- +migrate Down", |
| 89 | + "-- SQL section 'Down' is executed when this migration is rolled back", |
| 90 | + "", |
| 91 | + }, "\n") |
| 92 | + |
| 93 | + file, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) |
| 94 | + if err != nil { |
| 95 | + return fileName, err |
| 96 | + } |
| 97 | + defer func() { |
| 98 | + _ = file.Close() |
| 99 | + }() |
| 100 | + |
| 101 | + _, err = file.WriteString(fileContent) |
| 102 | + |
| 103 | + return fileName, err |
| 104 | +} |
0 commit comments