Skip to content

Commit

Permalink
added new cmd command for create a new migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Zatonskiy committed Jul 21, 2023
1 parent 29e2587 commit 28a5bc8
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.11.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRu
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
Expand Down Expand Up @@ -671,6 +672,7 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand Down Expand Up @@ -749,6 +751,8 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
105 changes: 105 additions & 0 deletions internal/command/migrate_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2018 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

package command

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/gozix/di"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

// NewMigrateCreate is subcommand constructor.
func NewMigrateCreate(ctn di.Container) *cobra.Command {
return &cobra.Command{
Use: "create",
Short: "Create named migration",
Long: `Normally each migration is run within a transaction.
However some SQL commands (for example creating an index concurrently in PostgreSQL)
cannot be executed inside a transaction. In order to execute such a command in a migration,
the migration can be run using the notransaction option https://github.com/rubenv/sql-migrate`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return ctn.Call(func(ctx context.Context, logger *zap.Logger) (err error) {
const extension string = "sql"

var name = "example"
if len(args) > 0 {
name = args[0]
}

var path = cmd.Flag("path").Value.String()
if !filepath.IsAbs(path) {
var appPath, ok = ctx.Value("app.path").(string)
if !ok {
return errors.New("app.path is undefined")
}

path = filepath.Join(appPath, path)
}

if _, err := create(path, name, extension); err != nil {
return err
}

logger.Info(
"Create migration",
zap.String("migration", filepath.Join(path, name+"."+extension)),
)

return nil
})
},
}
}

func create(migrationDir string, name string, extension string) (string, error) {
fileName := filepath.Join(
filepath.Clean(migrationDir),
fmt.Sprintf("%s_%s.%s",
// version
strconv.FormatInt(time.Now().Unix(), 10),
// name
name,
// extension
extension,
),
)

fileContent := strings.Join([]string{
"-- +migrate Up",
"-- SQL in section 'Up' is executed when this migration is applied",
"SET LOCAL statement_timeout = 30000; -- <miliseconds>",
"",
"",
"",
"-- +migrate Down",
"-- SQL section 'Down' is executed when this migration is rolled back",
"",
}, "\n")

f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return fileName, err
}
defer func(f *os.File) {
_ = f.Close()
}(f)

_, err = f.WriteString(fileContent)
if err != nil {
return fileName, err
}

return fileName, nil
}
3 changes: 3 additions & 0 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (b *Bundle) Build(builder di.Builder) error {
di.Provide(command.NewMigrateUp, di.Tags{{
Name: tag,
}}),
di.Provide(command.NewMigrateCreate, di.Tags{{
Name: tag,
}}),
)
}

Expand Down

0 comments on commit 28a5bc8

Please sign in to comment.