Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch CockroachDB driver from lib/pq to jackc/pgx. #697

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions graph/sql/cockroach/cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import (
"github.com/cayleygraph/cayley/graph"
graphlog "github.com/cayleygraph/cayley/graph/log"
csql "github.com/cayleygraph/cayley/graph/sql"
"github.com/cayleygraph/cayley/graph/sql/postgres"
"github.com/lib/pq"
"github.com/jackc/pgx"
_ "github.com/jackc/pgx/stdlib" // registers "pgx" driver
)

const Type = "cockroach"

const driverName = "postgres"

func init() {
csql.Register(Type, csql.Registration{
Driver: driverName,
Driver: "pgx",
HashType: `BYTEA`,
BytesType: `BYTEA`,
HorizonType: `BIGSERIAL`,
Expand All @@ -31,9 +29,17 @@ func init() {
FAMILY fvalue (value, value_string, datatype, language, iri, bnode,
value_int, value_bool, value_float, value_time)
`,
QueryDialect: postgres.QueryDialect,
QueryDialect: csql.QueryDialect{
RegexpOp: "~",
FieldQuote: func(name string) string {
return pgx.Identifier{name}.Sanitize()
},
Placeholder: func(n int) string {
return fmt.Sprintf("$%d", n)
},
},
NoForeignKeys: true,
Error: postgres.ConvError,
Error: convError,
//Estimated: func(table string) string{
// return "SELECT reltuples::BIGINT AS estimate FROM pg_class WHERE relname='"+table+"';"
//},
Expand Down Expand Up @@ -75,8 +81,8 @@ func retryTxCockroach(tx *sql.Tx, stmts func() error) error {
// for either the standard PG errcode SerializationFailureError:40001 or the Cockroach extension
// errcode RetriableError:CR000. The Cockroach extension has been removed server-side, but support
// for it has been left here for now to maintain backwards compatibility.
pqErr, ok := err.(*pq.Error)
if retryable := ok && (pqErr.Code == "CR000" || pqErr.Code == "40001"); !retryable {
pgErr, ok := err.(pgx.PgError)
if retryable := ok && (pgErr.Code == "CR000" || pgErr.Code == "40001"); !retryable {
if released {
err = &AmbiguousCommitError{err}
}
Expand All @@ -88,11 +94,23 @@ func retryTxCockroach(tx *sql.Tx, stmts func() error) error {
}
}

func convError(err error) error {
e, ok := err.(pgx.PgError)
if !ok {
return err
}
switch e.Code {
case "42P07":
return graph.ErrDatabaseExists
}
return err
}

func convInsertError(err error) error {
if err == nil {
return err
}
if pe, ok := err.(*pq.Error); ok {
if pe, ok := err.(pgx.PgError); ok {
if pe.Code == "23505" {
// TODO: reference to delta
return &graph.DeltaError{Err: graph.ErrQuadExists}
Expand Down
21 changes: 18 additions & 3 deletions graph/sql/cockroach/cockroach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ package cockroach

import (
"database/sql"
"net"
"strconv"
"testing"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/sql/sqltest"
"github.com/cayleygraph/cayley/internal/dock"
"github.com/lib/pq"
"github.com/jackc/pgx"
_ "github.com/jackc/pgx/stdlib"
)

func makeCockroach(t testing.TB) (string, graph.Options, func()) {
Expand All @@ -19,15 +22,27 @@ func makeCockroach(t testing.TB) (string, graph.Options, func()) {
conf.Cmd = []string{"start", "--insecure"}

addr, closer := dock.RunAndWait(t, conf, "26257", func(addr string) bool {
conn, err := pq.Open(`postgresql://root@` + addr + `?sslmode=disable`)
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return false
}
port, err := strconv.Atoi(portStr)
if err != nil {
return false
}
conn, err := pgx.Connect(pgx.ConnConfig{
Host: host,
Port: uint16(port),
User: "root",
})
if err != nil {
return false
}
conn.Close()
return true
})
addr = `postgresql://root@` + addr
db, err := sql.Open(driverName, addr+`?sslmode=disable`)
db, err := sql.Open("pgx", addr+`?sslmode=disable`)
if err != nil {
closer()
t.Fatal(err)
Expand Down