Skip to content

Commit

Permalink
Support user-set schema version
Browse files Browse the repository at this point in the history
  • Loading branch information
hdpe committed Feb 7, 2021
1 parent 5f3090c commit c52df5b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
11 changes: 9 additions & 2 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ import (
)

var approve bool
var version string

func init() {
migrateCmd.Flags().BoolVarP(&approve, "approve", "a", false, "approve this migration without prompting")
migrateCmd.Flags().BoolVarP(&approve, "approve", "a", false,
"approve this migration without prompting")

migrateCmd.Flags().StringVarP(&version, "version", "v",
(&util.DefaultClock{}).Now().UTC().Format("20060102150405"),
"index version suffix for this migration - defaults to current timestamp")

rootCmd.AddCommand(migrateCmd)
}

Expand All @@ -33,7 +40,7 @@ var migrateCmd = &cobra.Command{

ctx := newContext(envName)

planner := plan.NewPlanner(ctx.Es, ctx.Conf, ctx.Changelog, ctx.Schema, ctx.Proc, &util.DefaultClock{})
planner := plan.NewPlanner(ctx.Es, ctx.Conf, ctx.Changelog, ctx.Schema, ctx.Proc, version)

getLock(ctx, envName)
defer releaseLock(ctx, envName)
Expand Down
5 changes: 1 addition & 4 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import (
"github.com/hdpe.me/esup/es"
"github.com/hdpe.me/esup/resource"
"github.com/hdpe.me/esup/schema"
"github.com/hdpe.me/esup/util"
"reflect"
)

func NewPlanner(es *es.Client, config config.Config, changelog *resource.Changelog, s schema.Schema,
proc *resource.Preprocessor, clock util.Clock) *Planner {

version := clock.Now().UTC().Format("20060102150405")
proc *resource.Preprocessor, version string) *Planner {

return &Planner{
es: es,
Expand Down
2 changes: 1 addition & 1 deletion plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestPlanner_Plan(t *testing.T) {
})
}

p := NewPlanner(ctx.Es, ctx.Conf, ctx.Changelog, s, ctx.Proc, tc.Clock())
p := NewPlanner(ctx.Es, ctx.Conf, ctx.Changelog, s, ctx.Proc, tc.Version())

plan, err := p.Plan()

Expand Down
8 changes: 3 additions & 5 deletions plan/plan_test_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var documentTestCases = []PlanTestCase{
Content: `{"k":"v"}`,
Meta: schema.DocumentMeta{Ignored: false},
},
clock: testutil.NewStaticClock("2001-02-03T04:05:06Z"),
expected: []testutil.Matcher{
newIndexDocumentMatcher().
withIndex("env-is").
Expand Down Expand Up @@ -51,7 +50,6 @@ var documentTestCases = []PlanTestCase{
Content: "{}",
Meta: schema.DocumentMeta{Ignored: true},
},
clock: testutil.NewStaticClock("2001-02-03T04:05:06Z"),
expected: []testutil.Matcher{
newWriteChangelogEntryMatcher(),
},
Expand All @@ -63,7 +61,7 @@ type documentTestCase struct {
envName string
indexSet IndexSetSpec
document DocumentSpec
clock util.Clock
version string
setup func(Setup)
expected []testutil.Matcher

Expand All @@ -80,8 +78,8 @@ func (r *documentTestCase) EnvName() string {
return r.envName
}

func (r *documentTestCase) Clock() util.Clock {
return r.clock
func (r *documentTestCase) Version() string {
return r.version
}

func (r *documentTestCase) Schema() (schema.Schema, error) {
Expand Down
13 changes: 5 additions & 8 deletions plan/plan_test_index_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package plan
import (
"github.com/hdpe.me/esup/schema"
"github.com/hdpe.me/esup/testutil"
"github.com/hdpe.me/esup/util"
"io/ioutil"
"os"
)
Expand All @@ -12,7 +11,7 @@ var indexSetTestCases = []PlanTestCase{
&indexSetTestCase{
desc: "create fresh index set",
envName: "env",
clock: testutil.NewStaticClock("2001-02-03T04:05:06Z"),
version: "20010203040506",
indexSet: IndexSetSpec{
Name: "x",
Content: "{}",
Expand All @@ -37,7 +36,7 @@ var indexSetTestCases = []PlanTestCase{
&indexSetTestCase{
desc: "update existing index set",
envName: "env",
clock: testutil.NewStaticClock("2001-02-03T04:05:06Z"),
version: "20010203040506",
setup: func(setup Setup) {
setup.Apply(
&createIndex{
Expand Down Expand Up @@ -80,7 +79,6 @@ var indexSetTestCases = []PlanTestCase{
&indexSetTestCase{
desc: "create static index set",
envName: "env",
clock: testutil.NewStaticClock("2001-02-03T04:05:06Z"),
setup: func(setup Setup) {
setup.Apply(
&createIndex{
Expand Down Expand Up @@ -116,7 +114,6 @@ var indexSetTestCases = []PlanTestCase{
&indexSetTestCase{
desc: "update static index set",
envName: "env",
clock: testutil.NewStaticClock("2001-02-03T04:05:06Z"),
setup: func(setup Setup) {
setup.Apply(
&createIndex{
Expand Down Expand Up @@ -157,7 +154,7 @@ var indexSetTestCases = []PlanTestCase{
type indexSetTestCase struct {
desc string
envName string
clock util.Clock
version string
setup func(Setup)
indexSet IndexSetSpec
expected []testutil.Matcher
Expand All @@ -174,8 +171,8 @@ func (r *indexSetTestCase) EnvName() string {
return r.envName
}

func (r *indexSetTestCase) Clock() util.Clock {
return r.clock
func (r *indexSetTestCase) Version() string {
return r.version
}

func (r *indexSetTestCase) Schema() (schema.Schema, error) {
Expand Down
3 changes: 1 addition & 2 deletions plan/plan_test_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"github.com/hdpe.me/esup/resource"
"github.com/hdpe.me/esup/schema"
"github.com/hdpe.me/esup/testutil"
"github.com/hdpe.me/esup/util"
)

type PlanTestCase interface {
Desc() string
EnvName() string
Clock() util.Clock
Version() string
Schema() (schema.Schema, error)
Setup() func(setup Setup)
Expected() []testutil.Matcher
Expand Down

0 comments on commit c52df5b

Please sign in to comment.