Skip to content

Commit 6d48386

Browse files
authored
Handle scan and close errors in SchemaHistory (#482)
1 parent 929ea27 commit 6d48386

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

pkg/migrations/op_common_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313

1414
"github.com/lib/pq"
15+
"github.com/stretchr/testify/assert"
1516

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

742743
res = append(res, row)
743744
}
745+
assert.NoError(t, q.Err())
744746

745747
return res
746748
}

pkg/roll/execute_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ func MustSelect(t *testing.T, db *sql.DB, schema, version, table string) []map[s
903903

904904
res = append(res, row)
905905
}
906+
assert.NoError(t, q.Err())
906907

907908
return res
908909
}

pkg/state/history.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/lib/pq"
14+
1415
"github.com/xataio/pgroll/pkg/migrations"
1516
)
1617

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

36-
defer rows.Close()
37-
3837
var entries []Migration
3938
for rows.Next() {
4039
var name, rawMigration string
4140
var createdAt time.Time
4241

43-
rows.Scan(&name, &rawMigration, &createdAt)
44-
if err != nil {
45-
return nil, err
42+
if err := rows.Scan(&name, &rawMigration, &createdAt); err != nil {
43+
return nil, fmt.Errorf("row scan: %w", err)
4644
}
4745

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

58+
if err := rows.Err(); err != nil {
59+
return nil, fmt.Errorf("iterating rows: %w", err)
60+
}
61+
6062
return entries, nil
6163
}
6264

pkg/state/state_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ func TestInferredMigration(t *testing.T) {
240240
if err != nil {
241241
t.Fatal(err)
242242
}
243-
defer rows.Close()
244243

245244
var gotMigrations []migrations.Migration
246245
for rows.Next() {
@@ -254,6 +253,7 @@ func TestInferredMigration(t *testing.T) {
254253
}
255254
gotMigrations = append(gotMigrations, gotMigration)
256255
}
256+
assert.NoError(t, rows.Err())
257257

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

@@ -296,7 +296,6 @@ func TestInferredMigrationsInTransactionHaveDifferentTimestamps(t *testing.T) {
296296
if err != nil {
297297
t.Fatal(err)
298298
}
299-
defer rows.Close()
300299

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

314313
migrations = append(migrations, migration)
315314
}
315+
assert.NoError(t, rows.Err())
316316

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

0 commit comments

Comments
 (0)