Skip to content

Commit

Permalink
Merge pull request #9 from dannov91/changelog-generator
Browse files Browse the repository at this point in the history
Implement changelog.xml file generator
  • Loading branch information
paganotoni authored Apr 28, 2021
2 parents d15d30c + 05fa7a3 commit be28b2f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
32 changes: 26 additions & 6 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
// MigrationTemplate for the migration generator.
//go:embed templates/migration.xml.tmpl
migrationTemplate string
//go:embed templates/changelog.xml.tmpl
changelogTemplate string
)

var (
Expand Down Expand Up @@ -86,11 +88,6 @@ func (g Generator) Generate(ctx context.Context, root string, args []string) err
log.Infof("migration generated in %v", path)
err = g.addToChangelog(root, path)

if os.IsNotExist(err) {
log.Infof("auto-add to changelog file failed: %v", err.Error())
return nil
}

if err == ErrInvalidChangelogFormat {
log.Infof("auto-add to changelog file failed: %v", err.Error())
return nil
Expand All @@ -100,13 +97,24 @@ func (g Generator) Generate(ctx context.Context, root string, args []string) err
return err
}

log.Infof("migration added to the changelog")
log.Infof("migration added to the changelog.xml file")
return nil
}

func (g Generator) addToChangelog(root, path string) error {
changelog := filepath.Join(root, "migrations", "changelog.xml")
original, err := ioutil.ReadFile(changelog)

if os.IsNotExist(err) {
err = g.generateChangelogFile()
if err != nil {
log.Infof("failed generating changelog.xml file: %v", err.Error())
}

log.Infof("changelog.xml was not found, file was generated automatically")
original, err = ioutil.ReadFile(changelog)
}

if err != nil {
return err
}
Expand Down Expand Up @@ -187,6 +195,18 @@ func (g Generator) generateFile(args []string) (string, error) {
return path, ioutil.WriteFile(path, tpl.Bytes(), 0655)
}

func (g Generator) generateChangelogFile() error {
filename := "changelog.xml"
path := filepath.Join("migrations", filename)

err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return err
}

return ioutil.WriteFile(path, []byte(changelogTemplate), 0655)
}

// composeFilename from the passed arg and timestamp, if the passed path is
// a dot (.) or a folder "/" then it will return ErrInvalidName.
func (g Generator) composeFilename(passed, timestamp string) (string, error) {
Expand Down
82 changes: 69 additions & 13 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func TestGeneratorRun(t *testing.T) {
t.Errorf("error should be nil, got %v", err)
}

// Check generated migration file
path := filepath.Join(root, "12345-aaa.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
t.Error("should have created the file in the root")
t.Error("should have created migration file in the root")
}

d, err := ioutil.ReadFile(path)
Expand All @@ -45,6 +46,22 @@ func TestGeneratorRun(t *testing.T) {
if content := string(d); !strings.Contains(content, "12345-aaa") {
t.Errorf("file content %v should contain %v", content, "12345-aaa")
}

// Check that the changelog file exists and verify content
path = filepath.Join(root, "migrations", "changelog.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
t.Error("should have created changelog file in the root")
}

d, err = ioutil.ReadFile(path)
if err != nil {
t.Errorf("error should be nil, got %v", err)
}

if content := string(d); !strings.Contains(content, `<include file="12345-aaa.xml" />`) {
t.Errorf("file content %v should contain %v", content, `<include file="12345-aaa.xml" />`)
}
})

t.Run("folder", func(t *testing.T) {
Expand All @@ -60,6 +77,7 @@ func TestGeneratorRun(t *testing.T) {
t.Errorf("error should be nil, got %v", err)
}

// Check generated migration file
path := filepath.Join(root, "folder", "is", "here", "12345-aaa.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
Expand All @@ -74,6 +92,22 @@ func TestGeneratorRun(t *testing.T) {
if content := string(d); !strings.Contains(content, "12345-aaa") {
t.Errorf("file content %v should contain %v", content, "12345-aaa")
}

// Check that the changelog file exists and verify content
path = filepath.Join(root, "migrations", "changelog.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
t.Error("should have created changelog file in the root")
}

d, err = ioutil.ReadFile(path)
if err != nil {
t.Errorf("error should be nil, got %v", err)
}

if content := string(d); !strings.Contains(content, `<include file="folder/is/here/12345-aaa.xml" />`) {
t.Errorf("file content %v should contain %v", content, `<include file="folder/is/here/12345-aaa.xml" />`)
}
})

t.Run("folder exists", func(t *testing.T) {
Expand All @@ -94,6 +128,7 @@ func TestGeneratorRun(t *testing.T) {
t.Errorf("error should be nil, got %v", err)
}

// Check generated migration file
path := filepath.Join(root, "folder", "is", "here", "12345-aaa.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
Expand All @@ -108,6 +143,22 @@ func TestGeneratorRun(t *testing.T) {
if content := string(d); !strings.Contains(content, "12345-aaa") {
t.Errorf("file content %v should contain %v", content, "12345-aaa")
}

// Check that the changelog file exists and verify content
path = filepath.Join(root, "migrations", "changelog.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
t.Error("should have created changelog file in the root")
}

d, err = ioutil.ReadFile(path)
if err != nil {
t.Errorf("error should be nil, got %v", err)
}

if content := string(d); !strings.Contains(content, `<include file="folder/is/here/12345-aaa.xml" />`) {
t.Errorf("file content %v should contain %v", content, `<include file="folder/is/here/12345-aaa.xml" />`)
}
})

t.Run("different base", func(t *testing.T) {
Expand All @@ -132,6 +183,7 @@ func TestGeneratorRun(t *testing.T) {
t.Errorf("error should be nil, got %v", err)
}

// Check generated migration file
path := filepath.Join(root, "migrations", "12345-aaa.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
Expand All @@ -146,6 +198,22 @@ func TestGeneratorRun(t *testing.T) {
if content := string(d); !strings.Contains(content, "12345-aaa") {
t.Errorf("file content %v should contain %v", content, "12345-aaa")
}

// Check that the changelog file exists and verify content
path = filepath.Join(root, "migrations", "changelog.xml")
_, err = os.Stat(path)
if os.IsNotExist(err) {
t.Error("should have created changelog file in the root")
}

d, err = ioutil.ReadFile(path)
if err != nil {
t.Errorf("error should be nil, got %v", err)
}

if content := string(d); !strings.Contains(content, `<include file="migrations/12345-aaa.xml" />`) {
t.Errorf("file content %v should contain %v", content, `<include file="migrations/12345-aaa.xml" />`)
}
})
}

Expand Down Expand Up @@ -277,18 +345,6 @@ func TestAddToChangelogInvalidFormats(t *testing.T) {
}
}

func TestAddToChangelogFileNotExists(t *testing.T) {
g := Generator{}
base := os.TempDir()
os.Chdir(base)

os.Remove(filepath.Join(base, "migrations", "changelog.xml"))
err := g.addToChangelog(base, "some.xml")
if !os.IsNotExist(err) {
t.Errorf("has error, but from unexpected type: %v", err.Error())
}
}

func createChangelog(base string, changeLogContent string) (string, error) {
err := os.MkdirAll(filepath.Join(base, "migrations"), 0777)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions templates/changelog.xml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
</databaseChangeLog>

0 comments on commit be28b2f

Please sign in to comment.