Skip to content

Commit

Permalink
Add description field
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jan 12, 2019
1 parent 676595c commit b9fe40e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 52 deletions.
2 changes: 2 additions & 0 deletions cmd/rove/testdata/success.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
--changeset josephspurrier:1
--description Create the user status table.
--description Ensure to set the auto value on zero.
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
CREATE TABLE user_status (
id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT,
Expand Down
10 changes: 5 additions & 5 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ func (r *Rove) Migrate(max int) error {

tx, err := r.db.BeginTx()
if err != nil {
return fmt.Errorf("sql error begin transaction - %v", err.Error())
return fmt.Errorf("error on begin transaction - %v", err.Error())
}

// Execute the query.
err = tx.Exec(cs.Changes())
if err != nil {
return fmt.Errorf("sql error on changeset %v:%v - %v", cs.Author, cs.ID, err.Error())
return fmt.Errorf("error on changeset %v:%v - %v", cs.Author, cs.ID, err.Error())
}

err = tx.Commit()
if err != nil {
errr := tx.Rollback()
if errr != nil {
return fmt.Errorf("sql error on commit rollback %v:%v - %v", cs.Author, cs.ID, errr.Error())
return fmt.Errorf("error on commit rollback %v:%v - %v", cs.Author, cs.ID, errr.Error())
}
return fmt.Errorf("sql error on commit %v:%v - %v", cs.Author, cs.ID, err.Error())
return fmt.Errorf("error on commit %v:%v - %v", cs.Author, cs.ID, err.Error())
}

// Count the number of rows.
Expand All @@ -85,7 +85,7 @@ func (r *Rove) Migrate(max int) error {

// Insert the record.
err = r.db.Insert(cs.ID, cs.Author, cs.Filename, count+1, newChecksum,
cs.Description, cs.Version)
cs.Descriptions(), cs.Version)
if err != nil {
return err
}
Expand Down
23 changes: 21 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import (
"github.com/josephspurrier/rove/pkg/changeset"
)

const (
elementChangeset = "--changeset "
elementRollback = "--rollback "
elementInclude = "--include "
elementDescription = "--description "
elementMemory = "memory"
)

var (
// ErrInvalidFormat is when a changeset is not found.
ErrInvalidFormat = errors.New("invalid changeset format")
)

// parseFileToArray will parse a file into changesets.
func parseFileToArray(filename string) ([]changeset.Record, error) {
f, err := os.Open(filename)
Expand All @@ -24,7 +37,7 @@ func parseFileToArray(filename string) ([]changeset.Record, error) {
return parseToArray(f, filename)
}

// parseToArray will split the SQL migration into an ordered array.
// parseToArray will split the migration into an ordered array.
func parseToArray(r io.Reader, filename string) ([]changeset.Record, error) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
Expand Down Expand Up @@ -59,7 +72,7 @@ func parseToArray(r io.Reader, filename string) ([]changeset.Record, error) {
// Create a new changeset.
cs := new(changeset.Record)
cs.ParseHeader(strings.TrimPrefix(line, elementChangeset))
cs.SetFileInfo(path.Base(filename), "sql", appVersion)
cs.SetFileInfo(path.Base(filename), appVersion)
arr = append(arr, *cs)
continue
}
Expand All @@ -75,6 +88,12 @@ func parseToArray(r io.Reader, filename string) ([]changeset.Record, error) {
continue
}

// Determine if the line is a description.
if strings.HasPrefix(line, elementDescription) {
arr[len(arr)-1].AddDescription(strings.TrimPrefix(line, elementDescription))
continue
}

// Determine if the line is comment, ignore it.
if strings.HasPrefix(line, "--") {
continue
Expand Down
47 changes: 32 additions & 15 deletions pkg/adapter/jsonfile/jsonfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"encoding/json"
"io/ioutil"
"os"
"time"

"github.com/josephspurrier/rove"
"github.com/josephspurrier/rove/pkg/adapter/mysql"
"github.com/josephspurrier/rove/pkg/changeset"
)

// Info is a JSON changelog.
Expand All @@ -17,6 +17,21 @@ type Info struct {
db *mysql.MySQL
}

// Changeset contains a single record change.
type Changeset struct {
ID string `json:"id"`
Author string `json:"author"`
Filename string `json:"filename"`
DateExecuted string `json:"dateexecuted"`
OrderExecuted int `json:"orderexecuted"`
Checksum string `json:"checksum"`
Description string `json:"description"`
Tag string `json:"tag"`
Version string `json:"version"`
}

//id,author,filename,dateexecuted,orderexecuted,md5sum,description,version

// New sets the filename and returns an object that satisfies the rove.Changelog
// interface.
func New(filename string, db *mysql.MySQL) (m *Info, err error) {
Expand Down Expand Up @@ -46,7 +61,7 @@ func (m *Info) ChangesetApplied(id, author, filename string) (checksum string, e
}

// Convert to JSON.
data := make([]changeset.Record, 0)
data := make([]Changeset, 0)
err = json.Unmarshal(b, &data)
if err != nil {
return "", err
Expand All @@ -56,7 +71,7 @@ func (m *Info) ChangesetApplied(id, author, filename string) (checksum string, e
for _, cs := range data {
// If found, return the checksum.
if cs.ID == id && cs.Author == author && cs.Filename == filename {
return cs.MD5, nil
return cs.Checksum, nil
}
}

Expand All @@ -80,7 +95,7 @@ func (m *Info) Count() (count int, err error) {
}

// Convert to JSON.
data := make([]changeset.Record, 0)
data := make([]Changeset, 0)
err = json.Unmarshal(b, &data)
if err != nil {
return 0, err
Expand All @@ -98,19 +113,21 @@ func (m *Info) Insert(id, author, filename string, count int, checksum, descript
}

// Convert to JSON.
data := make([]changeset.Record, 0)
data := make([]Changeset, 0)
err = json.Unmarshal(b, &data)
if err != nil {
return err
}

data = append(data, changeset.Record{
Author: author,
Description: description,
Filename: filename,
ID: id,
MD5: checksum,
Version: version,
data = append(data, Changeset{
Author: author,
Description: description,
Filename: filename,
ID: id,
Checksum: checksum,
Version: version,
OrderExecuted: len(data) + 1,
DateExecuted: time.Now().Format("2006-01-02 13:04:05"),
})

b, err = json.Marshal(data)
Expand All @@ -131,7 +148,7 @@ func (m *Info) Changesets(reverse bool) ([]rove.Changeset, error) {
}

// Convert to JSON.
results := make([]changeset.Record, 0)
results := make([]Changeset, 0)
err = json.Unmarshal(b, &results)
if err != nil {
return nil, err
Expand Down Expand Up @@ -169,13 +186,13 @@ func (m *Info) Delete(id, author, filename string) error {
}

// Convert to JSON.
data := make([]changeset.Record, 0)
data := make([]Changeset, 0)
err = json.Unmarshal(b, &data)
if err != nil {
return err
}

newData := make([]changeset.Record, 0)
newData := make([]Changeset, 0)
for _, cs := range data {
if cs.ID == id && cs.Author == author && cs.Filename == filename {
// skip
Expand Down
33 changes: 22 additions & 11 deletions pkg/changeset/changeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ var (

// Record is a changeset.
type Record struct {
ID string
Author string
Filename string
MD5 string
Description string
Version string
OrderExecuted int //FIXME: I don't think these should be here.
ID string
Author string
Filename string
MD5 string
Version string

change []string
rollback []string
change []string
rollback []string
description []string
}

// ParseHeader will parse the header information.
Expand All @@ -39,9 +38,8 @@ func (cs *Record) ParseHeader(line string) error {
}

// SetFileInfo will set the file information.
func (cs *Record) SetFileInfo(filename string, description string, version string) {
func (cs *Record) SetFileInfo(filename string, version string) {
cs.Filename = filename
cs.Description = description
cs.Version = version
}

Expand All @@ -53,6 +51,14 @@ func (cs *Record) AddRollback(line string) {
cs.rollback = append(cs.rollback, line)
}

// AddDescription will add a description command.
func (cs *Record) AddDescription(line string) {
if len(cs.description) == 0 {
cs.description = make([]string, 0)
}
cs.description = append(cs.description, line)
}

// AddChange will add a change command.
func (cs *Record) AddChange(line string) {
if len(cs.change) == 0 {
Expand All @@ -71,6 +77,11 @@ func (cs *Record) Rollbacks() string {
return strings.Join(cs.rollback, "\n")
}

// Descriptions will return the descriptions.
func (cs *Record) Descriptions() string {
return strings.Join(cs.description, "\n")
}

// Checksum returns an MD5 checksum for the changeset.
func (cs *Record) Checksum() string {
return md5sum(cs.Changes())
Expand Down
2 changes: 1 addition & 1 deletion pkg/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

// Connection holds the details for the MySQL connection.
// Connection holds the details for a database connection.
type Connection struct {
Username string `json:"Username" env:"DB_USERNAME"`
Password string `json:"Password" env:"DB_PASSWORD"`
Expand Down
8 changes: 4 additions & 4 deletions reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ func (r *Rove) Reset(max int) error {

tx, err := r.db.BeginTx()
if err != nil {
return fmt.Errorf("sql error begin transaction - %v", err.Error())
return fmt.Errorf("error on begin transaction - %v", err.Error())
}

// Execute the query.
err = tx.Exec(cs.Rollbacks())
if err != nil {
return fmt.Errorf("sql error on rollback %v:%v - %v", cs.Author, cs.ID, err.Error())
return fmt.Errorf("error on rollback %v:%v - %v", cs.Author, cs.ID, err.Error())
}

err = tx.Commit()
if err != nil {
errr := tx.Rollback()
if errr != nil {
return fmt.Errorf("sql error on commit rollback %v:%v - %v", cs.Author, cs.ID, errr.Error())
return fmt.Errorf("error on commit rollback %v:%v - %v", cs.Author, cs.ID, errr.Error())
}
return fmt.Errorf("sql error on commit %v:%v - %v", cs.Author, cs.ID, err.Error())
return fmt.Errorf("error on commit %v:%v - %v", cs.Author, cs.ID, err.Error())
}

// Delete the record.
Expand Down
15 changes: 1 addition & 14 deletions rove.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package rove

import (
"errors"
)

const (
appVersion = "1.0"
elementChangeset = "--changeset "
elementRollback = "--rollback "
elementInclude = "--include "
elementMemory = "memory"
)

var (
// ErrInvalidFormat is when a changeset is not found.
ErrInvalidFormat = errors.New("invalid changeset format")
appVersion = "1.0"
)

// Rove contains the database migration information.
Expand Down

0 comments on commit b9fe40e

Please sign in to comment.