Skip to content

Commit

Permalink
Move test data, improve multi-statement handling
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jan 12, 2019
1 parent b2f0b16 commit 676595c
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 166 deletions.
17 changes: 4 additions & 13 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,15 @@ func (r *Rove) Migrate(max int) error {
continue
}

arrQueries := strings.Split(cs.Changes(), ";")

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

// Loop through each change.
for _, q := range arrQueries {
if len(q) == 0 {
continue
}

// Execute the query.
err = tx.Exec(q)
if err != nil {
return fmt.Errorf("sql error on changeset %v:%v - %v", cs.Author, cs.ID, 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())
}

err = tx.Commit()
Expand Down
6 changes: 3 additions & 3 deletions pkg/adapter/jsonfile/jsonfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFileMigration(t *testing.T) {
assert.Nil(t, err)

// Set up rove.
r := rove.NewFileMigration(js, "testdata/success.sql")
r := rove.NewFileMigration(js, "../testdata/success.sql")
r.Verbose = true

// Run migration.
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestMigrationFailDuplicate(t *testing.T) {
assert.Nil(t, err)

// Set up rove.
r := rove.NewFileMigration(js, "testdata/fail-duplicate.sql")
r := rove.NewFileMigration(js, "../testdata/fail-duplicate.sql")
r.Verbose = true

err = r.Migrate(0)
Expand All @@ -111,7 +111,7 @@ func TestInclude(t *testing.T) {
assert.Nil(t, err)

// Set up rove.
r := rove.NewFileMigration(js, "testdata/parent.sql")
r := rove.NewFileMigration(js, "../testdata/parent.sql")
r.Verbose = true

// Run migration.
Expand Down
8 changes: 4 additions & 4 deletions rove_test.go → pkg/adapter/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rove_test
package mysql_test

import (
"testing"
Expand All @@ -17,7 +17,7 @@ func TestFileMigration(t *testing.T) {
m.DB = db

// Set up rove.
r := rove.NewFileMigration(m, "testdata/success.sql")
r := rove.NewFileMigration(m, "../testdata/success.sql")
r.Verbose = true

// Run migration.
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestMigrationFailDuplicate(t *testing.T) {
m.DB = db

// Set up rove.
r := rove.NewFileMigration(m, "testdata/fail-duplicate.sql")
r := rove.NewFileMigration(m, "../testdata/fail-duplicate.sql")
r.Verbose = true

err := r.Migrate(0)
Expand All @@ -104,7 +104,7 @@ func TestInclude(t *testing.T) {
m.DB = db

// Set up rove.
r := rove.NewFileMigration(m, "testdata/parent.sql")
r := rove.NewFileMigration(m, "../testdata/parent.sql")
r.Verbose = true

// Run migration.
Expand Down
2 changes: 1 addition & 1 deletion pkg/adapter/mysql/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func setEnv(unique string) {
os.Setenv(unique+"DB_USERNAME", "root")
os.Setenv(unique+"DB_PASSWORD", "")
os.Setenv(unique+"DB_DATABASE", TestDatabaseName+unique)
os.Setenv(unique+"DB_PARAMETER", "parseTime=true&allowNativePasswords=true")
os.Setenv(unique+"DB_PARAMETER", "parseTime=true&allowNativePasswords=true&multiStatements=true")
}

func unsetEnv(unique string) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ CREATE TABLE user_status (

PRIMARY KEY (id)
);
CREATE TABLE license (
id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT,

status VARCHAR(25) NOT NULL,

created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,

PRIMARY KEY (id)
);
--rollback DROP TABLE user_status;
--rollback DROP TABLE license;

--changeset josephspurrier:2
INSERT INTO `user_status` (`id`, `status`, `created_at`, `updated_at`, `deleted`) VALUES
(1, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0),
(1, 'active;live', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0),
(2, 'inactive', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0);
--rollback TRUNCATE TABLE user_status;

Expand Down
18 changes: 4 additions & 14 deletions reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rove
import (
"errors"
"fmt"
"strings"
)

// Reset will remove all migrations. If max is 0, all rollbacks are run.
Expand Down Expand Up @@ -38,24 +37,15 @@ func (r *Rove) Reset(max int) error {
return errors.New("changeset is missing: " + id)
}

arrQueries := strings.Split(cs.Rollbacks(), ";")

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

// Loop through each rollback.
for _, q := range arrQueries {
if len(q) == 0 {
continue
}

// Execute the query.
err = tx.Exec(q)
if err != nil {
return fmt.Errorf("sql error on rollback %v:%v - %v", cs.Author, cs.ID, 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())
}

err = tx.Commit()
Expand Down
20 changes: 0 additions & 20 deletions testdata/child1.sql

This file was deleted.

22 changes: 0 additions & 22 deletions testdata/child2.sql

This file was deleted.

43 changes: 0 additions & 43 deletions testdata/fail-duplicate.sql

This file was deleted.

2 changes: 0 additions & 2 deletions testdata/parent.sql

This file was deleted.

43 changes: 0 additions & 43 deletions testdata/success.sql

This file was deleted.

0 comments on commit 676595c

Please sign in to comment.