-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
63 lines (51 loc) · 2.1 KB
/
migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cmd
import (
"github.com/logrusorgru/aurora/v3"
"github.com/pkg/errors"
"github.com/thomasduchatelle/ephoto/cmd/fphoto/printer"
"github.com/thomasduchatelle/ephoto/tools/dphotoops/migrator"
"os"
"time"
"github.com/spf13/cobra"
)
var migrateArg = struct {
tableName string
snsARN string
repopulate bool
indexTransformation bool
albumOwnerTransformation bool
}{}
// rootCmd represents the base command when called without any subcommands
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Apply migration scripts on a DynamoDB table (index upgrades, v1 -> v2, album's owner fix, ...)",
Run: func(cmd *cobra.Command, args []string) {
if migrateArg.tableName == "" {
printer.Error(errors.Errorf("--table is mandatory"), "")
os.Exit(10)
}
printer.Info("Start to migrate table %s ...", aurora.Cyan(migrateArg.tableName))
start := time.Now()
count, err := migrator.Migrate(migrateArg.tableName, migrateArg.snsARN, migrateArg.repopulate, buildTransformationsSlice())
printer.FatalIfError(err, 1)
elapsed := int(time.Now().Sub(start).Seconds())
printer.Success("table %s with %d records has been migrated in %d seconds", aurora.Cyan(migrateArg.tableName), count, elapsed)
},
}
func buildTransformationsSlice() (transformations []interface{}) {
if migrateArg.indexTransformation {
transformations = append(transformations, new(migrator.TransformationUpDateIndex))
}
if migrateArg.albumOwnerTransformation {
transformations = append(transformations, new(migrator.TransformationAlbumOwner))
}
return
}
func init() {
rootCmd.AddCommand(migrateCmd)
migrateCmd.Flags().StringVarP(&migrateArg.tableName, "table", "t", "", "Table name")
migrateCmd.Flags().StringVar(&migrateArg.snsARN, "sns", "", "SNS ARN")
migrateCmd.Flags().BoolVar(&migrateArg.repopulate, "caching", false, "TRUE to populate the cache")
migrateCmd.Flags().BoolVar(&migrateArg.indexTransformation, "index", false, "update DynamoDB indexes")
migrateCmd.Flags().BoolVar(&migrateArg.albumOwnerTransformation, "album-owner", false, "add Owner field to albums missing it")
}