Skip to content

Commit

Permalink
implement and test Extensioner
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Oct 7, 2019
1 parent 1ffb7a1 commit 2e42fab
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 33 deletions.
6 changes: 6 additions & 0 deletions chiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (a *Archiver) archive(ctx context.Context, rows Rows, table, bucket string)
formatter = a.format(w, columns)
g, gctx = errgroup.WithContext(ctx)
)
if extensioner, ok := formatter.(Extensioner); ok && a.extension == "" {
a.extension = extensioner.Extension()
}
g.Go(func() error {
return a.download(gctx, rows, columns, formatter, w)
})
Expand Down Expand Up @@ -218,6 +221,9 @@ func (a *Archiver) upload(ctx context.Context, r io.ReadCloser, table string, bu
}
}()

if table == "" {
table = "table"
}
if a.key == "" {
if a.extension != "" {
a.key = fmt.Sprintf("%s.%s", table, a.extension)
Expand Down
17 changes: 16 additions & 1 deletion chiv_formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"regexp"
"strconv"

"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

// Column reports its name, database type name and scan type.
Expand Down Expand Up @@ -90,6 +90,11 @@ func (f *csvFormatter) Close() error {
return nil
}

// Extension returns the default CSV formatter extension.
func (f *csvFormatter) Extension() string {
return "csv"
}

type yamlFormatter struct {
w io.Writer
columns []Column
Expand Down Expand Up @@ -132,6 +137,11 @@ func (f *yamlFormatter) Close() error {
return nil
}

// Extension returns the default YAML formatter extension.
func (f *yamlFormatter) Extension() string {
return "yaml"
}

const (
openBracket = byte('[')
closeBracket = byte(']')
Expand Down Expand Up @@ -196,6 +206,11 @@ func (f *jsonFormatter) Close() error {
return nil
}

// Extension returns the default JSON formatter extension.
func (f *jsonFormatter) Extension() string {
return "json"
}

func (f *jsonFormatter) writeByte(b byte) error {
_, err := f.w.Write([]byte{b})
if err != nil {
Expand Down
30 changes: 11 additions & 19 deletions chiv_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"

"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -56,7 +55,7 @@ func TestArchiver_Archive(t *testing.T) {
{
expected: "./testdata/postgres/postgres.csv",
table: "postgres_table",
key: "postgres_table",
key: "postgres_table.csv",
options: []chiv.Option{},
},
},
Expand All @@ -69,13 +68,13 @@ func TestArchiver_Archive(t *testing.T) {
teardown: "./testdata/postgres/postgres_teardown.sql",
bucket: "postgres_bucket",
options: []chiv.Option{
chiv.WithKey("postgres_table.csv"),
chiv.WithKey("archive.csv"),
},
calls: []call{
{
expected: "./testdata/postgres/postgres.csv",
table: "postgres_table",
key: "postgres_table.csv",
key: "archive.csv",
options: []chiv.Option{},
},
},
Expand All @@ -94,7 +93,7 @@ func TestArchiver_Archive(t *testing.T) {
{
expected: "./testdata/postgres/postgres_with_null.csv",
table: "postgres_table",
key: "postgres_table",
key: "postgres_table.csv",
options: []chiv.Option{},
},
},
Expand All @@ -108,7 +107,6 @@ func TestArchiver_Archive(t *testing.T) {
bucket: "postgres_bucket",
options: []chiv.Option{
chiv.WithFormat(chiv.JSON),
chiv.WithKey("postgres_table.json"),
},
calls: []call{
{
Expand All @@ -128,7 +126,6 @@ func TestArchiver_Archive(t *testing.T) {
bucket: "postgres_bucket",
options: []chiv.Option{
chiv.WithFormat(chiv.YAML),
chiv.WithKey("postgres_table.yaml"),
},
calls: []call{
{
Expand Down Expand Up @@ -156,16 +153,13 @@ func TestArchiver_Archive(t *testing.T) {
key: "postgres_table.json",
options: []chiv.Option{
chiv.WithFormat(chiv.JSON),
chiv.WithKey("postgres_table.json"),
},
},
{
expected: "./testdata/postgres/postgres.yaml",
table: "postgres_table",
key: "postgres_table.yaml",
options: []chiv.Option{
chiv.WithKey("postgres_table.yaml"),
},
options: []chiv.Option{},
},
},
},
Expand All @@ -191,7 +185,7 @@ func TestArchiver_Archive(t *testing.T) {
{
expected: "./testdata/postgres/postgres.yaml",
table: "postgres_table",
key: "postgres_table",
key: "postgres_table.yaml",
options: []chiv.Option{},
},
},
Expand All @@ -205,7 +199,6 @@ func TestArchiver_Archive(t *testing.T) {
bucket: "postgres_bucket",
options: []chiv.Option{
chiv.WithFormat(chiv.CSV),
chiv.WithExtension("csv"),
},
calls: []call{
{
Expand Down Expand Up @@ -234,7 +227,7 @@ func TestArchiver_Archive(t *testing.T) {
{
expected: "./testdata/postgres/postgres_subset.csv",
table: "postgres_table",
key: "postgres_table",
key: "postgres_table.csv",
options: []chiv.Option{
chiv.WithColumns("id", "text_column", "int_column"),
},
Expand All @@ -253,7 +246,7 @@ func TestArchiver_Archive(t *testing.T) {
{
expected: "./testdata/mariadb/happy.csv",
table: "test_table",
key: "test_table",
key: "test_table.csv",
options: []chiv.Option{},
},
},
Expand All @@ -267,13 +260,12 @@ func TestArchiver_Archive(t *testing.T) {
bucket: "mariadb_bucket",
options: []chiv.Option{
chiv.WithFormat(chiv.YAML),
chiv.WithKey("mariadb_table.yaml"),
},
calls: []call{
{
expected: "./testdata/mariadb/happy.yaml",
table: "test_table",
key: "mariadb_table.yaml",
key: "test_table.yaml",
options: []chiv.Option{},
},
},
Expand All @@ -287,7 +279,6 @@ func TestArchiver_Archive(t *testing.T) {
bucket: "mariadb_bucket",
options: []chiv.Option{
chiv.WithFormat(chiv.JSON),
chiv.WithKey("test_table.json"),
},
calls: []call{
{
Expand Down Expand Up @@ -336,6 +327,7 @@ func TestArchiveWithContext(t *testing.T) {
driver = "postgres"
bucket = "postgres_bucket"
table = "postgres_table"
key = "postgres_table.csv"
setup = "./testdata/postgres/postgres_setup.sql"
teardown = "./testdata/postgres/postgres_teardown.sql"
expected = "./testdata/postgres/postgres.csv"
Expand All @@ -354,7 +346,7 @@ func TestArchiveWithContext(t *testing.T) {

require.NoError(t, chiv.ArchiveWithContext(context.Background(), db, uploader, table, bucket))

actual := download(t, downloader, bucket, table)
actual := download(t, downloader, bucket, key)
require.Equal(t, readFile(t, expected), actual)
}

Expand Down
Loading

0 comments on commit 2e42fab

Please sign in to comment.