Skip to content

Commit

Permalink
Handle scan and close errors in SchemaHistory (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade authored Nov 22, 2024
1 parent 929ea27 commit 6d48386
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/migrations/op_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/lib/pq"
"github.com/stretchr/testify/assert"

"github.com/xataio/pgroll/internal/testutils"
"github.com/xataio/pgroll/pkg/migrations"
Expand Down Expand Up @@ -741,6 +742,7 @@ func MustSelect(t *testing.T, db *sql.DB, schema, version, table string) []map[s

res = append(res, row)
}
assert.NoError(t, q.Err())

return res
}
Expand Down
1 change: 1 addition & 0 deletions pkg/roll/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ func MustSelect(t *testing.T, db *sql.DB, schema, version, table string) []map[s

res = append(res, row)
}
assert.NoError(t, q.Err())

return res
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/lib/pq"

"github.com/xataio/pgroll/pkg/migrations"
)

Expand All @@ -33,16 +34,13 @@ func (s *State) SchemaHistory(ctx context.Context, schema string) ([]Migration,
return nil, err
}

defer rows.Close()

var entries []Migration
for rows.Next() {
var name, rawMigration string
var createdAt time.Time

rows.Scan(&name, &rawMigration, &createdAt)
if err != nil {
return nil, err
if err := rows.Scan(&name, &rawMigration, &createdAt); err != nil {
return nil, fmt.Errorf("row scan: %w", err)
}

var mig migrations.Migration
Expand All @@ -57,6 +55,10 @@ func (s *State) SchemaHistory(ctx context.Context, schema string) ([]Migration,
})
}

if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating rows: %w", err)
}

return entries, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ func TestInferredMigration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer rows.Close()

var gotMigrations []migrations.Migration
for rows.Next() {
Expand All @@ -254,6 +253,7 @@ func TestInferredMigration(t *testing.T) {
}
gotMigrations = append(gotMigrations, gotMigration)
}
assert.NoError(t, rows.Err())

assert.Equal(t, len(tt.wantMigrations), len(gotMigrations), "unexpected number of migrations")

Expand Down Expand Up @@ -296,7 +296,6 @@ func TestInferredMigrationsInTransactionHaveDifferentTimestamps(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer rows.Close()

type m struct {
name string
Expand All @@ -313,6 +312,7 @@ func TestInferredMigrationsInTransactionHaveDifferentTimestamps(t *testing.T) {

migrations = append(migrations, migration)
}
assert.NoError(t, rows.Err())

// Ensure that the two inferred migrations have different timestamps
assert.Equal(t, 2, len(migrations), "unexpected number of migrations")
Expand Down

0 comments on commit 6d48386

Please sign in to comment.