Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
More rows.Close() and rows.Err() (#3262)
Browse files Browse the repository at this point in the history
Looks like we missed some `rows.Close()`

Even though `rows.Err()` is mostly not necessary, we should be more
consistent in the DB layer.

[skip ci]
  • Loading branch information
S7evinK authored Nov 9, 2023
1 parent ee73a90 commit 699f5ca
Show file tree
Hide file tree
Showing 50 changed files with 101 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (s *notaryServerKeysMetadataStatements) SelectKeys(ctx context.Context, txn
}
results = append(results, sk)
}
return results, nil
return results, rows.Err()
}

func (s *notaryServerKeysMetadataStatements) DeleteOldJSONResponses(ctx context.Context, txn *sql.Tx) error {
Expand Down
2 changes: 1 addition & 1 deletion federationapi/storage/postgres/queue_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ func (s *queueJSONStatements) SelectQueueJSON(
}
blobs[nid] = blob
}
return blobs, err
return blobs, rows.Err()
}
2 changes: 1 addition & 1 deletion federationapi/storage/postgres/relay_servers_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *relayServersStatements) SelectRelayServers(
}
result = append(result, spec.ServerName(relayServer))
}
return result, nil
return result, rows.Err()
}

func (s *relayServersStatements) DeleteRelayServers(
Expand Down
2 changes: 1 addition & 1 deletion federationapi/storage/sqlite3/joined_hosts_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,5 @@ func joinedHostsFromStmt(
})
}

return result, nil
return result, rows.Err()
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *notaryServerKeysMetadataStatements) SelectKeys(ctx context.Context, txn
}
results = append(results, sk)
}
return results, nil
return results, rows.Err()
}

func (s *notaryServerKeysMetadataStatements) DeleteOldJSONResponses(ctx context.Context, txn *sql.Tx) error {
Expand Down
2 changes: 1 addition & 1 deletion federationapi/storage/sqlite3/queue_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ func (s *queueJSONStatements) SelectQueueJSON(
}
blobs[nid] = blob
}
return blobs, err
return blobs, rows.Err()
}
2 changes: 1 addition & 1 deletion federationapi/storage/sqlite3/relay_servers_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (s *relayServersStatements) SelectRelayServers(
}
result = append(result, spec.ServerName(relayServer))
}
return result, nil
return result, rows.Err()
}

func (s *relayServersStatements) DeleteRelayServers(
Expand Down
2 changes: 1 addition & 1 deletion relayapi/storage/postgres/relay_queue_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ func (s *relayQueueJSONStatements) SelectQueueJSON(
}
blobs[nid] = blob
}
return blobs, err
return blobs, rows.Err()
}
2 changes: 1 addition & 1 deletion relayapi/storage/sqlite3/relay_queue_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ func (s *relayQueueJSONStatements) SelectQueueJSON(
}
blobs[nid] = blob
}
return blobs, err
return blobs, rows.Err()
}
3 changes: 2 additions & 1 deletion roomserver/storage/postgres/events_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func (s *eventStatements) BulkSelectSnapshotsFromEventIDs(
if err != nil {
return nil, err
}
defer internal.CloseAndLogIfError(ctx, rows, "BulkSelectSnapshotsFromEventIDs: rows.close() failed")

var eventID string
var stateNID types.StateSnapshotNID
Expand Down Expand Up @@ -563,7 +564,7 @@ func (s *eventStatements) SelectRoomNIDsForEventNIDs(
}
result[eventNID] = roomNID
}
return result, nil
return result, rows.Err()
}

func eventNIDsAsArray(eventNIDs []types.EventNID) pq.Int64Array {
Expand Down
2 changes: 1 addition & 1 deletion roomserver/storage/postgres/membership_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (s *membershipStatements) SelectRoomsWithMembership(
}
roomNIDs = append(roomNIDs, roomNID)
}
return roomNIDs, nil
return roomNIDs, rows.Err()
}

func (s *membershipStatements) SelectJoinedUsersSetForRooms(
Expand Down
8 changes: 4 additions & 4 deletions roomserver/storage/postgres/rooms_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *roomStatements) SelectRoomIDsWithEvents(ctx context.Context, txn *sql.T
}
roomIDs = append(roomIDs, roomID)
}
return roomIDs, nil
return roomIDs, rows.Err()
}
func (s *roomStatements) InsertRoomNID(
ctx context.Context, txn *sql.Tx,
Expand Down Expand Up @@ -255,7 +255,7 @@ func (s *roomStatements) SelectRoomVersionsForRoomNIDs(
}
result[roomNID] = roomVersion
}
return result, nil
return result, rows.Err()
}

func (s *roomStatements) BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID) ([]string, error) {
Expand All @@ -277,7 +277,7 @@ func (s *roomStatements) BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roo
}
roomIDs = append(roomIDs, roomID)
}
return roomIDs, nil
return roomIDs, rows.Err()
}

func (s *roomStatements) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, roomIDs []string) ([]types.RoomNID, error) {
Expand All @@ -299,7 +299,7 @@ func (s *roomStatements) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, ro
}
roomNIDs = append(roomNIDs, roomNID)
}
return roomNIDs, nil
return roomNIDs, rows.Err()
}

func roomNIDsAsArray(roomNIDs []types.RoomNID) pq.Int64Array {
Expand Down
3 changes: 2 additions & 1 deletion roomserver/storage/postgres/user_room_keys_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (s *userRoomKeysStatements) SelectAllPublicKeysForUser(ctx context.Context,
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
defer internal.CloseAndLogIfError(ctx, rows, "SelectAllPublicKeysForUser: failed to close rows")

resultMap := make(map[types.RoomNID]ed25519.PublicKey)

Expand All @@ -173,5 +174,5 @@ func (s *userRoomKeysStatements) SelectAllPublicKeysForUser(ctx context.Context,
}
resultMap[roomNID] = pubkey
}
return resultMap, err
return resultMap, rows.Err()
}
2 changes: 1 addition & 1 deletion roomserver/storage/sqlite3/event_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ func (s *eventJSONStatements) BulkSelectEventJSON(
}
result.EventNID = types.EventNID(eventNID)
}
return results[:i], nil
return results[:i], rows.Err()
}
4 changes: 2 additions & 2 deletions roomserver/storage/sqlite3/event_state_keys_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *eventStateKeyStatements) BulkSelectEventStateKeyNID(
}
result[stateKey] = types.EventStateKeyNID(stateKeyNID)
}
return result, nil
return result, rows.Err()
}

func (s *eventStateKeyStatements) BulkSelectEventStateKey(
Expand Down Expand Up @@ -167,5 +167,5 @@ func (s *eventStateKeyStatements) BulkSelectEventStateKey(
}
result[types.EventStateKeyNID(stateKeyNID)] = stateKey
}
return result, nil
return result, rows.Err()
}
2 changes: 1 addition & 1 deletion roomserver/storage/sqlite3/event_types_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ func (s *eventTypeStatements) BulkSelectEventTypeNID(
}
result[eventType] = types.EventTypeNID(eventTypeNID)
}
return result, nil
return result, rows.Err()
}
18 changes: 15 additions & 3 deletions roomserver/storage/sqlite3/events_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ func (s *eventStatements) BulkSelectStateEventByID(
}
results = append(results, result)
}
if err = rows.Err(); err != nil {
return nil, err
}
if !excludeRejected && i != len(eventIDs) {
// If there are fewer rows returned than IDs then we were asked to lookup event IDs we don't have.
// We don't know which ones were missing because we don't return the string IDs in the query.
Expand Down Expand Up @@ -377,7 +380,7 @@ func (s *eventStatements) BulkSelectStateEventByNID(
return nil, err
}
}
return results[:i], err
return results[:i], rows.Err()
}

// bulkSelectStateAtEventByID lookups the state at a list of events by event ID.
Expand Down Expand Up @@ -425,6 +428,9 @@ func (s *eventStatements) BulkSelectStateAtEventByID(
)
}
}
if err = rows.Err(); err != nil {
return nil, err
}
if i != len(eventIDs) {
return nil, types.MissingEventError(
fmt.Sprintf("storage: event IDs missing from the database (%d != %d)", i, len(eventIDs)),
Expand Down Expand Up @@ -507,6 +513,9 @@ func (s *eventStatements) BulkSelectStateAtEventAndReference(
result.BeforeStateSnapshotNID = types.StateSnapshotNID(stateSnapshotNID)
result.EventID = eventID
}
if err = rows.Err(); err != nil {
return nil, err
}
if i != len(eventNIDs) {
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
}
Expand Down Expand Up @@ -544,6 +553,9 @@ func (s *eventStatements) BulkSelectEventID(ctx context.Context, txn *sql.Tx, ev
}
results[types.EventNID(eventNID)] = eventID
}
if err = rows.Err(); err != nil {
return nil, err
}
if i != len(eventNIDs) {
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
}
Expand Down Expand Up @@ -602,7 +614,7 @@ func (s *eventStatements) bulkSelectEventNID(ctx context.Context, txn *sql.Tx, e
RoomNID: types.RoomNID(roomNID),
}
}
return results, nil
return results, rows.Err()
}

func (s *eventStatements) SelectMaxEventDepth(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (int64, error) {
Expand Down Expand Up @@ -652,7 +664,7 @@ func (s *eventStatements) SelectRoomNIDsForEventNIDs(
}
result[eventNID] = roomNID
}
return result, nil
return result, rows.Err()
}

func eventNIDsAsArray(eventNIDs []types.EventNID) string {
Expand Down
5 changes: 4 additions & 1 deletion roomserver/storage/sqlite3/invite_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func (s *inviteStatements) UpdateInviteRetired(
}
eventIDs = append(eventIDs, inviteEventID)
}
if err = rows.Err(); err != nil {
return
}
// now retire the invites
stmt = sqlutil.TxStmt(txn, s.updateInviteRetiredStmt)
_, err = stmt.ExecContext(ctx, roomNID, targetUserNID)
Expand Down Expand Up @@ -157,5 +160,5 @@ func (s *inviteStatements) SelectInviteActiveForUserInRoom(
result = append(result, types.EventStateKeyNID(senderUserNID))
eventIDs = append(eventIDs, eventID)
}
return result, eventIDs, eventJSON, nil
return result, eventIDs, eventJSON, rows.Err()
}
4 changes: 3 additions & 1 deletion roomserver/storage/sqlite3/membership_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (s *membershipStatements) SelectMembershipsFromRoom(
}
eventNIDs = append(eventNIDs, eNID)
}
err = rows.Err()
return
}

Expand Down Expand Up @@ -277,6 +278,7 @@ func (s *membershipStatements) SelectMembershipsFromRoomAndMembership(
}
eventNIDs = append(eventNIDs, eNID)
}
err = rows.Err()
return
}

Expand Down Expand Up @@ -313,7 +315,7 @@ func (s *membershipStatements) SelectRoomsWithMembership(
}
roomNIDs = append(roomNIDs, roomNID)
}
return roomNIDs, nil
return roomNIDs, rows.Err()
}

func (s *membershipStatements) SelectJoinedUsersSetForRooms(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID, userNIDs []types.EventStateKeyNID, localOnly bool) (map[types.EventStateKeyNID]int, error) {
Expand Down
2 changes: 1 addition & 1 deletion roomserver/storage/sqlite3/room_aliases_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *roomAliasesStatements) SelectAliasesFromRoomID(

aliases = append(aliases, alias)
}

err = rows.Err()
return
}

Expand Down
8 changes: 4 additions & 4 deletions roomserver/storage/sqlite3/rooms_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *roomStatements) SelectRoomIDsWithEvents(ctx context.Context, txn *sql.T
}
roomIDs = append(roomIDs, roomID)
}
return roomIDs, nil
return roomIDs, rows.Err()
}

func (s *roomStatements) SelectRoomInfo(ctx context.Context, txn *sql.Tx, roomID string) (*types.RoomInfo, error) {
Expand Down Expand Up @@ -265,7 +265,7 @@ func (s *roomStatements) SelectRoomVersionsForRoomNIDs(
}
result[roomNID] = roomVersion
}
return result, nil
return result, rows.Err()
}

func (s *roomStatements) BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID) ([]string, error) {
Expand Down Expand Up @@ -293,7 +293,7 @@ func (s *roomStatements) BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roo
}
roomIDs = append(roomIDs, roomID)
}
return roomIDs, nil
return roomIDs, rows.Err()
}

func (s *roomStatements) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, roomIDs []string) ([]types.RoomNID, error) {
Expand Down Expand Up @@ -321,5 +321,5 @@ func (s *roomStatements) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, ro
}
roomNIDs = append(roomNIDs, roomNID)
}
return roomNIDs, nil
return roomNIDs, rows.Err()
}
7 changes: 5 additions & 2 deletions roomserver/storage/sqlite3/state_snapshot_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,16 @@ func (s *stateSnapshotStatements) BulkSelectStateBlockNIDs(
var stateBlockNIDsJSON string
for ; rows.Next(); i++ {
result := &results[i]
if err := rows.Scan(&result.StateSnapshotNID, &stateBlockNIDsJSON); err != nil {
if err = rows.Scan(&result.StateSnapshotNID, &stateBlockNIDsJSON); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(stateBlockNIDsJSON), &result.StateBlockNIDs); err != nil {
if err = json.Unmarshal([]byte(stateBlockNIDsJSON), &result.StateBlockNIDs); err != nil {
return nil, err
}
}
if err = rows.Err(); err != nil {
return nil, err
}
if i != len(stateNIDs) {
return nil, types.MissingStateError(fmt.Sprintf("storage: state NIDs missing from the database (%d != %d)", i, len(stateNIDs)))
}
Expand Down
3 changes: 2 additions & 1 deletion roomserver/storage/sqlite3/user_room_keys_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func (s *userRoomKeysStatements) SelectAllPublicKeysForUser(ctx context.Context,
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
defer internal.CloseAndLogIfError(ctx, rows, "SelectAllPublicKeysForUser: failed to close rows")

resultMap := make(map[types.RoomNID]ed25519.PublicKey)

Expand All @@ -188,5 +189,5 @@ func (s *userRoomKeysStatements) SelectAllPublicKeysForUser(ctx context.Context,
}
resultMap[roomNID] = pubkey
}
return resultMap, err
return resultMap, rows.Err()
}
2 changes: 1 addition & 1 deletion setup/mscs/msc2836/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (p *DB) ChildrenForParent(ctx context.Context, eventID, relType string, rec
}
children = append(children, evInfo)
}
return children, nil
return children, rows.Err()
}

func (p *DB) ParentForChild(ctx context.Context, eventID, relType string) (*eventInfo, error) {
Expand Down
2 changes: 1 addition & 1 deletion syncapi/storage/postgres/current_room_state_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func currentRoomStateRowsToStreamEvents(rows *sql.Rows) ([]types.StreamEvent, er
})
}

return events, nil
return events, rows.Err()
}

func rowsToEvents(rows *sql.Rows) ([]*rstypes.HeaderedEvent, error) {
Expand Down
2 changes: 2 additions & 0 deletions syncapi/storage/postgres/memberships_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"database/sql"
"fmt"

"github.com/matrix-org/dendrite/internal"
"github.com/matrix-org/dendrite/internal/sqlutil"
rstypes "github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/dendrite/syncapi/storage/tables"
Expand Down Expand Up @@ -160,6 +161,7 @@ func (s *membershipsStatements) SelectMemberships(
if err != nil {
return
}
defer internal.CloseAndLogIfError(ctx, rows, "SelectMemberships: failed to close rows")
var (
eventID string
)
Expand Down
Loading

0 comments on commit 699f5ca

Please sign in to comment.