Do not store 'null' in the database for empty JSON arrays

This can cause issues, though it should be noted that the majority
of the time this will marshal/unmarshal just fine, see
https://play.golang.org/p/Doe2NZUgv7Q
This commit is contained in:
Kegan Dougal 2021-08-04 16:08:24 +01:00
parent da101469fa
commit 17a7896614
3 changed files with 7 additions and 1 deletions

View file

@ -571,6 +571,9 @@ func (s *eventStatements) SelectRoomNIDsForEventNIDs(
}
func eventNIDsAsArray(eventNIDs []types.EventNID) string {
if eventNIDs == nil {
eventNIDs = []types.EventNID{} // don't store 'null' in the DB
}
b, _ := json.Marshal(eventNIDs)
return string(b)
}

View file

@ -86,7 +86,7 @@ func (s *stateBlockStatements) BulkInsertStateData(
entries types.StateEntries,
) (id types.StateBlockNID, err error) {
entries = entries[:util.SortAndUnique(entries)]
var nids types.EventNIDs
nids := types.EventNIDs{} // zero slice to not store 'null' in the DB
for _, e := range entries {
nids = append(nids, e.EventNID)
}

View file

@ -87,6 +87,9 @@ func prepareStateSnapshotTable(db *sql.DB) (tables.StateSnapshot, error) {
func (s *stateSnapshotStatements) InsertState(
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, stateBlockNIDs types.StateBlockNIDs,
) (stateNID types.StateSnapshotNID, err error) {
if stateBlockNIDs == nil {
stateBlockNIDs = []types.StateBlockNID{} // zero slice to not store 'null' in the DB
}
stateBlockNIDs = stateBlockNIDs[:util.SortAndUnique(stateBlockNIDs)]
stateBlockNIDsJSON, err := json.Marshal(stateBlockNIDs)
if err != nil {