Skip to content

Commit

Permalink
docs(examples): Update examples to use pgx/v5 (#2863)
Browse files Browse the repository at this point in the history
* docs: Update examples to use pgx/v5

* test: Update example tests to work correctly with pgx v5

* test: Fix examples tests
  • Loading branch information
andrewmbenton authored Oct 17, 2023
1 parent 1ac6f19 commit 795bbc7
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 114 deletions.
13 changes: 7 additions & 6 deletions examples/authors/postgresql/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions examples/authors/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ package authors

import (
"context"
"database/sql"
"testing"

_ "github.com/lib/pq"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"

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

func TestAuthors(t *testing.T) {
ctx := context.Background()
uri := hosted.PostgreSQL(t, []string{"schema.sql"})
db, err := sql.Open("postgres", uri)
db, err := pgx.Connect(ctx, uri)
if err != nil {
t.Fatal(err)
}
defer db.Close()
defer db.Close(ctx)

ctx := context.Background()
q := New(db)

// list all authors
Expand All @@ -34,7 +34,7 @@ func TestAuthors(t *testing.T) {
// create an author
insertedAuthor, err := q.CreateAuthor(ctx, CreateAuthorParams{
Name: "Brian Kernighan",
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
Bio: pgtype.Text{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
})
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions examples/authors/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions examples/authors/postgresql/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sql:
gen:
go:
package: authors
sql_package: pgx/v5
out: postgresql
- schema: mysql/schema.sql
queries: mysql/query.sql
Expand Down Expand Up @@ -45,4 +46,4 @@ rules:
rule: "postgresql.explain.plan.total_cost > 300.0"
- name: mysql-query-too-costly
message: "Too costly"
rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0"
rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0"
23 changes: 11 additions & 12 deletions examples/batch/postgresql/batch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/batch/postgresql/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions examples/batch/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"testing"
"time"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
)

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

now := time.Now()
now := pgtype.Timestamptz{Time: time.Now(), Valid: true}

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

for _, book := range books0 {
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
Expand Down
25 changes: 12 additions & 13 deletions examples/batch/postgresql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/batch/postgresql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/batch/postgresql/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/batch/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"rules": [
"sqlc/db-prepare"
],
"sql_package": "pgx/v4",
"sql_package": "pgx/v5",
"emit_json_tags": true,
"emit_prepared_queries": true,
"emit_interface": true
Expand Down
13 changes: 7 additions & 6 deletions examples/booktest/postgresql/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 795bbc7

Please sign in to comment.