Skip to content

Commit

Permalink
Merge pull request #714 from upper/v4
Browse files Browse the repository at this point in the history
merge-back into master
  • Loading branch information
xiam authored Jun 23, 2024
2 parents ee0862b + 2e63463 commit b8d22cf
Show file tree
Hide file tree
Showing 60 changed files with 919 additions and 1,174 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
go-version: [1.19.x, 1.20.x, 1.21.x]
go-version: [1.20.x, 1.21.x, 1.22.x]

runs-on: ${{ matrix.os }}

Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
SHELL ?= /bin/bash
SHELL ?= bash
CPU_CORES ?= $(shell nproc)

PARALLEL_FLAGS ?= --halt-on-error 2 --jobs=2 -v -u
PARALLEL_FLAGS ?= --halt-on-error 2 --jobs=$(CPU_CORES) -v -u

TEST_FLAGS ?=
TEST_FLAGS ?= -v -failfast -race -timeout 20m

UPPER_DB_LOG ?= WARN

Expand Down
10 changes: 5 additions & 5 deletions adapter/cockroachdb/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SHELL ?= bash

COCKROACHDB_VERSION ?= v23.1.8
COCKROACHDB_SUPPORTED ?= $(COCKROACHDB_VERSION) v22.2.9 v21.2.9
PROJECT ?= upper_cockroachdb_$(COCKROACHDB_VERSION)
COCKROACHDB_VERSION ?= v23.2.6
COCKROACHDB_SUPPORTED ?= $(COCKROACHDB_VERSION) v22.2.19 v21.2.17
PROJECT ?= $(subst .,_,"upper_cockroachdb_$(COCKROACHDB_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 26257
Expand Down Expand Up @@ -30,7 +30,7 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 5 && \
psql -Uroot -h$(DB_HOST) -p$(DB_PORT) postgres -c "\
DROP USER IF EXISTS $(DB_USERNAME); \
Expand All @@ -41,7 +41,7 @@ server-up: server-down
"

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
14 changes: 7 additions & 7 deletions adapter/cockroachdb/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ func (vs values) Isset(k string) bool {
//
// You can use a ConnectionURL struct as an argument for Open:
//
// var settings = cockroachdb.ConnectionURL{
// Host: "localhost", // Database server IP or host name.
// Database: "peanuts", // Database name.
// User: "cbrown", // Optional user name.
// Password: "snoopy", // Optional user password.
// }
// var settings = cockroachdb.ConnectionURL{
// Host: "localhost", // Database server IP or host name.
// Database: "peanuts", // Database name.
// User: "cbrown", // Optional user name.
// Password: "snoopy", // Optional user password.
// }
//
// sess, err = cockroachdb.Open(settings)
// sess, err = cockroachdb.Open(settings)
//
// If you already have a valid DSN, you can use ParseURL to convert it into
// a ConnectionURL before passing it to Open.
Expand Down
15 changes: 8 additions & 7 deletions adapter/cockroachdb/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"database/sql/driver"
"time"

"github.com/upper/db/v4"
"github.com/upper/db/v4/internal/sqlbuilder"
)

Expand Down Expand Up @@ -90,12 +91,12 @@ func DecodeJSONB(dst interface{}, src interface{}) error {
//
// Example:
//
// type MyCustomStruct struct {
// ID int64 `db:"id" json:"id"`
// Name string `db:"name" json:"name"`
// ...
// cockroachdb.JSONBConverter
// }
// type MyCustomStruct struct {
// ID int64 `db:"id" json:"id"`
// Name string `db:"name" json:"name"`
// ...
// cockroachdb.JSONBConverter
// }
type JSONBConverter struct {
}

Expand Down Expand Up @@ -144,7 +145,7 @@ func (t *timeWrapper) Scan(src interface{}) error {
}

func (d *database) ConvertValueContext(ctx context.Context, in interface{}) interface{} {
tz, _ := ctx.Value("timezone").(*time.Location)
tz, _ := ctx.Value(db.ContextKey("timezone")).(*time.Location)

switch v := in.(type) {
case *time.Time:
Expand Down
1 change: 1 addition & 0 deletions adapter/cockroachdb/custom_types_pgx.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !pq
// +build !pq

// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
Expand Down
1 change: 1 addition & 0 deletions adapter/cockroachdb/custom_types_pq.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build pq
// +build pq

// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
Expand Down
8 changes: 6 additions & 2 deletions adapter/cockroachdb/database_pgx.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !pq
// +build !pq

// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
Expand Down Expand Up @@ -26,9 +27,12 @@ package cockroachdb
import (
"context"
"database/sql"

"time"

_ "github.com/jackc/pgx/v4/stdlib"
"github.com/upper/db/v4"
"github.com/upper/db/v4/internal/sqladapter"
"time"
)

func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
Expand All @@ -38,7 +42,7 @@ func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
}
if tz := connURL.Options["timezone"]; tz != "" {
loc, _ := time.LoadLocation(tz)
ctx := context.WithValue(sess.Context(), "timezone", loc)
ctx := context.WithValue(sess.Context(), db.ContextKey("timezone"), loc)
sess.SetContext(ctx)
}
return sql.Open("pgx", dsn)
Expand Down
7 changes: 5 additions & 2 deletions adapter/cockroachdb/database_pq.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build pq
// +build pq

// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
Expand Down Expand Up @@ -26,9 +27,11 @@ package cockroachdb
import (
"context"
"database/sql"
"time"

_ "github.com/lib/pq"
db "github.com/upper/db/v4"
"github.com/upper/db/v4/internal/sqladapter"
"time"
)

func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
Expand All @@ -38,7 +41,7 @@ func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
}
if tz := connURL.Options["timezone"]; tz != "" {
loc, _ := time.LoadLocation(tz)
ctx := context.WithValue(sess.Context(), "timezone", loc)
ctx := context.WithValue(sess.Context(), db.ContextKey("timezone"), loc)
sess.SetContext(ctx)
}
return sql.Open("postgres", dsn)
Expand Down
3 changes: 0 additions & 3 deletions adapter/cockroachdb/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
version: '3'

services:

server:
image: cockroachdb/cockroach:${COCKROACHDB_VERSION:-v2}
environment:
Expand Down
6 changes: 3 additions & 3 deletions adapter/mongo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL ?= bash

MONGO_VERSION ?= 4
MONGO_SUPPORTED ?= $(MONGO_VERSION) 3
PROJECT ?= upper_mongo_$(MONGO_VERSION)
PROJECT ?= $(subst .,_,"upper_mongo_$(MONGO_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 27017
Expand Down Expand Up @@ -31,11 +31,11 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 10

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
80 changes: 22 additions & 58 deletions adapter/mongo/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@ package mongo

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConnectionURL(t *testing.T) {

c := ConnectionURL{}

// Default connection string is only the protocol.
if c.String() != "" {
t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String())
}
assert.Equal(t, "", c.String(), "Expecting default connectiong string to be empty")

// Adding a database name.
c.Database = "myfilename"

if c.String() != "mongodb://myfilename" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://myfilename", c.String())

// Adding an option.
c.Options = map[string]string{
Expand All @@ -54,31 +51,22 @@ func TestConnectionURL(t *testing.T) {
// Setting host.
c.Host = "localhost"

if c.String() != "mongodb://user:pass@localhost/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://user:pass@localhost/myfilename?cache=foobar&mode=ro", c.String())

// Setting host and port.
c.Host = "localhost:27017"

if c.String() != "mongodb://user:pass@localhost:27017/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://user:pass@localhost:27017/myfilename?cache=foobar&mode=ro", c.String())

// Setting cluster.
c.Host = "localhost,1.2.3.4,example.org:1234"

if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro", c.String())

// Setting another database.
c.Database = "another_database"

if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}

assert.Equal(t, "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro", c.String())
}

func TestParseConnectionURL(t *testing.T) {
Expand All @@ -88,48 +76,24 @@ func TestParseConnectionURL(t *testing.T) {

s = "mongodb:///mydatabase"

if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}
u, err = ParseURL(s)
require.NoError(t, err)

if u.Database != "mydatabase" {
t.Fatal("Failed to parse database.")
}
assert.Equal(t, "mydatabase", u.Database)

s = "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro"

if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}
u, err = ParseURL(s)
require.NoError(t, err)

if u.Database != "another_database" {
t.Fatal("Failed to get database.")
}

if u.Options["cache"] != "foobar" {
t.Fatal("Expecting option.")
}

if u.Options["mode"] != "ro" {
t.Fatal("Expecting option.")
}

if u.User != "user" {
t.Fatal("Expecting user.")
}

if u.Password != "pass" {
t.Fatal("Expecting password.")
}

if u.Host != "localhost,1.2.3.4,example.org:1234" {
t.Fatal("Expecting host.")
}
assert.Equal(t, "another_database", u.Database)
assert.Equal(t, "foobar", u.Options["cache"])
assert.Equal(t, "ro", u.Options["mode"])
assert.Equal(t, "user", u.User)
assert.Equal(t, "pass", u.Password)
assert.Equal(t, "localhost,1.2.3.4,example.org:1234", u.Host)

s = "http://example.org"

if _, err = ParseURL(s); err == nil {
t.Fatal("Expecting error.")
}

_, err = ParseURL(s)
require.Error(t, err)
}
6 changes: 3 additions & 3 deletions adapter/mssql/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL ?= bash

MSSQL_VERSION ?= 2022-latest
MSSQL_SUPPORTED ?= $(MSSQL_VERSION) 2019-latest
PROJECT ?= upper_mssql_$(MSSQL_VERSION)
PROJECT ?= $(subst .,_,"upper_mssql_$(MSSQL_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 1433
Expand Down Expand Up @@ -31,11 +31,11 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 20

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
Loading

0 comments on commit b8d22cf

Please sign in to comment.