From a5edf72ba2ff477db71a92da5906541f23e732df Mon Sep 17 00:00:00 2001 From: Patryk Kalinowski Date: Fri, 26 Feb 2021 15:05:07 +0300 Subject: [PATCH 1/3] root: add Makefile and docker-compose.yml --- CONTRIBUTING.md | 14 ++++++++++++++ Makefile | 25 +++++++++++++++++++++++++ docker-compose.yml | 10 ++++++++++ ingestion.go | 7 ++++--- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 Makefile create mode 100644 docker-compose.yml diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b092dc6..5caa4cd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..157400d --- /dev/null +++ b/Makefile @@ -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 + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9b308fe --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/ingestion.go b/ingestion.go index e74cfc1..adb5877 100644 --- a/ingestion.go +++ b/ingestion.go @@ -2,6 +2,7 @@ package terracost import ( "context" + "fmt" "github.com/cycloidio/terracost/price" "github.com/cycloidio/terracost/product" @@ -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 } From a4814058027cdabbf41995d07eec857ab4873fc2 Mon Sep 17 00:00:00 2001 From: Patryk Kalinowski Date: Fri, 26 Feb 2021 15:06:27 +0300 Subject: [PATCH 2/3] e2e: change the MySQL host and DB credentials --- e2e/aws_estimation_test.go | 2 +- e2e/aws_ingestion_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/aws_estimation_test.go b/e2e/aws_estimation_test.go index ea2cb9f..f617704 100644 --- a/e2e/aws_estimation_test.go +++ b/e2e/aws_estimation_test.go @@ -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) diff --git a/e2e/aws_ingestion_test.go b/e2e/aws_ingestion_test.go index 68cfb61..8317dc3 100644 --- a/e2e/aws_ingestion_test.go +++ b/e2e/aws_ingestion_test.go @@ -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") From 5d441a11397d0faf25071faf999f131f183958f4 Mon Sep 17 00:00:00 2001 From: Patryk Kalinowski Date: Fri, 26 Feb 2021 15:06:44 +0300 Subject: [PATCH 3/3] scripts: add a script to migrate the DB --- scripts/migrate.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 scripts/migrate.go diff --git a/scripts/migrate.go b/scripts/migrate.go new file mode 100644 index 0000000..73e065c --- /dev/null +++ b/scripts/migrate.go @@ -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!") +}