Skip to content

Commit bf7ab05

Browse files
committed
add revision generation with command
1 parent 695ac69 commit bf7ab05

9 files changed

Lines changed: 409 additions & 48 deletions

File tree

cmd/qrafter-migrations-init/main.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

cmd/qrafter-migrations/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Command qrafter-migrations runs qrafter migration tools.
2+
package main
3+
4+
import (
5+
"os"
6+
7+
qmig "github.com/SennovE/qrafter/migrations"
8+
)
9+
10+
func main() {
11+
if err := qmig.RunMigrationsCommand(os.Args[1:]); err != nil {
12+
panic(err)
13+
}
14+
}

migrations/cli_params.go

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ package migrations
33
import (
44
"flag"
55
"fmt"
6+
"time"
67
)
78

8-
func revisionCommandOptionsFromArgs(args []string) (string, *configOptions, error) {
9-
fs := flag.NewFlagSet("revision-command", flag.ContinueOnError)
9+
const (
10+
defaultMigrationDirectory = "./migrations"
11+
defaultMigrationComment = "migration"
12+
defaultRevisionTimeout = 30 * time.Second
13+
)
14+
15+
func configOptionsFromArgs(args []string) (string, *configOptions, error) {
16+
fs := flag.NewFlagSet("migrations-init", flag.ContinueOnError)
1017

1118
var path string
1219
var options configOptions
1320

1421
fs.StringVar(
1522
&path,
16-
"path",
17-
"",
18-
"Path for generated cmd file",
23+
"dir",
24+
defaultMigrationDirectory,
25+
"directory for generated qrafter config file",
1926
)
2027

2128
fs.StringVar(
@@ -72,3 +79,85 @@ func revisionCommandOptionsFromArgs(args []string) (string, *configOptions, erro
7279

7380
return path, &options, nil
7481
}
82+
83+
type revisionOptions struct {
84+
WorkDir string
85+
MigrationDir string
86+
ConfigImportPath string
87+
Comment string
88+
GoBinary string
89+
Timeout time.Duration
90+
}
91+
92+
func revisionOptionsFromArgs(args []string) (*revisionOptions, error) {
93+
fs := flag.NewFlagSet("migrations-revision", flag.ContinueOnError)
94+
95+
options := revisionOptions{
96+
WorkDir: ".",
97+
MigrationDir: defaultMigrationDirectory,
98+
Comment: defaultMigrationComment,
99+
GoBinary: "go",
100+
Timeout: defaultRevisionTimeout,
101+
}
102+
103+
fs.StringVar(
104+
&options.WorkDir,
105+
"workdir",
106+
options.WorkDir,
107+
"project directory containing go.mod",
108+
)
109+
110+
fs.StringVar(
111+
&options.MigrationDir,
112+
"dir",
113+
options.MigrationDir,
114+
"directory containing qrafter_config.go and generated migrations",
115+
)
116+
117+
fs.StringVar(
118+
&options.ConfigImportPath,
119+
"config-import",
120+
"",
121+
"Go import path for package containing MigrationConfig",
122+
)
123+
124+
fs.StringVar(
125+
&options.Comment,
126+
"comment",
127+
options.Comment,
128+
"migration comment",
129+
)
130+
131+
fs.StringVar(
132+
&options.GoBinary,
133+
"go",
134+
options.GoBinary,
135+
"go binary used for the temporary build",
136+
)
137+
138+
fs.DurationVar(
139+
&options.Timeout,
140+
"timeout",
141+
options.Timeout,
142+
"timeout for the temporary build and schema diff",
143+
)
144+
145+
if err := fs.Parse(args); err != nil {
146+
return nil, err
147+
}
148+
149+
if options.WorkDir == "" {
150+
return nil, fmt.Errorf("missing required flag: --workdir")
151+
}
152+
if options.MigrationDir == "" {
153+
return nil, fmt.Errorf("missing required flag: --dir")
154+
}
155+
if options.GoBinary == "" {
156+
return nil, fmt.Errorf("missing required flag: --go")
157+
}
158+
if options.Timeout <= 0 {
159+
return nil, fmt.Errorf("timeout must be positive")
160+
}
161+
162+
return &options, nil
163+
}

migrations/command.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package migrations
2+
3+
import "fmt"
4+
5+
// RunMigrationsCommand dispatches qrafter migration CLI subcommands.
6+
func RunMigrationsCommand(args []string) error {
7+
if len(args) == 0 {
8+
return fmt.Errorf("usage: qrafter-migrations <init|revision> [flags]")
9+
}
10+
11+
switch args[0] {
12+
case "init":
13+
return generateMigrationsConfig(args[1:])
14+
case "revision":
15+
return generateMigrationRevision(args[1:])
16+
default:
17+
return fmt.Errorf("unknown migrations command %q", args[0])
18+
}
19+
}

0 commit comments

Comments
 (0)