Skip to content

Commit

Permalink
options pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Mar 21, 2019
1 parent 092b654 commit 40ae355
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
75 changes: 73 additions & 2 deletions chiv.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,81 @@
// Package chiv archives arbitrarily large relational database tables to Amazon S3.
package chiv

import (
"context"
"database/sql"

"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func Archive(uploader *s3manager.Uploader) {
type Archiver struct {
db *sql.DB
s3 *s3manager.Uploader
format Format // TODO WithFormat(Format) Option
// 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
)

// NewArchiver constructs an Archiver with the given database connection, S3 uploader and options.
func NewArchiver(db *sql.DB, s3 *s3manager.Uploader, options ...Option) *Archiver {
a := Archiver{
db: db,
s3: s3,
format: DefaultFormat,
}

for _, option := range options {
option(&a)
}

return &a
}

// Archive a database table to S3.
func (a *Archiver) Archive(table string, options ...Option) error {
return a.ArchiveWithContext(context.Background(), table, options...)
}

// Archive a database table to S3 with context.
func (a *Archiver) ArchiveWithContext(ctx context.Context, table string, options ...Option) error {
archiver := archiver{
db: a.db, // TODO do these need to be top level or use archiver.config.db and archiver.config.s3?
s3: a.s3,
ctx: ctx,
config: a,
}

for _, option := range options {
option(archiver.config)
}

return archiver.archive(table)
}

type archiver struct {
db *sql.DB
s3 *s3manager.Uploader
ctx context.Context
config *Archiver
}

func (a *archiver) archive(table string) error {

// TODO the work
// db cursor selecting: ???
// s3 streaming: https://docs.aws.amazon.com/code-samples/latest/catalog/go-s3-upload_arbitrary_sized_stream.go.html

//https://docs.aws.amazon.com/code-samples/latest/catalog/go-s3-upload_arbitrary_sized_stream.go.html
return nil // TODO return size or some other info along w/ error?
}
4 changes: 3 additions & 1 deletion chiv_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package chiv_test includes integration tests external to package chiv
// and relies on external services postgres and s3 (localstack) via CodeShip.
package chiv_test

import (
Expand All @@ -9,4 +11,4 @@ import (
func TestArchive(t *testing.T) {
chiv.Archive()
t.Log("test test test")
}
}

0 comments on commit 40ae355

Please sign in to comment.