From 1f36532869be8586f00af96ccf3bc43d42851739 Mon Sep 17 00:00:00 2001 From: Hiroaki Sano Date: Thu, 21 Dec 2023 11:25:01 +0900 Subject: [PATCH 1/4] Support Go 1.21 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bf04295..501c096 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/achiku/planter -go 1.19 +go 1.21 require ( github.com/alecthomas/kingpin v2.2.6+incompatible From 7032387cc94c76036ba5ebcc81f62fba2ab9f24a Mon Sep 17 00:00:00 2001 From: Hiroaki Sano Date: Thu, 21 Dec 2023 11:25:37 +0900 Subject: [PATCH 2/4] Drop Go 1.17, 1.18 --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d97170f..9fb6a82 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,9 +16,9 @@ jobs: # - macOS-latest # - windows-latest go: + - "1.21" + - "1.20" - "1.19" - - "1.18" - - "1.17" services: postgres: image: postgres:12 From ea529f9b0b2b4575156fc9adece29d33d982d1ea Mon Sep 17 00:00:00 2001 From: Hiroaki Sano Date: Thu, 21 Dec 2023 11:26:54 +0900 Subject: [PATCH 3/4] Update Go modules --- go.mod | 6 +++--- go.sum | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 501c096..b68a84e 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,11 @@ go 1.21 require ( github.com/alecthomas/kingpin v2.2.6+incompatible - github.com/lib/pq v1.2.0 - github.com/pkg/errors v0.8.1 + github.com/lib/pq v1.10.9 + github.com/pkg/errors v0.9.1 ) require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect - github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect + github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect ) diff --git a/go.sum b/go.sum index deb2cb3..0f6a243 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,14 @@ github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvr github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= 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/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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= From f6f635218f0c4d82caadb8e60fffb755632529cd Mon Sep 17 00:00:00 2001 From: Hiroaki Sano Date: Thu, 21 Dec 2023 16:33:59 +0900 Subject: [PATCH 4/4] feat: Add docker-compose.yml to run test on Docker --- Makefile | 8 ++++++++ README.md | 8 +++++++- docker-compose.yml | 17 +++++++++++++++++ initdb/init.sql | 3 +++ planter_test.go | 9 ++++++++- 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 docker-compose.yml create mode 100644 initdb/init.sql diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4dcc59e --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: test +test: + docker-compose up -d + DB_PORT=15432 go test -v ./... + +.PHONY: clean +clean: + docker-compose down -v diff --git a/README.md b/README.md index 58fd6ad..8e34bc2 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,13 @@ Args: ## Test -setup database. +Run on docker + +``` +make +``` + +or setup manually and run test ``` create database planter; diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1fdd9c1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +services: + db: + image: postgres:12.11 + ports: + - "15432:5432" + environment: + POSTGRES_HOST_AUTH_METHOD: trust + volumes: + - db:/var/lib/postgresql/data + - ./initdb:/docker-entrypoint-initdb.d + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 1s + timeout: 1s + retries: 10 +volumes: + db: diff --git a/initdb/init.sql b/initdb/init.sql new file mode 100644 index 0000000..dc1067c --- /dev/null +++ b/initdb/init.sql @@ -0,0 +1,3 @@ +CREATE DATABASE planter; +CREATE USER planter; +CREATE SCHEMA planter AUTHORIZATION planter; diff --git a/planter_test.go b/planter_test.go index 8b24227..09ffc88 100644 --- a/planter_test.go +++ b/planter_test.go @@ -2,7 +2,9 @@ package main import ( "database/sql" + "fmt" "io/ioutil" + "os" "reflect" "testing" ) @@ -12,7 +14,12 @@ import ( // CREATE DATABASE planter OWNER planter; func testPgSetup(t *testing.T) (*sql.DB, func()) { - conn, err := sql.Open("postgres", "user=planter dbname=planter sslmode=disable") + port := os.Getenv("DB_PORT") + if port == "" { + port = "5432" + } + dsn := fmt.Sprintf("user=planter port=%s dbname=planter sslmode=disable", port) + conn, err := sql.Open("postgres", dsn) if err != nil { t.Fatal(err) }