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

pgx v5 #835

Closed
wants to merge 9 commits into from
Closed
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
22 changes: 12 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
branches:
- main
pull_request:
branches:
- main

jobs:
mysql-tests:
Expand All @@ -12,8 +14,8 @@ jobs:
strategy:
matrix:
go-version:
- "1.17.x"
- "1.18.x"
- "1.21"
- "1.22"

services:
mysql:
Expand Down Expand Up @@ -61,8 +63,8 @@ jobs:
strategy:
matrix:
go-version:
- "1.17.x"
- "1.18.x"
- "1.21"
- "1.22"

services:
postgres:
Expand Down Expand Up @@ -106,8 +108,8 @@ jobs:
strategy:
matrix:
go-version:
- "1.17.x"
- "1.18.x"
- "1.21"
- "1.22"

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -151,8 +153,8 @@ jobs:
strategy:
matrix:
go-version:
- "1.17.x"
- "1.18.x"
- "1.21"
- "1.22"

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -194,8 +196,8 @@ jobs:
strategy:
matrix:
go-version:
- "1.17.x"
- "1.18.x"
- "1.21"
- "1.22"
os:
- "macos-latest"
- "windows-latest"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PACKAGE_NAME := github.com/gobuffalo/pop
GOLANG_CROSS_VERSION ?= v1.18
GOLANG_CROSS_VERSION ?= v1.22

TAGS ?= "sqlite"
GO_BIN ?= go
Expand Down
9 changes: 1 addition & 8 deletions SHOULDERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,9 @@ Thank you to the following **GIANTS**:
* [github.com/inconshreveable/mousetrap](https://godoc.org/github.com/inconshreveable/mousetrap)
* [github.com/jackc/chunkreader](https://godoc.org/github.com/jackc/chunkreader)
* [github.com/jackc/chunkreader/v2](https://godoc.org/github.com/jackc/chunkreader/v2)
* [github.com/jackc/pgconn](https://godoc.org/github.com/jackc/pgconn)
* [github.com/jackc/pgio](https://godoc.org/github.com/jackc/pgio)
* [github.com/jackc/pgmock](https://godoc.org/github.com/jackc/pgmock)
* [github.com/jackc/pgpassfile](https://godoc.org/github.com/jackc/pgpassfile)
* [github.com/jackc/pgproto3](https://godoc.org/github.com/jackc/pgproto3)
* [github.com/jackc/pgproto3/v2](https://godoc.org/github.com/jackc/pgproto3/v2)
* [github.com/jackc/pgservicefile](https://godoc.org/github.com/jackc/pgservicefile)
* [github.com/jackc/pgtype](https://godoc.org/github.com/jackc/pgtype)
* [github.com/jackc/pgx/v4](https://godoc.org/github.com/jackc/pgx/v4)
* [github.com/jackc/puddle](https://godoc.org/github.com/jackc/puddle)
* [github.com/jackc/pgx/v5](https://godoc.org/github.com/jackc/pgx/v5)
* [github.com/jmoiron/sqlx](https://godoc.org/github.com/jmoiron/sqlx)
* [github.com/joho/godotenv](https://godoc.org/github.com/joho/godotenv)
* [github.com/kballard/go-shellquote](https://godoc.org/github.com/kballard/go-shellquote)
Expand Down
3 changes: 1 addition & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"text/template"
Expand Down Expand Up @@ -102,7 +101,7 @@ func ParseConfig(r io.Reader) (map[string]*ConnectionDetails, error) {
return envy.Get(s1, "")
},
})
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"errors"
"fmt"
"math/rand"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -297,7 +296,7 @@ func (c *Connection) timeFunc(name string, fn func() error) error {
// Connection type, TX.ID, and optionally a copy ID. It makes it easy to trace
// related queries for a single request.
//
// examples: "conn-7881415437117811350", "tx-4924907692359316530", "tx-831769923571164863-ytzxZa"
// examples: "conn-7881415437117811350", "tx-4924907692359316530", "tx-831769923571164863-ytzxZa"
func (c *Connection) setID(id ...string) {
if len(id) == 1 {
idElems := strings.Split(id[0], "-")
Expand All @@ -311,7 +310,7 @@ func (c *Connection) setID(id ...string) {
c.ID = fmt.Sprintf("%s-%s", prefix, body)
} else {
prefix := "conn"
body := rand.Int()
body := randx.NonNegativeInt()

if c.TX != nil {
prefix = "tx"
Expand Down
10 changes: 6 additions & 4 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ func (cd *ConnectionDetails) withURL() error {
}
cd.Database = strings.TrimPrefix(u.Path, "/")

hp := strings.Split(u.Host, ":")
cd.Host = hp[0]
if len(hp) > 1 {
cd.Port = hp[1]
cd.Host = u.Hostname()
if u.Port() != "" {
cd.Port = u.Port()
}

if u.User != nil {
cd.User = u.User.Username()
cd.Password, _ = u.User.Password()
}
for k, v := range u.Query() {
cd.setOption(k, v[0])
}
cd.RawOptions = u.RawQuery

return nil
Expand Down
2 changes: 1 addition & 1 deletion connection_instrumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

mysqld "github.com/go-sql-driver/mysql"
"github.com/gobuffalo/pop/v6/logging"
pgx "github.com/jackc/pgx/v4/stdlib"
pgx "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"github.com/luna-duclos/instrumentedsql"
)
Expand Down
20 changes: 6 additions & 14 deletions database.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
mysql:
dialect: "mysql"
database: "pop_test"
host: {{ envOr "MYSQL_HOST" "127.0.0.1" }}
port: {{ envOr "MYSQL_PORT" "3306" }}
user: {{ envOr "MYSQL_USER" "root" }}
password: {{ envOr "MYSQL_PASSWORD" "root#" }}
host: '{{ envOr "MYSQL_HOST" "127.0.0.1" }}'
port: '{{ envOr "MYSQL_PORT" "3306" }}'
user: '{{ envOr "MYSQL_USER" "root" }}'
password: '{{ envOr "MYSQL_PASSWORD" "root#" }}'
options:
readTimeout: 5s

postgres:
url: {{ envOr "POSTGRESQL_URL" "postgres://postgres:postgres%23@localhost:5433/pop_test?sslmode=disable" }}
url: '{{ envOr "POSTGRESQL_URL" "postgres://postgres:postgres%23@localhost:5433/pop_test?sslmode=disable" }}'
pool: 25

cockroach:
# url: "cockroach://root@127.0.0.1:26257/pop_test?application_name=cockroach&sslmode=disable"
url: postgres://{{ envOr "COCKROACH_USER" "root" }}:{{ envOr "COCKROACH_PASSWORD" "" }}@{{ envOr "COCKROACH_HOST" "127.0.0.1" }}:{{ envOr "COCKROACH_PORT" "26257" }}/pop_test?application_name=cockroach&sslmode=disable
dialect: "cockroach"
database: "pop_test"
host: {{ envOr "COCKROACH_HOST" "127.0.0.1" }}
port: {{ envOr "COCKROACH_PORT" "26257" }}
user: {{ envOr "COCKROACH_USER" "root" }}
password: {{ envOr "COCKROACH_PASSWORD" "" }}
options:
sslmode: disable

cockroach_ssl:
dialect: "cockroach"
Expand All @@ -32,4 +25,3 @@ sqlite:
database: "tmp/test.sqlite"
options:
lock: true

4 changes: 4 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type dB struct {
*sqlx.DB
}

func (db *dB) SQLDB() *sql.DB {
return db.DB.DB
}

func (db *dB) TransactionContext(ctx context.Context) (*Tx, error) {
return newTX(ctx, db, nil)
}
Expand Down
61 changes: 43 additions & 18 deletions dialect_cockroach.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package pop

import (
"bufio"
"bytes"
"database/sql"
"fmt"
"io"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"

Expand All @@ -19,7 +20,7 @@ import (
"github.com/gobuffalo/pop/v6/internal/defaults"
"github.com/gobuffalo/pop/v6/logging"
"github.com/gofrs/uuid"
_ "github.com/jackc/pgx/v4/stdlib" // Import PostgreSQL driver
_ "github.com/jackc/pgx/v5/stdlib" // Import PostgreSQL driver
"github.com/jmoiron/sqlx"
)

Expand Down Expand Up @@ -213,18 +214,31 @@ func (p *cockroach) DropDB() error {
}

func (p *cockroach) URL() string {
if p.ConnectionDetails.URL != "" {
return p.ConnectionDetails.URL
}
return p.url().String()
}

func (p *cockroach) url() *url.URL {
c := p.ConnectionDetails
if c.URL != "" {
return c.URL
q := url.Values{}
for k, v := range c.Options {
q.Set(k, v)
}
return &url.URL{
Scheme: "postgres",
User: url.UserPassword(c.User, c.Password),
Host: net.JoinHostPort(c.Host, c.Port),
Path: "/" + c.Database,
RawQuery: q.Encode(),
}
s := "postgres://%s:%s@%s:%s/%s?%s"
return fmt.Sprintf(s, c.User, url.QueryEscape(c.Password), c.Host, c.Port, c.Database, c.OptionsString(""))
}

func (p *cockroach) urlWithoutDb() string {
c := p.ConnectionDetails
s := "postgres://%s:%s@%s:%s/?%s"
return fmt.Sprintf(s, c.User, url.QueryEscape(c.Password), c.Host, c.Port, c.OptionsString(""))
u := p.url()
u.Path = "/"
return u.String()
}

func (p *cockroach) MigrationURL() string {
Expand All @@ -249,7 +263,7 @@ func (p *cockroach) FizzTranslator() fizz.Translator {
}

func (p *cockroach) DumpSchema(w io.Writer) error {
cmd := exec.Command("cockroach", "sql", "-e", "SHOW CREATE ALL TABLES", "-d", p.Details().Database, "--format", "raw")
cmd := exec.Command("cockroach", "sql", "--url", p.URL(), "-e", "SHOW CREATE ALL TABLES", "--format", "raw")

c := p.ConnectionDetails
if defaults.String(c.option("sslmode"), "disable") == "disable" || strings.Contains(c.RawOptions, "sslmode=disable") {
Expand All @@ -271,15 +285,24 @@ func cockroachDumpSchema(deets *ConnectionDetails, cmd *exec.Cmd, w io.Writer) e
return err
}

// --format raw returns comments prefixed with # which is invalid, so we make it a valid SQL comment.
result := regexp.MustCompile("(?m)^#").ReplaceAll(bb.Bytes(), []byte("-- #"))

if _, err := w.Write(result); err != nil {
var written int
s := bufio.NewScanner(&bb)
for s.Scan() {
// --format raw returns lots of useless comments starting with #
if bytes.HasPrefix(s.Bytes(), []byte{'#'}) {
continue
}
if n, err := fmt.Fprintln(w, s.Text()); err != nil {
return err
} else {
written += n
}
}
if err := s.Err(); err != nil {
return err
}

x := bytes.TrimSpace(result)
if len(x) == 0 {
if written == 0 {
return fmt.Errorf("unable to dump schema for %s", deets.Database)
}

Expand Down Expand Up @@ -349,8 +372,10 @@ func newCockroach(deets *ConnectionDetails) (dialect, error) {
}

func finalizerCockroach(cd *ConnectionDetails) {
appName := filepath.Base(os.Args[0])
cd.setOptionWithDefault("application_name", cd.option("application_name"), appName)
if cd.URL == "" {
appName := filepath.Base(os.Args[0])
cd.setOptionWithDefault("application_name", cd.option("application_name"), appName)
}
cd.Port = defaults.String(cd.Port, portCockroach)
if cd.URL != "" {
cd.URL = "postgres://" + trimCockroachPrefix(cd.URL)
Expand Down
3 changes: 1 addition & 2 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/gob"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -178,7 +177,7 @@ func genericLoadSchema(d dialect, r io.Reader) error {
defer db.Close()

// Get reader contents
contents, err := ioutil.ReadAll(r)
contents, err := io.ReadAll(r)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (m *mysql) FizzTranslator() fizz.Translator {

func (m *mysql) DumpSchema(w io.Writer) error {
deets := m.Details()
cmd := exec.Command("mysqldump", "-d", "-h", deets.Host, "-P", deets.Port, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
cmd := exec.Command("mysqldump", "--protocol", "TCP", "-d", "-h", deets.Host, "-P", deets.Port, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
if deets.Port == "socket" {
cmd = exec.Command("mysqldump", "-d", "-S", deets.Host, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
}
Expand Down
11 changes: 4 additions & 7 deletions dialect_mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pop

import (
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -214,13 +213,11 @@ func (s *MySQLSuite) Test_MySQL_DDL_Operations() {

func (s *MySQLSuite) Test_MySQL_DDL_Schema() {
r := s.Require()

f, err := ioutil.TempFile(os.TempDir(), "pop_test_mysql_dump")
f, err := os.CreateTemp(s.T().TempDir(), "pop_test_mysql_dump")
r.NoError(err)
defer func() {
f.Close()
os.Remove(f.Name())
}()
s.T().Cleanup(func() {
_ = f.Close()
})

// do it against "pop_test"
err = PDB.Dialect.DumpSchema(f)
Expand Down
2 changes: 1 addition & 1 deletion dialect_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/gobuffalo/pop/v6/internal/defaults"
"github.com/gobuffalo/pop/v6/logging"
"github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v4/stdlib" // Load pgx driver
_ "github.com/jackc/pgx/v5/stdlib" // Load pgx driver
"github.com/jmoiron/sqlx"
)

Expand Down
Loading
Loading