Skip to content

Commit 795bbc7

Browse files
docs(examples): Update examples to use pgx/v5 (#2863)
* docs: Update examples to use pgx/v5 * test: Update example tests to work correctly with pgx v5 * test: Fix examples tests
1 parent 1ac6f19 commit 795bbc7

File tree

21 files changed

+108
-114
lines changed

21 files changed

+108
-114
lines changed

examples/authors/postgresql/db.go

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/postgresql/db_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ package authors
55

66
import (
77
"context"
8-
"database/sql"
98
"testing"
109

11-
_ "github.com/lib/pq"
10+
"github.com/jackc/pgx/v5"
11+
"github.com/jackc/pgx/v5/pgtype"
1212

1313
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
1414
)
1515

1616
func TestAuthors(t *testing.T) {
17+
ctx := context.Background()
1718
uri := hosted.PostgreSQL(t, []string{"schema.sql"})
18-
db, err := sql.Open("postgres", uri)
19+
db, err := pgx.Connect(ctx, uri)
1920
if err != nil {
2021
t.Fatal(err)
2122
}
22-
defer db.Close()
23+
defer db.Close(ctx)
2324

24-
ctx := context.Background()
2525
q := New(db)
2626

2727
// list all authors
@@ -34,7 +34,7 @@ func TestAuthors(t *testing.T) {
3434
// create an author
3535
insertedAuthor, err := q.CreateAuthor(ctx, CreateAuthorParams{
3636
Name: "Brian Kernighan",
37-
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
37+
Bio: pgtype.Text{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
3838
})
3939
if err != nil {
4040
t.Fatal(err)

examples/authors/postgresql/models.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/postgresql/query.sql.go

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/sqlc.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ sql:
1515
gen:
1616
go:
1717
package: authors
18+
sql_package: pgx/v5
1819
out: postgresql
1920
- schema: mysql/schema.sql
2021
queries: mysql/query.sql
@@ -45,4 +46,4 @@ rules:
4546
rule: "postgresql.explain.plan.total_cost > 300.0"
4647
- name: mysql-query-too-costly
4748
message: "Too costly"
48-
rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0"
49+
rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0"

examples/batch/postgresql/batch.go

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/batch/postgresql/db.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/batch/postgresql/db_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/jackc/pgx/v4"
11+
"github.com/jackc/pgx/v5"
12+
"github.com/jackc/pgx/v5/pgtype"
1213
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
1314
)
1415

@@ -31,7 +32,7 @@ func TestBatchBooks(t *testing.T) {
3132
t.Fatal(err)
3233
}
3334

34-
now := time.Now()
35+
now := pgtype.Timestamptz{Time: time.Now(), Valid: true}
3536

3637
// batch insert new books
3738
newBooksParams := []CreateBookParams{
@@ -114,7 +115,7 @@ func TestBatchBooks(t *testing.T) {
114115
})
115116

116117
for _, book := range books0 {
117-
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
118+
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
118119
author, err := dq.GetAuthor(ctx, book.AuthorID)
119120
if err != nil {
120121
t.Fatal(err)

examples/batch/postgresql/models.go

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/batch/postgresql/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/batch/postgresql/query.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/batch/sqlc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"rules": [
2020
"sqlc/db-prepare"
2121
],
22-
"sql_package": "pgx/v4",
22+
"sql_package": "pgx/v5",
2323
"emit_json_tags": true,
2424
"emit_prepared_queries": true,
2525
"emit_interface": true

examples/booktest/postgresql/db.go

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)