Skip to content

docs: Update examples to use pgx/v5 #2863

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

Merged
merged 3 commits into from
Oct 17, 2023
Merged
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
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
@@ -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
@@ -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)
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
@@ -15,6 +15,7 @@ sql:
gen:
go:
package: authors
sql_package: pgx/v5
out: postgresql
- schema: mysql/schema.sql
queries: mysql/query.sql
@@ -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
@@ -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"
)

@@ -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{
@@ -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)
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
@@ -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
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.

18 changes: 9 additions & 9 deletions examples/booktest/postgresql/db_test.go
Original file line number Diff line number Diff line change
@@ -5,24 +5,24 @@ package booktest

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

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

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

func TestBooks(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()
dq := New(db)

// create an author
@@ -32,15 +32,15 @@ func TestBooks(t *testing.T) {
}

// create transaction
tx, err := db.Begin()
tx, err := db.Begin(ctx)
if err != nil {
t.Fatal(err)
}

tq := dq.WithTx(tx)

// save first book
now := time.Now()
now := pgtype.Timestamptz{Time: time.Now(), Valid: true}
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: a.AuthorID,
Isbn: "1",
@@ -107,7 +107,7 @@ func TestBooks(t *testing.T) {
}

// tx commit
err = tx.Commit()
err = tx.Commit(ctx)
if err != nil {
t.Fatal(err)
}
@@ -132,7 +132,7 @@ func TestBooks(t *testing.T) {
t.Fatal(err)
}
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)
5 changes: 3 additions & 2 deletions examples/booktest/postgresql/models.go
44 changes: 18 additions & 26 deletions examples/booktest/postgresql/query.sql.go
3 changes: 2 additions & 1 deletion examples/booktest/sqlc.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"schema": "postgresql/schema.sql",
"queries": "postgresql/query.sql",
"engine": "postgresql",
"sql_package": "pgx/v5",
"database": {
"managed": true
},
@@ -47,4 +48,4 @@
]
}
]
}
}
13 changes: 7 additions & 6 deletions examples/jets/postgresql/db.go
9 changes: 3 additions & 6 deletions examples/jets/postgresql/query-building.sql.go
1 change: 1 addition & 0 deletions examples/jets/sqlc.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"schema": "postgresql/schema.sql",
"queries": "postgresql/query-building.sql",
"engine": "postgresql",
"sql_package": "pgx/v5",
"database": {
"managed": true
},
3 changes: 2 additions & 1 deletion examples/ondeck/sqlc.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"schema": "postgresql/schema",
"queries": "postgresql/query",
"engine": "postgresql",
"sql_package": "database/sql",
"database": {
"managed": true
},
@@ -56,4 +57,4 @@
"emit_interface": true
}
]
}
}