Skip to content

Commit b35692c

Browse files
authored
migrations: Update user-facing commands (sourcegraph#30411)
1 parent 6e9896c commit b35692c

File tree

7 files changed

+199
-75
lines changed

7 files changed

+199
-75
lines changed

dev/sg/sg_migration.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ var (
3434
LongHelp: cliutil.ConstructLongHelp(),
3535
}
3636

37-
upCommand = cliutil.Up("sg migration", runMigration, stdout.Out)
38-
downCommand = cliutil.Down("sg migration", runMigration, stdout.Out)
37+
upCommand = cliutil.Up("sg migration", runMigration, stdout.Out)
38+
upToCommand = cliutil.UpTo("sg migration", runMigration, stdout.Out)
39+
UndoCommand = cliutil.Undo("sg migration", runMigration, stdout.Out)
40+
downToCommand = cliutil.DownTo("sg migration", runMigration, stdout.Out)
3941

4042
migrationSquashFlagSet = flag.NewFlagSet("sg migration squash", flag.ExitOnError)
4143
migrationSquashDatabaseNameFlag = migrationSquashFlagSet.String("db", db.DefaultDatabase.Name, "The target database instance")
@@ -60,7 +62,9 @@ var (
6062
Subcommands: []*ffcli.Command{
6163
migrationAddCommand,
6264
upCommand,
63-
downCommand,
65+
upToCommand,
66+
UndoCommand,
67+
downToCommand,
6468
migrationSquashCommand,
6569
},
6670
}

internal/database/migration/cliutil/down.go

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cliutil
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"strconv"
8+
9+
"github.com/peterbourgon/ff/v3/ffcli"
10+
11+
"github.com/sourcegraph/sourcegraph/internal/database/migration/runner"
12+
"github.com/sourcegraph/sourcegraph/lib/output"
13+
)
14+
15+
func DownTo(commandName string, run RunFunc, out *output.Output) *ffcli.Command {
16+
var (
17+
flagSet = flag.NewFlagSet(fmt.Sprintf("%s downto", commandName), flag.ExitOnError)
18+
schemaNameFlag = flagSet.String("db", "", `The target schema to migrate.`)
19+
targetFlag = flagSet.String("target", "", "Revert all children of the given target.")
20+
)
21+
22+
exec := func(ctx context.Context, args []string) error {
23+
if len(args) != 0 {
24+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: too many arguments"))
25+
return flag.ErrHelp
26+
}
27+
28+
if *schemaNameFlag == "" {
29+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply a schema via -db"))
30+
return flag.ErrHelp
31+
}
32+
33+
if *targetFlag == "" {
34+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply a migration target via -target"))
35+
return flag.ErrHelp
36+
}
37+
38+
version, err := strconv.Atoi(*targetFlag)
39+
if err != nil {
40+
return err
41+
}
42+
43+
return run(ctx, runner.Options{
44+
Operations: []runner.MigrationOperation{
45+
{
46+
SchemaName: *schemaNameFlag,
47+
Type: runner.MigrationOperationTypeTargetedDown,
48+
TargetVersion: version,
49+
},
50+
},
51+
})
52+
}
53+
54+
return &ffcli.Command{
55+
Name: "downto",
56+
ShortUsage: fmt.Sprintf("%s downto -db=<schema> -target=<target>,<target>,...", commandName),
57+
ShortHelp: `Revert any applied migrations that are children of the given targets - this effectively "resets" the database to the target migration`,
58+
FlagSet: flagSet,
59+
Exec: exec,
60+
LongHelp: ConstructLongHelp(),
61+
}
62+
}

internal/database/migration/cliutil/flags.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ func Flags(commandName string, run RunFunc, out *output.Output) *ffcli.Command {
2626
},
2727
Subcommands: []*ffcli.Command{
2828
Up(commandName, run, out),
29-
Down(commandName, run, out),
29+
UpTo(commandName, run, out),
30+
Undo(commandName, run, out),
31+
DownTo(commandName, run, out),
3032
},
3133
}
3234
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cliutil
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
8+
"github.com/peterbourgon/ff/v3/ffcli"
9+
10+
"github.com/sourcegraph/sourcegraph/internal/database/migration/runner"
11+
"github.com/sourcegraph/sourcegraph/lib/output"
12+
)
13+
14+
func Undo(commandName string, run RunFunc, out *output.Output) *ffcli.Command {
15+
var (
16+
flagSet = flag.NewFlagSet(fmt.Sprintf("%s undo", commandName), flag.ExitOnError)
17+
schemaNameFlag = flagSet.String("db", "", `The target schema to migrate.`)
18+
)
19+
20+
exec := func(ctx context.Context, args []string) error {
21+
if len(args) != 0 {
22+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: too many arguments"))
23+
return flag.ErrHelp
24+
}
25+
26+
if *schemaNameFlag == "" {
27+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply a schema via -db"))
28+
return flag.ErrHelp
29+
}
30+
31+
return run(ctx, runner.Options{
32+
Operations: []runner.MigrationOperation{
33+
{
34+
SchemaName: *schemaNameFlag,
35+
Type: runner.MigrationOperationTypeRevert,
36+
},
37+
},
38+
})
39+
}
40+
41+
return &ffcli.Command{
42+
Name: "undo",
43+
ShortUsage: fmt.Sprintf("%s undo -db=<schema>", commandName),
44+
ShortHelp: `Revert the last migration applied - useful in local development`,
45+
FlagSet: flagSet,
46+
Exec: exec,
47+
LongHelp: ConstructLongHelp(),
48+
}
49+
}

internal/database/migration/cliutil/up.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"strings"
78

89
"github.com/peterbourgon/ff/v3/ffcli"
910

@@ -14,35 +15,30 @@ import (
1415

1516
func Up(commandName string, run RunFunc, out *output.Output) *ffcli.Command {
1617
var (
17-
upFlagSet = flag.NewFlagSet(fmt.Sprintf("%s up", commandName), flag.ExitOnError)
18-
upDatabaseNameFlag = upFlagSet.String("db", "all", `The target database instance. Supply "all" (the default) to migrate all databases.`)
19-
upTargetFlag = upFlagSet.Int("target", 0, "Apply all migrations up to this target. Zero (the default) applies all migrations.")
18+
flagSet = flag.NewFlagSet(fmt.Sprintf("%s up", commandName), flag.ExitOnError)
19+
schemaNameFlag = flagSet.String("db", "all", `The target schema(s) to migrate. Comma-separated values are accepted. Supply "all" (the default) to migrate all schemas.`)
2020
)
2121

22-
execUp := func(ctx context.Context, args []string) error {
22+
exec := func(ctx context.Context, args []string) error {
2323
if len(args) != 0 {
2424
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: too many arguments"))
2525
return flag.ErrHelp
2626
}
2727

28-
if *upDatabaseNameFlag == "all" && *upTargetFlag != 0 {
29-
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply -db to migrate a specific database"))
28+
schemaNames := strings.Split(*schemaNameFlag, ",")
29+
if len(schemaNames) == 0 {
30+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply a schema via -db"))
3031
return flag.ErrHelp
3132
}
32-
33-
var databaseNames []string
34-
if *upDatabaseNameFlag == "all" {
35-
databaseNames = append(databaseNames, schemas.SchemaNames...)
36-
} else {
37-
databaseNames = append(databaseNames, *upDatabaseNameFlag)
33+
if len(schemaNames) == 1 && schemaNames[0] == "all" {
34+
schemaNames = schemas.SchemaNames
3835
}
3936

4037
operations := []runner.MigrationOperation{}
41-
for _, databaseName := range databaseNames {
38+
for _, schemaName := range schemaNames {
4239
operations = append(operations, runner.MigrationOperation{
43-
SchemaName: databaseName,
44-
Type: runner.MigrationOperationTypeTargetedUp,
45-
TargetVersion: *upTargetFlag,
40+
SchemaName: schemaName,
41+
Type: runner.MigrationOperationTypeUpgrade,
4642
})
4743
}
4844

@@ -53,10 +49,10 @@ func Up(commandName string, run RunFunc, out *output.Output) *ffcli.Command {
5349

5450
return &ffcli.Command{
5551
Name: "up",
56-
ShortUsage: fmt.Sprintf("%s up [-db=all] [-target=0]", commandName),
57-
ShortHelp: "Run up migrations",
58-
FlagSet: upFlagSet,
59-
Exec: execUp,
52+
ShortUsage: fmt.Sprintf("%s up -db=<schema>", commandName),
53+
ShortHelp: "Apply all migrations",
54+
FlagSet: flagSet,
55+
Exec: exec,
6056
LongHelp: ConstructLongHelp(),
6157
}
6258
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cliutil
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"strconv"
8+
9+
"github.com/peterbourgon/ff/v3/ffcli"
10+
11+
"github.com/sourcegraph/sourcegraph/internal/database/migration/runner"
12+
"github.com/sourcegraph/sourcegraph/lib/output"
13+
)
14+
15+
func UpTo(commandName string, run RunFunc, out *output.Output) *ffcli.Command {
16+
var (
17+
flagSet = flag.NewFlagSet(fmt.Sprintf("%s upto", commandName), flag.ExitOnError)
18+
schemaNameFlag = flagSet.String("db", "", `The target schema to migrate.`)
19+
targetFlag = flagSet.String("target", "", "The migration to apply.")
20+
)
21+
22+
exec := func(ctx context.Context, args []string) error {
23+
if len(args) != 0 {
24+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: too many arguments"))
25+
return flag.ErrHelp
26+
}
27+
28+
if *schemaNameFlag == "" {
29+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply a schema via -db"))
30+
return flag.ErrHelp
31+
}
32+
33+
if *targetFlag == "" {
34+
out.WriteLine(output.Linef("", output.StyleWarning, "ERROR: supply a migration target via -target"))
35+
return flag.ErrHelp
36+
}
37+
38+
version, err := strconv.Atoi(*targetFlag)
39+
if err != nil {
40+
return err
41+
}
42+
43+
return run(ctx, runner.Options{
44+
Operations: []runner.MigrationOperation{
45+
{
46+
SchemaName: *schemaNameFlag,
47+
Type: runner.MigrationOperationTypeTargetedUp,
48+
TargetVersion: version,
49+
},
50+
},
51+
})
52+
}
53+
54+
return &ffcli.Command{
55+
Name: "upto",
56+
ShortUsage: fmt.Sprintf("%s upto -db=<schema> -target=<target>,<target>,...", commandName),
57+
ShortHelp: "Ensure a given migration has been applied - may apply dependency migrations",
58+
FlagSet: flagSet,
59+
Exec: exec,
60+
LongHelp: ConstructLongHelp(),
61+
}
62+
}

0 commit comments

Comments
 (0)