Skip to content

Commit

Permalink
Use strings.NewReader() instead of bytes.NewReader() when source is a…
Browse files Browse the repository at this point in the history
… string

    - Group imports
  • Loading branch information
dhui committed Nov 6, 2018
1 parent 1df8057 commit 06d3ebd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
7 changes: 4 additions & 3 deletions database/cockroachdb/cockroachdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cockroachdb
// error codes https://github.com/lib/pq/blob/master/error.go

import (
//"bytes"
"database/sql"
"fmt"
"io"
"strings"
"testing"
)

"bytes"
import (
dt "github.com/golang-migrate/migrate/v4/database/testing"
mt "github.com/golang-migrate/migrate/v4/testing"
"github.com/lib/pq"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestMultiStatement(t *testing.T) {
if err != nil {
t.Fatalf("%v", err)
}
if err := d.Run(bytes.NewReader([]byte("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);"))); err != nil {
if err := d.Run(strings.NewReader("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);")); err != nil {
t.Fatalf("expected err to be nil, got %v", err)
}

Expand Down
13 changes: 7 additions & 6 deletions database/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package postgres
// error codes https://github.com/lib/pq/blob/master/error.go

import (
"bytes"
"context"
"database/sql"
sqldriver "database/sql/driver"
Expand All @@ -12,7 +11,9 @@ import (
"strconv"
"strings"
"testing"
)

import (
dt "github.com/golang-migrate/migrate/v4/database/testing"
mt "github.com/golang-migrate/migrate/v4/testing"
)
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestMultiStatement(t *testing.T) {
t.Fatalf("%v", err)
}
defer d.Close()
if err := d.Run(bytes.NewReader([]byte("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);"))); err != nil {
if err := d.Run(strings.NewReader("CREATE TABLE foo (foo text); CREATE TABLE bar (bar text);")); err != nil {
t.Fatalf("expected err to be nil, got %v", err)
}

Expand Down Expand Up @@ -100,7 +101,7 @@ func TestErrorParsing(t *testing.T) {

wantErr := `migration failed: syntax error at or near "TABLEE" (column 37) in line 1: CREATE TABLE foo ` +
`(foo text); CREATE TABLEE bar (bar text); (details: pq: syntax error at or near "TABLEE")`
if err := d.Run(bytes.NewReader([]byte("CREATE TABLE foo (foo text); CREATE TABLEE bar (bar text);"))); err == nil {
if err := d.Run(strings.NewReader("CREATE TABLE foo (foo text); CREATE TABLEE bar (bar text);")); err == nil {
t.Fatal("expected err but got nil")
} else if err.Error() != wantErr {
t.Fatalf("expected '%s' but got '%s'", wantErr, err.Error())
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestWithSchema(t *testing.T) {
defer d.Close()

// create foobar schema
if err := d.Run(bytes.NewReader([]byte("CREATE SCHEMA foobar AUTHORIZATION postgres"))); err != nil {
if err := d.Run(strings.NewReader("CREATE SCHEMA foobar AUTHORIZATION postgres")); err != nil {
t.Fatal(err)
}
if err := d.SetVersion(1, false); err != nil {
Expand Down Expand Up @@ -190,10 +191,10 @@ func TestParallelSchema(t *testing.T) {
defer d.Close()

// create foo and bar schemas
if err := d.Run(bytes.NewReader([]byte("CREATE SCHEMA foo AUTHORIZATION postgres"))); err != nil {
if err := d.Run(strings.NewReader("CREATE SCHEMA foo AUTHORIZATION postgres")); err != nil {
t.Fatal(err)
}
if err := d.Run(bytes.NewReader([]byte("CREATE SCHEMA bar AUTHORIZATION postgres"))); err != nil {
if err := d.Run(strings.NewReader("CREATE SCHEMA bar AUTHORIZATION postgres")); err != nil {
t.Fatal(err)
}

Expand Down
5 changes: 4 additions & 1 deletion migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"testing"
)

import (
dStub "github.com/golang-migrate/migrate/v4/database/stub"
"github.com/golang-migrate/migrate/v4/source"
sStub "github.com/golang-migrate/migrate/v4/source/stub"
Expand Down Expand Up @@ -1375,7 +1378,7 @@ func M(version uint, targetVersion ...int) *Migration {
// mr is a convenience func to create a new *Migration from the raw database query
func mr(value string) *Migration {
return &Migration{
Body: ioutil.NopCloser(bytes.NewReader([]byte(value))),
Body: ioutil.NopCloser(strings.NewReader(value)),
}
}

Expand Down
7 changes: 4 additions & 3 deletions source/github/github.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package github

import (
"bytes"
"context"
"fmt"
"io"
Expand All @@ -10,7 +9,9 @@ import (
"os"
"path"
"strings"
)

import (
"github.com/golang-migrate/migrate/v4/source"
"github.com/google/go-github/github"
)
Expand Down Expand Up @@ -158,7 +159,7 @@ func (g *Github) ReadUp(version uint) (r io.ReadCloser, identifier string, err e
if err != nil {
return nil, "", err
}
return ioutil.NopCloser(bytes.NewReader([]byte(r))), m.Identifier, nil
return ioutil.NopCloser(strings.NewReader(r)), m.Identifier, nil
}
}
return nil, "", &os.PathError{fmt.Sprintf("read version %v", version), g.path, os.ErrNotExist}
Expand All @@ -175,7 +176,7 @@ func (g *Github) ReadDown(version uint) (r io.ReadCloser, identifier string, err
if err != nil {
return nil, "", err
}
return ioutil.NopCloser(bytes.NewReader([]byte(r))), m.Identifier, nil
return ioutil.NopCloser(strings.NewReader(r)), m.Identifier, nil
}
}
return nil, "", &os.PathError{fmt.Sprintf("read version %v", version), g.path, os.ErrNotExist}
Expand Down

0 comments on commit 06d3ebd

Please sign in to comment.