From 8c3dc7a413283bd86bf91bc29766f07bf406ff2d Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Thu, 21 Mar 2019 14:41:55 -0400 Subject: [PATCH] create and drop test table; cache go mod --- Dockerfile | 9 ++++++-- README.md | 4 +++- chiv.go | 9 -------- chiv_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++-- go.mod | 1 + go.sum | 2 ++ options.go | 16 +++++++++++++ 7 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 options.go diff --git a/Dockerfile b/Dockerfile index 0010863..ee259f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,11 @@ RUN apk add --no-cache \ git WORKDIR /chiv -COPY . . -RUN make setup \ No newline at end of file +COPY go.mod . +COPY go.sum . +COPY Makefile . + +RUN make setup + +COPY . . \ No newline at end of file diff --git a/README.md b/README.md index 2af28f4..2143eb4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # chiv -Archive arbitrarily large database tables to Amazon S3 using cursors and multipart upload +Archive arbitrarily large relational database tables to Amazon S3. + + diff --git a/chiv.go b/chiv.go index 1a45ab6..03ba9b0 100644 --- a/chiv.go +++ b/chiv.go @@ -15,15 +15,6 @@ type Archiver struct { // TODO other options like batch size... in bytes or rows? } -type Option func(*Archiver) - -type Format int - -const ( - CSV Format = iota - JSON -) - const ( DefaultFormat = CSV ) diff --git a/chiv_test.go b/chiv_test.go index 586731f..27c5b2c 100644 --- a/chiv_test.go +++ b/chiv_test.go @@ -3,12 +3,72 @@ package chiv_test import ( + "database/sql" "testing" + "github.com/aws/aws-sdk-go/aws" + "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/lib/pq" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/gavincabbage/chiv" ) func TestArchive(t *testing.T) { - chiv.Archive() - t.Log("test test test") + db := newDB(t) + uploader := newUploader(t) + + mustExec(t, db, ` + CREATE TABLE IF NOT EXISTS "test_table" ( + id UUID PRIMARY KEY, + text_column TEXT, + char_column VARCHAR(10), + int_column INTEGER, + bool_column BOOLEAN, + ts_column TIMESTAMP + );`) + defer mustExec(t, db, `DROP TABLE "test_table";`) + + subject := chiv.NewArchiver(db, uploader) + assert.NotNil(t, subject) +} + +func newDB(t *testing.T) *sql.DB { + // TODO make this generic so it can be run locally as well + const databaseURL = "postgres://postgres@postgres/test?sslmode=disable" + + db, err := sql.Open("postgres", databaseURL) + require.NoError(t, err) + + return db +} + +func newUploader(t *testing.T) *s3manager.Uploader { + const ( + awsRegion = "us-east-1" + s3Endpoint = "http://s3:4572" + ) + + awsConfig := aws.NewConfig(). + WithRegion(awsRegion). + WithDisableSSL(true). + WithCredentials(credentials.NewEnvCredentials()) + + awsSession, err := session.NewSession(awsConfig) + require.NoError(t, err) + + client := s3.New(awsSession) + client.Endpoint = s3Endpoint + + return s3manager.NewUploaderWithClient(client) +} + +func mustExec(t *testing.T, db *sql.DB, query string) { + if _, err := db.Exec(query); err != nil { + t.Error(err) + } } diff --git a/go.mod b/go.mod index 14ab092..af0fcfb 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/aws/aws-sdk-go v1.18.6 + github.com/lib/pq v1.0.0 github.com/stretchr/testify v1.3.0 // indirect golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect ) diff --git a/go.sum b/go.sum index 961d557..29f8152 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/options.go b/options.go new file mode 100644 index 0000000..c22afdd --- /dev/null +++ b/options.go @@ -0,0 +1,16 @@ +package chiv + +type Option func(*Archiver) + +type Format int + +const ( + CSV Format = iota + JSON +) + +func WithFormat(f Format) Option { + return func(a *Archiver) { + a.format = f + } +}