Dump AWS Dynamo DB tables to yaml files for fixtures. To run DH_SPEC_FILE=.config.yml go run ./cmd/dumper/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"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/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
"github.com/deliveryhero/pd-qr/internal/infrastructure/config" | |
"gopkg.in/yaml.v2" | |
) | |
func getDynamoDBClient(cfg *config.Config) (*dynamodb.DynamoDB, error) { | |
creds := cfg.General.AWSCreds | |
awsCreds := credentials.NewStaticCredentials( | |
creds.Key, | |
creds.Secret, | |
"", | |
) | |
// For using localstack without introducing new sub domain | |
s3ForcePathStyle := true | |
awscfg := &aws.Config{ | |
Region: aws.String(creds.Region), | |
Credentials: awsCreds, | |
S3ForcePathStyle: &s3ForcePathStyle, | |
} | |
if creds.Endpoint != "" { | |
awscfg.Endpoint = aws.String(creds.Endpoint) | |
} | |
session, err := session.NewSession(awscfg) | |
if err != nil { | |
return nil, err | |
} | |
return dynamodb.New(session), nil | |
} | |
func dumpDdbTable(ddbClient *dynamodb.DynamoDB, tableName string, directory string) error { | |
scanInput := &dynamodb.ScanInput{ | |
TableName: aws.String(tableName), | |
} | |
req, output := ddbClient.ScanRequest(scanInput) | |
err := req.Send() | |
if err != nil { | |
return err | |
} | |
if output == nil || len(output.Items) == 0 { | |
return nil | |
} | |
var out []map[string]interface{} | |
err = dynamodbattribute.UnmarshalListOfMaps(output.Items, &out) | |
if err != nil { | |
return err | |
} | |
d, err := yaml.Marshal(&out) | |
if err != nil { | |
return err | |
} | |
f, err := os.Create(directory + "/" + tableName + ".yml") | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
f.WriteString(string(d)) | |
return nil | |
} | |
func run() error { | |
cfg, err := config.Load("FakeCommitHash", "FakeTag") | |
if err != nil { | |
return fmt.Errorf("unable to load configurations: '%v'", err) | |
} | |
ddbClient, err := getDynamoDBClient(cfg) | |
if err != nil { | |
return err | |
} | |
tables := []string{"asia-local-qr-codes", "asia-local-qr-exports"} | |
for _, t := range tables { | |
err = dumpDdbTable(ddbClient, t, "./tests/integration/testdata/fixtures") | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func main() { | |
fmt.Println("Dumping dynamodb tables to fixtures...") | |
if err := run(); err != nil { | |
log.Fatalln(err) | |
} | |
fmt.Println("Done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment