- Ensure the correct ID is returned from the Max statements as the NullInt64 was not working as expected (#25)

- Check before creating the nextAccountDataID

Co-authored-by: alexf@example.com <alexf@example.com>
This commit is contained in:
alexfca 2021-11-07 16:47:55 +11:00 committed by GitHub
parent 60e11f88b8
commit 6d27770a69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 31 deletions

View file

@ -130,11 +130,6 @@ func (s *accountDataStatements) InsertAccountData(
// " ON CONFLICT (user_id, room_id, type) DO UPDATE" + // " ON CONFLICT (user_id, room_id, type) DO UPDATE" +
// " SET id = $5" // " SET id = $5"
pos, err = s.streamIDStatements.nextAccountDataID(ctx, txn)
if err != nil {
return
}
// UNIQUE (user_id, room_id, type) // UNIQUE (user_id, room_id, type)
// roomId can be NULL // roomId can be NULL
docId := fmt.Sprintf("%s,%s,%s", userID, cosmosdbapi.EnsureIdPart(roomID), dataType) docId := fmt.Sprintf("%s,%s,%s", userID, cosmosdbapi.EnsureIdPart(roomID), dataType)
@ -143,8 +138,12 @@ func (s *accountDataStatements) InsertAccountData(
dbData, _ := getAccountDataType(s, ctx, s.getPartitionKey(), cosmosDocId) dbData, _ := getAccountDataType(s, ctx, s.getPartitionKey(), cosmosDocId)
if dbData != nil { if dbData != nil {
dbData.SetUpdateTime() dbData.SetUpdateTime()
dbData.AccountDataType.ID = int64(pos)
} else { } else {
pos, err = s.streamIDStatements.nextAccountDataID(ctx, txn)
if err != nil {
return
}
data := accountDataTypeCosmos{ data := accountDataTypeCosmos{
ID: int64(pos), ID: int64(pos),
UserID: userID, UserID: userID,
@ -243,7 +242,7 @@ func (s *accountDataStatements) SelectMaxAccountDataID(
// "SELECT MAX(id) FROM syncapi_account_data_type" // "SELECT MAX(id) FROM syncapi_account_data_type"
var nullableID sql.NullInt64 // var nullableID sql.NullInt64
// err = sqlutil.TxStmt(txn, s.selectMaxAccountDataIDStmt).QueryRowContext(ctx).Scan(&nullableID) // err = sqlutil.TxStmt(txn, s.selectMaxAccountDataIDStmt).QueryRowContext(ctx).Scan(&nullableID)
params := map[string]interface{}{ params := map[string]interface{}{
"@x1": s.getCollectionName(), "@x1": s.getCollectionName(),
@ -258,11 +257,12 @@ func (s *accountDataStatements) SelectMaxAccountDataID(
s.selectMaxAccountDataIDStmt, params, &rows) s.selectMaxAccountDataIDStmt, params, &rows)
if err != cosmosdbutil.ErrNoRows && len(rows) == 1 { if err != cosmosdbutil.ErrNoRows && len(rows) == 1 {
nullableID.Int64 = rows[0].Number id = rows[0].Number
} }
if nullableID.Valid { // if nullableID.Valid {
id = nullableID.Int64 // id = nullableID.Int64
} // }
return return
} }

View file

@ -287,7 +287,7 @@ func (s *inviteEventsStatements) SelectInviteEventsInRange(
func (s *inviteEventsStatements) SelectMaxInviteID( func (s *inviteEventsStatements) SelectMaxInviteID(
ctx context.Context, txn *sql.Tx, ctx context.Context, txn *sql.Tx,
) (id int64, err error) { ) (id int64, err error) {
var nullableID sql.NullInt64 // var nullableID sql.NullInt64
// "SELECT MAX(id) FROM syncapi_invite_events" // "SELECT MAX(id) FROM syncapi_invite_events"
@ -306,11 +306,11 @@ func (s *inviteEventsStatements) SelectMaxInviteID(
s.selectMaxInviteIDStmt, params, &rows) s.selectMaxInviteIDStmt, params, &rows)
if len(rows) > 0 { if len(rows) > 0 {
nullableID.Int64 = rows[0].Max id = rows[0].Max
} }
if nullableID.Valid { // if nullableID.Valid {
id = nullableID.Int64 // id = nullableID.Int64
} // }
return return
} }

View file

@ -377,7 +377,7 @@ func (s *peekStatements) SelectMaxPeekID(
// "SELECT MAX(id) FROM syncapi_peeks" // "SELECT MAX(id) FROM syncapi_peeks"
// stmt := sqlutil.TxStmt(txn, s.selectMaxPeekIDStmt) // stmt := sqlutil.TxStmt(txn, s.selectMaxPeekIDStmt)
var nullableID sql.NullInt64 // var nullableID sql.NullInt64
params := map[string]interface{}{ params := map[string]interface{}{
"@x1": s.getCollectionName(), "@x1": s.getCollectionName(),
} }
@ -392,11 +392,11 @@ func (s *peekStatements) SelectMaxPeekID(
// err = stmt.QueryRowContext(ctx).Scan(&nullableID) // err = stmt.QueryRowContext(ctx).Scan(&nullableID)
if rows != nil { if rows != nil {
nullableID.Int64 = rows[0].Max id = rows[0].Max
} }
if nullableID.Valid { // if nullableID.Valid {
id = nullableID.Int64 // id = nullableID.Int64
} // }
return return
} }

View file

@ -203,7 +203,7 @@ func (r *receiptStatements) SelectRoomReceiptsAfter(ctx context.Context, roomIDs
func (s *receiptStatements) SelectMaxReceiptID( func (s *receiptStatements) SelectMaxReceiptID(
ctx context.Context, txn *sql.Tx, ctx context.Context, txn *sql.Tx,
) (id int64, err error) { ) (id int64, err error) {
var nullableID sql.NullInt64 // var nullableID sql.NullInt64
// "SELECT MAX(id) FROM syncapi_receipts" // "SELECT MAX(id) FROM syncapi_receipts"
@ -221,11 +221,11 @@ func (s *receiptStatements) SelectMaxReceiptID(
// stmt := sqlutil.TxStmt(txn, s.selectMaxReceiptID) // stmt := sqlutil.TxStmt(txn, s.selectMaxReceiptID)
if rows != nil { if rows != nil {
nullableID.Int64 = rows[0].Max id = rows[0].Max
} }
// err = stmt.QueryRowContext(ctx).Scan(&nullableID) // err = stmt.QueryRowContext(ctx).Scan(&nullableID)
if nullableID.Valid { // if nullableID.Valid {
id = nullableID.Int64 // id = nullableID.Int64
} // }
return return
} }

View file

@ -268,7 +268,7 @@ func (s *sendToDeviceStatements) DeleteSendToDeviceMessages(
func (s *sendToDeviceStatements) SelectMaxSendToDeviceMessageID( func (s *sendToDeviceStatements) SelectMaxSendToDeviceMessageID(
ctx context.Context, txn *sql.Tx, ctx context.Context, txn *sql.Tx,
) (id int64, err error) { ) (id int64, err error) {
var nullableID sql.NullInt64 // var nullableID sql.NullInt64
// "SELECT MAX(id) FROM syncapi_send_to_device" // "SELECT MAX(id) FROM syncapi_send_to_device"
params := map[string]interface{}{ params := map[string]interface{}{
@ -286,11 +286,11 @@ func (s *sendToDeviceStatements) SelectMaxSendToDeviceMessageID(
// err = stmt.QueryRowContext(ctx).Scan(&nullableID) // err = stmt.QueryRowContext(ctx).Scan(&nullableID)
if rows != nil { if rows != nil {
nullableID.Int64 = rows[0].Max id = rows[0].Max
} }
if nullableID.Valid { // if nullableID.Valid {
id = nullableID.Int64 // id = nullableID.Int64
} // }
return return
} }