mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-30 04:52:46 +00:00
Update room version descriptors, add error handling (#906)
* Update room version descriptors, add error handling * Fix database queries * Drop Get from version package * Fix database wrapping, add comments for version descriptions * Don't set default room_version value in SQL
This commit is contained in:
parent
dfd8b93d93
commit
452f393dd7
9 changed files with 121 additions and 95 deletions
|
@ -45,5 +45,5 @@ type Database interface {
|
|||
GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderUserID string) (membershipEventNID types.EventNID, stillInRoom bool, err error)
|
||||
GetMembershipEventNIDsForRoom(ctx context.Context, roomNID types.RoomNID, joinOnly bool) ([]types.EventNID, error)
|
||||
EventsFromIDs(ctx context.Context, eventIDs []string) ([]types.Event, error)
|
||||
GetRoomVersionForRoom(ctx context.Context, roomNID types.RoomNID) (int64, error)
|
||||
GetRoomVersionForRoom(ctx context.Context, roomNID types.RoomNID) (gomatrixserverlib.RoomVersion, error)
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
"github.com/lib/pq"
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
const roomsSchema = `
|
||||
|
@ -42,13 +43,13 @@ CREATE TABLE IF NOT EXISTS roomserver_rooms (
|
|||
state_snapshot_nid BIGINT NOT NULL DEFAULT 0,
|
||||
-- The version of the room, which will assist in determining the state resolution
|
||||
-- algorithm, event ID format, etc.
|
||||
room_version BIGINT NOT NULL DEFAULT 1
|
||||
room_version TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
// Same as insertEventTypeNIDSQL
|
||||
const insertRoomNIDSQL = "" +
|
||||
"INSERT INTO roomserver_rooms (room_id) VALUES ($1)" +
|
||||
"INSERT INTO roomserver_rooms (room_id, room_version) VALUES ($1, $2)" +
|
||||
" ON CONFLICT ON CONSTRAINT roomserver_room_id_unique" +
|
||||
" DO NOTHING RETURNING (room_nid)"
|
||||
|
||||
|
@ -92,11 +93,12 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
|||
}
|
||||
|
||||
func (s *roomStatements) insertRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomID string,
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (types.RoomNID, error) {
|
||||
var roomNID int64
|
||||
stmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
||||
err := stmt.QueryRowContext(ctx, roomID).Scan(&roomNID)
|
||||
err := stmt.QueryRowContext(ctx, roomID, roomVersion).Scan(&roomNID)
|
||||
return types.RoomNID(roomNID), err
|
||||
}
|
||||
|
||||
|
@ -165,8 +167,8 @@ func (s *roomStatements) updateLatestEventNIDs(
|
|||
|
||||
func (s *roomStatements) selectRoomVersionForRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||
) (int64, error) {
|
||||
var roomVersion int64
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
var roomVersion gomatrixserverlib.RoomVersion
|
||||
stmt := common.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
||||
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
||||
return roomVersion, err
|
||||
|
|
|
@ -68,7 +68,8 @@ func (d *Database) StoreEvent(
|
|||
}
|
||||
}
|
||||
|
||||
if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID()); err != nil {
|
||||
// TODO: Room version here
|
||||
if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID(), "1"); err != nil {
|
||||
return 0, types.StateAtEvent{}, err
|
||||
}
|
||||
|
||||
|
@ -121,13 +122,14 @@ func (d *Database) StoreEvent(
|
|||
}
|
||||
|
||||
func (d *Database) assignRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomID string,
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (types.RoomNID, error) {
|
||||
// Check if we already have a numeric ID in the database.
|
||||
roomNID, err := d.statements.selectRoomNID(ctx, txn, roomID)
|
||||
if err == sql.ErrNoRows {
|
||||
// We don't have a numeric ID so insert one into the database.
|
||||
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID)
|
||||
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID, roomVersion)
|
||||
if err == sql.ErrNoRows {
|
||||
// We raced with another insert so run the select again.
|
||||
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||
|
@ -494,7 +496,8 @@ func (d *Database) MembershipUpdater(
|
|||
}
|
||||
}()
|
||||
|
||||
roomNID, err := d.assignRoomNID(ctx, txn, roomID)
|
||||
// TODO: Room version here
|
||||
roomNID, err := d.assignRoomNID(ctx, txn, roomID, "1")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -699,7 +702,7 @@ func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]type
|
|||
|
||||
func (d *Database) GetRoomVersionForRoom(
|
||||
ctx context.Context, roomNID types.RoomNID,
|
||||
) (int64, error) {
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
return d.statements.selectRoomVersionForRoomNID(
|
||||
ctx, nil, roomNID,
|
||||
)
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
const roomsSchema = `
|
||||
|
@ -31,13 +32,13 @@ const roomsSchema = `
|
|||
latest_event_nids TEXT NOT NULL DEFAULT '[]',
|
||||
last_event_sent_nid INTEGER NOT NULL DEFAULT 0,
|
||||
state_snapshot_nid INTEGER NOT NULL DEFAULT 0,
|
||||
room_version INTEGER NOT NULL DEFAULT 1
|
||||
room_version TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
// Same as insertEventTypeNIDSQL
|
||||
const insertRoomNIDSQL = `
|
||||
INSERT INTO roomserver_rooms (room_id) VALUES ($1)
|
||||
INSERT INTO roomserver_rooms (room_id, room_version) VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING;
|
||||
`
|
||||
|
||||
|
@ -81,11 +82,12 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
|||
}
|
||||
|
||||
func (s *roomStatements) insertRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomID string,
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (types.RoomNID, error) {
|
||||
var err error
|
||||
insertStmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
||||
if _, err = insertStmt.ExecContext(ctx, roomID); err == nil {
|
||||
if _, err = insertStmt.ExecContext(ctx, roomID, roomVersion); err == nil {
|
||||
return s.selectRoomNID(ctx, txn, roomID)
|
||||
} else {
|
||||
return types.RoomNID(0), err
|
||||
|
@ -157,8 +159,8 @@ func (s *roomStatements) updateLatestEventNIDs(
|
|||
|
||||
func (s *roomStatements) selectRoomVersionForRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||
) (int64, error) {
|
||||
var roomVersion int64
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
var roomVersion gomatrixserverlib.RoomVersion
|
||||
stmt := common.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
||||
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
||||
return roomVersion, err
|
||||
|
|
|
@ -90,7 +90,8 @@ func (d *Database) StoreEvent(
|
|||
}
|
||||
}
|
||||
|
||||
if roomNID, err = d.assignRoomNID(ctx, txn, event.RoomID()); err != nil {
|
||||
// TODO: Room version here
|
||||
if roomNID, err = d.assignRoomNID(ctx, txn, event.RoomID(), "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -150,13 +151,14 @@ func (d *Database) StoreEvent(
|
|||
}
|
||||
|
||||
func (d *Database) assignRoomNID(
|
||||
ctx context.Context, txn *sql.Tx, roomID string,
|
||||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (roomNID types.RoomNID, err error) {
|
||||
// Check if we already have a numeric ID in the database.
|
||||
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||
if err == sql.ErrNoRows {
|
||||
// We don't have a numeric ID so insert one into the database.
|
||||
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID)
|
||||
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID, roomVersion)
|
||||
if err == nil {
|
||||
// Now get the numeric ID back out of the database
|
||||
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||
|
@ -630,7 +632,8 @@ func (d *Database) MembershipUpdater(
|
|||
}
|
||||
}()
|
||||
|
||||
roomNID, err := d.assignRoomNID(ctx, txn, roomID)
|
||||
// TODO: Room version here
|
||||
roomNID, err := d.assignRoomNID(ctx, txn, roomID, "1")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -853,7 +856,7 @@ func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]type
|
|||
|
||||
func (d *Database) GetRoomVersionForRoom(
|
||||
ctx context.Context, roomNID types.RoomNID,
|
||||
) (int64, error) {
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
return d.statements.selectRoomVersionForRoomNID(
|
||||
ctx, nil, roomNID,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue