Skip to content

Commit

Permalink
schema-tool: do not error when schema is already up-to-date (cadence-…
Browse files Browse the repository at this point in the history
  • Loading branch information
venkat1109 authored Jun 24, 2019
1 parent 6ffb281 commit 4d0e3e2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
42 changes: 35 additions & 7 deletions tools/common/schema/updatetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (task *UpdateTask) Run() error {

func (task *UpdateTask) executeUpdates(currVer string, updates []changeSet) error {

if len(updates) == 0 {
log.Printf("found zero updates from current version %v", currVer)
return nil
}

for _, cs := range updates {

err := task.execCQLStmts(cs.version, cs.cqlStmts)
Expand Down Expand Up @@ -168,9 +173,6 @@ func (task *UpdateTask) buildChangeSet(currVer string) ([]changeSet, error) {
if err != nil {
return nil, fmt.Errorf("error listing schema dir:%v", err.Error())
}
if len(verDirs) == 0 {
return nil, fmt.Errorf("no schema dirs in version range [%v-%v]", currVer, config.TargetVersion)
}

var result []changeSet

Expand Down Expand Up @@ -283,21 +285,31 @@ func readManifest(dirPath string) (*manifest, error) {
}

// readSchemaDir returns a sorted list of subdir names that hold
// the schema changes for versions in the range [startVer - endVer]
// the schema changes for versions in the range startVer < ver <= endVer
// when endVer is empty this method returns all subdir names that are greater than startVer
// this method has an assumption that the subdirs containing the
// schema changes will be of the form vx.x, where x.x is the version
// returns error when
// - startVer <= endVer
// - endVer is empty and no subdirs have version >= startVer
// - endVer is non-empty and subdir with version == endVer is not found
func readSchemaDir(dir string, startVer string, endVer string) ([]string, error) {

subdirs, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}

hasEndVer := len(endVer) > 0

if hasEndVer && cmpVersion(startVer, endVer) >= 0 {
return nil, fmt.Errorf("startVer (%v) must be less than endVer (%v)", startVer, endVer)
}

var endFound bool
var highestVer string
var result []string

hasEndVer := len(endVer) > 0

for _, dir := range subdirs {

if !dir.IsDir() {
Expand All @@ -312,6 +324,12 @@ func readSchemaDir(dir string, startVer string, endVer string) ([]string, error)

ver := dirToVersion(dirname)

if len(highestVer) == 0 {
highestVer = ver
} else if cmpVersion(ver, highestVer) > 0 {
highestVer = ver
}

highcmp := 0
lowcmp := cmpVersion(ver, startVer)
if hasEndVer {
Expand All @@ -326,10 +344,20 @@ func readSchemaDir(dir string, startVer string, endVer string) ([]string, error)
result = append(result, dirname)
}

if !endFound {
// when endVer is specified, atleast one result MUST be found since startVer < endVer
if hasEndVer && !endFound {
return nil, fmt.Errorf("version dir not found for target version %v", endVer)
}

// when endVer is empty and no result is found, then the highest version
// found must be equal to startVer, else return error
if !hasEndVer && len(result) == 0 {
if len(highestVer) == 0 || cmpVersion(startVer, highestVer) != 0 {
return nil, fmt.Errorf("no subdirs found with version >= %v", startVer)
}
return result, nil
}

sort.Sort(byVersion(result))

return result, nil
Expand Down
32 changes: 25 additions & 7 deletions tools/common/schema/updatetask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ func (s *UpdateTaskTestSuite) SetupSuite() {

func (s *UpdateTaskTestSuite) TestReadSchemaDir() {

emptyDir, err := ioutil.TempDir("", "update_schema_test_empty")
s.NoError(err)
defer os.RemoveAll(emptyDir)

tmpDir, err := ioutil.TempDir("", "update_schema_test")
s.Nil(err)
s.NoError(err)
defer os.RemoveAll(tmpDir)

subDirs := []string{"v0.5", "v1.5", "v2.5", "v3.5", "v10.2", "abc", "2.0", "3.0"}
Expand All @@ -54,17 +58,31 @@ func (s *UpdateTaskTestSuite) TestReadSchemaDir() {
}

_, err = readSchemaDir(tmpDir, "11.0", "11.2")
s.NotNil(err)
s.Error(err)
_, err = readSchemaDir(tmpDir, "0.5", "10.3")
s.NotNil(err)
s.Error(err)
_, err = readSchemaDir(tmpDir, "1.5", "1.5")
s.Error(err)
_, err = readSchemaDir(tmpDir, "1.5", "0.5")
s.Error(err)
_, err = readSchemaDir(tmpDir, "10.3", "")
s.Error(err)
_, err = readSchemaDir(emptyDir, "11.0", "")
s.Error(err)
_, err = readSchemaDir(emptyDir, "10.1", "")
s.Error(err)

ans, err := readSchemaDir(tmpDir, "0.4", "10.2")
s.Nil(err)
s.NoError(err)
s.Equal([]string{"v0.5", "v1.5", "v2.5", "v3.5", "v10.2"}, ans)

ans, err = readSchemaDir(tmpDir, "0.5", "3.5")
s.Nil(err)
s.NoError(err)
s.Equal([]string{"v1.5", "v2.5", "v3.5"}, ans)

ans, err = readSchemaDir(tmpDir, "10.2", "")
s.NoError(err)
s.Equal(0, len(ans))
}

func (s *UpdateTaskTestSuite) TestReadManifest() {
Expand Down Expand Up @@ -132,10 +150,10 @@ func (s *UpdateTaskTestSuite) runReadManifestTest(dir, input, currVer, minVer, d

m, err := readManifest(dir)
if isErr {
s.NotNil(err)
s.Error(err)
return
}
s.Nil(err)
s.NoError(err)
s.Equal(currVer, m.CurrVersion)
s.Equal(minVer, m.MinCompatibleVersion)
s.Equal(desc, m.Description)
Expand Down

0 comments on commit 4d0e3e2

Please sign in to comment.