Skip to content

Commit

Permalink
Include a DB Docker container and a Makefile (#25)
Browse files Browse the repository at this point in the history
* root: add Makefile and docker-compose.yml

* e2e: change the MySQL host and DB credentials

* scripts: add a script to migrate the DB
  • Loading branch information
Patryk Kalinowski authored Mar 3, 2021
1 parent baf692f commit 74dcbb5
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ $ git fetch upstream --prune
$ git rebase upstream/master
```

### Test your submission

First-time setup of the development environment requires running the database migrations:

```shell
$ make db-migrate
```

Run all the tests:

```shell
$ make test
```

## Adding a new resource

### AWS
Expand Down
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
IS_CI ?= 0

DOCKER_COMPOSE_CMD := docker-compose
GO_CMD := go
GO_TEST_CMD := $(GO_CMD) test ./...
GO_RUN_CMD := $(GO_CMD) run

.PHONY: test
test: db-up
@$(GO_TEST_CMD)

.PHONY: db-up
db-up: # Start the DB server
ifeq ($(IS_CI), 0)
@$(DOCKER_COMPOSE_CMD) up -d database
endif

.PHONY: down
down:
@$(DOCKER_COMPOSE_CMD) down

.PHONY: db-migrate
db-migrate: db-up
@$(GO_RUN_CMD) scripts/migrate.go

10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'
services:
database:
image: mysql:8.0.21
command: --default-authentication-plugin=mysql_native_password
ports:
- '33060:3306'
environment:
- MYSQL_ROOT_PASSWORD=terracost
- MYSQL_DATABASE=terracost_test
2 changes: 1 addition & 1 deletion e2e/aws_estimation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAWSEstimation(t *testing.T) {
}

ctx := context.Background()
db, err := sql.Open("mysql", "root:youdeploy@tcp(localhost:3306)/youdeploy_public_test?multiStatements=true")
db, err := sql.Open("mysql", "root:terracost@tcp(localhost:33060)/terracost_test?multiStatements=true")
require.NoError(t, err)

backend := mysql.NewBackend(db)
Expand Down
2 changes: 1 addition & 1 deletion e2e/aws_ingestion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAWSIngestion(t *testing.T) {

httpClient := mock.NewHTTPClient(ctrl)

db, err := sql.Open("mysql", "root:youdeploy@tcp(localhost:3306)/youdeploy_public_test?multiStatements=true")
db, err := sql.Open("mysql", "root:terracost@tcp(localhost:33060)/terracost_test?multiStatements=true")
require.NoError(t, err)

f, err := os.Open("testdata/AmazonEC2_eu-west-3.csv")
Expand Down
7 changes: 4 additions & 3 deletions ingestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package terracost

import (
"context"
"fmt"

"github.com/cycloidio/terracost/price"
"github.com/cycloidio/terracost/product"
Expand Down Expand Up @@ -40,18 +41,18 @@ func IngestPricing(ctx context.Context, backend Backend, ingester Ingester) erro
var err error
pp.Product.ID, err = backend.Product().Upsert(ctx, pp.Product)
if err != nil {
return err
return fmt.Errorf("failed to upsert product (SKU=%q): %w", pp.Product.SKU, err)
}
skuProductID[pp.Product.SKU] = pp.Product.ID
}

if _, err := backend.Price().Upsert(ctx, pp); err != nil {
return err
return fmt.Errorf("failed to upsert price (SKU=%q): %w", pp.Product.SKU, err)
}
}

if err := ingester.Err(); err != nil {
return err
return fmt.Errorf("unexpected ingester error: %w", err)
}
return nil
}
25 changes: 25 additions & 0 deletions scripts/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"context"
"database/sql"
"fmt"
"log"

_ "github.com/go-sql-driver/mysql"

"github.com/cycloidio/terracost/mysql"
)

func main() {
db, err := sql.Open("mysql", "root:terracost@tcp(localhost:33060)/terracost_test?multiStatements=true")
if err != nil {
log.Fatal(err)
}

if err := mysql.Migrate(context.Background(), db, "_migrations"); err != nil {
log.Fatal(err)
}

fmt.Println("Migrated successfully!")
}

0 comments on commit 74dcbb5

Please sign in to comment.