Skip to content

Commit

Permalink
create and drop test table; cache go mod
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Mar 21, 2019
1 parent 40ae355 commit 8c3dc7a
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ RUN apk add --no-cache \
git

WORKDIR /chiv
COPY . .

RUN make setup
COPY go.mod .
COPY go.sum .
COPY Makefile .

RUN make setup

COPY . .
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.


9 changes: 0 additions & 9 deletions chiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
64 changes: 62 additions & 2 deletions chiv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 16 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 8c3dc7a

Please sign in to comment.