Skip to content

Commit

Permalink
Remove all batch queries
Browse files Browse the repository at this point in the history
PGXv5 dramatically changes how batch queries work. In v4 we use a separate
batch and scan call. In v5, we use a callback with generics for type safety.

Remove batch queries for now since it complicates upgrading to pgx V5. We may
them back in later with more ergonomic API.

#74
  • Loading branch information
jschaf committed Jan 2, 2024
1 parent 8ecc0cb commit f37d9a6
Show file tree
Hide file tree
Showing 45 changed files with 9 additions and 3,575 deletions.
262 changes: 0 additions & 262 deletions example/author/query.sql.go

Large diffs are not rendered by default.

164 changes: 0 additions & 164 deletions example/author/query.sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package author
import (
"context"
"errors"
"github.com/jschaf/pggen/internal/errs"
"github.com/jschaf/pggen/internal/ptrs"
"github.com/stretchr/testify/require"
"testing"
Expand Down Expand Up @@ -40,21 +39,6 @@ func TestNewQuerier_FindAuthorByID(t *testing.T) {
t.Fatalf("expected no rows error to wrap pgx.ErrNoRows; got %s", err)
}
})

t.Run("FindAuthorByIDBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.FindAuthorByIDBatch(batch, adamsID)
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close batch results")
authors, err := q.FindAuthorByIDScan(results)
require.NoError(t, err)
assert.Equal(t, FindAuthorByIDRow{
AuthorID: adamsID,
FirstName: "john",
LastName: "adams",
Suffix: nil,
}, authors)
})
}

func TestNewQuerier_FindAuthors(t *testing.T) {
Expand Down Expand Up @@ -94,20 +78,6 @@ func TestNewQuerier_FindAuthors(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []FindAuthorsRow{}, authors)
})

t.Run("FindAuthorsBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.FindAuthorsBatch(batch, "george")
results := conn.SendBatch(context.Background(), batch)
authors, err := q.FindAuthorsScan(results)
defer errs.CaptureT(t, results.Close, "close batch results")
require.NoError(t, err)
want := []FindAuthorsRow{
{AuthorID: washingtonID, FirstName: "george", LastName: "washington", Suffix: nil},
{AuthorID: carverID, FirstName: "george", LastName: "carver", Suffix: nil},
}
assert.Equal(t, want, authors)
})
}

func TestNewQuerier_FindAuthors_PrepareAllQueries(t *testing.T) {
Expand Down Expand Up @@ -150,16 +120,6 @@ func TestNewQuerier_FindFirstNames(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []*string{ptrs.String("george"), ptrs.String("john")}, firstNames)
})

t.Run("FindFirstNamesBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.FindFirstNamesBatch(batch, adamsID)
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close batch results")
firstNames, err := q.FindFirstNamesScan(results)
require.NoError(t, err)
assert.Equal(t, []*string{ptrs.String("george"), ptrs.String("john")}, firstNames)
})
}

func TestNewQuerier_InsertAuthorSuffix(t *testing.T) {
Expand All @@ -183,26 +143,6 @@ func TestNewQuerier_InsertAuthorSuffix(t *testing.T) {
}
assert.Equal(t, want, author)
})

t.Run("InsertAuthorSuffixBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.InsertAuthorSuffixBatch(batch, InsertAuthorSuffixParams{
FirstName: "ulysses",
LastName: "grant",
})
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close batch results")
author, err := q.InsertAuthorSuffixScan(results)
require.NoError(t, err)
empty := ""
want := InsertAuthorSuffixRow{
AuthorID: author.AuthorID,
FirstName: "ulysses",
LastName: "grant",
Suffix: &empty, // TODO: should be nil, https://github.com/jschaf/pggen/issues/21
}
assert.Equal(t, want, author)
})
}

func TestNewQuerier_DeleteAuthorsByFirstName(t *testing.T) {
Expand All @@ -225,30 +165,6 @@ func TestNewQuerier_DeleteAuthorsByFirstName(t *testing.T) {
})
}

func TestNewQuerier_DeleteAuthorsByFirstNameBatch(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
insertAuthor(t, q, "john", "adams")
insertAuthor(t, q, "george", "washington")
insertAuthor(t, q, "george", "carver")

t.Run("DeleteAuthorsByFirstNameBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.DeleteAuthorsByFirstNameBatch(batch, "george")
results := conn.SendBatch(context.Background(), batch)
tag, err := q.DeleteAuthorsByFirstNameScan(results)
require.NoError(t, err)
assert.Truef(t, tag.Delete(), "expected delete tag; got %s", tag.String())
assert.Equal(t, int64(2), tag.RowsAffected())
require.NoError(t, results.Close())

authors, err := q.FindAuthors(context.Background(), "george")
require.NoError(t, err)
assert.Empty(t, authors, "no authors should remain with first name of george")
})
}

func TestNewQuerier_DeleteAuthorsByFullName(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
Expand Down Expand Up @@ -285,46 +201,6 @@ func TestNewQuerier_DeleteAuthorsByFullName(t *testing.T) {
})
}

func TestNewQuerier_DeleteAuthorsByFullNameBatch(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
washingtonID := insertAuthor(t, q, "george", "washington")
_, err := q.InsertAuthorSuffix(context.Background(), InsertAuthorSuffixParams{
FirstName: "george",
LastName: "washington",
Suffix: "Jr.",
})
require.NoError(t, err)

t.Run("DeleteAuthorsByFullNameBatch", func(t *testing.T) {
batch := &pgx.Batch{}
q.DeleteAuthorsByFullNameBatch(batch, DeleteAuthorsByFullNameParams{
FirstName: "george",
LastName: "washington",
Suffix: "Jr.",
})
results := conn.SendBatch(context.Background(), batch)
tag, err := q.DeleteAuthorsByFullNameScan(results)
require.NoError(t, err)
assert.Truef(t, tag.Delete(), "expected delete tag; got %s", tag.String())
assert.Equal(t, int64(1), tag.RowsAffected())
require.NoError(t, results.Close())

authors, err := q.FindAuthors(context.Background(), "george")
require.NoError(t, err)
want := []FindAuthorsRow{
{
AuthorID: washingtonID,
FirstName: "george",
LastName: "washington",
Suffix: nil,
},
}
assert.Equal(t, want, authors, "only one author with first name george should remain")
})
}

func TestNewQuerier_StringAggFirstName(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
Expand All @@ -343,31 +219,11 @@ func TestNewQuerier_StringAggFirstName(t *testing.T) {
require.Nil(t, firstNames)
})

t.Run("StringAggFirstNameBatch - null", func(t *testing.T) {
batch := &pgx.Batch{}
q.StringAggFirstNameBatch(batch, 999)
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close results")
firstNames, err := q.StringAggFirstNameScan(results)
require.NoError(t, err)
require.Nil(t, firstNames)
})

t.Run("StringAggFirstName - one", func(t *testing.T) {
firstNames, err := q.StringAggFirstName(context.Background(), washingtonID)
require.NoError(t, err)
assert.Equal(t, "george", *firstNames)
})

t.Run("StringAggFirstNameBatch - one", func(t *testing.T) {
batch := &pgx.Batch{}
q.StringAggFirstNameBatch(batch, washingtonID)
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close results")
firstNames, err := q.StringAggFirstNameScan(results)
require.NoError(t, err)
assert.Equal(t, "george", *firstNames)
})
}

func TestNewQuerier_ArrayAggFirstName(t *testing.T) {
Expand All @@ -388,31 +244,11 @@ func TestNewQuerier_ArrayAggFirstName(t *testing.T) {
require.Nil(t, firstNames)
})

t.Run("ArrayAggFirstNameBatch - null", func(t *testing.T) {
batch := &pgx.Batch{}
q.ArrayAggFirstNameBatch(batch, 999)
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close results")
firstNames, err := q.ArrayAggFirstNameScan(results)
require.NoError(t, err)
require.Nil(t, firstNames)
})

t.Run("ArrayAggFirstName - one", func(t *testing.T) {
firstNames, err := q.ArrayAggFirstName(context.Background(), washingtonID)
require.NoError(t, err)
assert.Equal(t, []string{"george"}, firstNames)
})

t.Run("ArrayAggFirstNameBatch - one", func(t *testing.T) {
batch := &pgx.Batch{}
q.ArrayAggFirstNameBatch(batch, washingtonID)
results := conn.SendBatch(context.Background(), batch)
defer errs.CaptureT(t, results.Close, "close results")
firstNames, err := q.ArrayAggFirstNameScan(results)
require.NoError(t, err)
assert.Equal(t, []string{"george"}, firstNames)
})
}

func insertAuthor(t *testing.T, q *DBQuerier, first, last string) int32 {
Expand Down
Loading

0 comments on commit f37d9a6

Please sign in to comment.