Skip to content

Commit d82b676

Browse files
author
Roman Zatonskiy
committed
added new cmd command for create a new migration
1 parent 29e2587 commit d82b676

4 files changed

Lines changed: 114 additions & 1 deletion

File tree

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ require (
4141
github.com/subosito/gotenv v1.4.2 // indirect
4242
go.uber.org/atomic v1.10.0 // indirect
4343
go.uber.org/multierr v1.9.0 // indirect
44-
golang.org/x/sys v0.5.0 // indirect
44+
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
45+
golang.org/x/sys v0.10.0 // indirect
4546
golang.org/x/term v0.5.0 // indirect
4647
golang.org/x/text v0.7.0 // indirect
48+
golang.org/x/tools v0.11.0 // indirect
4749
google.golang.org/protobuf v1.28.1 // indirect
4850
gopkg.in/ini.v1 v1.67.0 // indirect
4951
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRu
528528
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
529529
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
530530
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
531+
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
531532
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
532533
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
533534
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
@@ -671,6 +672,7 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
671672
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
672673
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
673674
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
675+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
674676
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
675677
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
676678
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
@@ -749,6 +751,8 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
749751
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
750752
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
751753
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
754+
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
755+
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
752756
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
753757
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
754758
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/command/migrate_create.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

migrate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func (b *Bundle) Build(builder di.Builder) error {
109109
di.Provide(command.NewMigrateUp, di.Tags{{
110110
Name: tag,
111111
}}),
112+
di.Provide(command.NewMigrateCreate, di.Tags{{
113+
Name: tag,
114+
}}),
112115
)
113116
}
114117

0 commit comments

Comments
 (0)