From 4c4de0dfc60bd8ed69b78c88d4bd1f980739ad8c Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 22 Aug 2022 12:05:38 +0100 Subject: [PATCH] Purge the room entry too --- roomserver/storage/postgres/rooms_table.go | 12 ++++++++++++ roomserver/storage/shared/storage.go | 3 +++ roomserver/storage/sqlite3/rooms_table.go | 6 ++++++ roomserver/storage/tables/interface.go | 1 + 4 files changed, 22 insertions(+) diff --git a/roomserver/storage/postgres/rooms_table.go b/roomserver/storage/postgres/rooms_table.go index 994399532..1227828ad 100644 --- a/roomserver/storage/postgres/rooms_table.go +++ b/roomserver/storage/postgres/rooms_table.go @@ -82,6 +82,9 @@ const bulkSelectRoomIDsSQL = "" + const bulkSelectRoomNIDsSQL = "" + "SELECT room_nid FROM roomserver_rooms WHERE room_id = ANY($1)" +const purgeRoomSQL = "" + + "DELETE FROM roomserver_rooms WHERE room_nid = $1" + type roomStatements struct { insertRoomNIDStmt *sql.Stmt selectRoomNIDStmt *sql.Stmt @@ -93,6 +96,7 @@ type roomStatements struct { selectRoomIDsStmt *sql.Stmt bulkSelectRoomIDsStmt *sql.Stmt bulkSelectRoomNIDsStmt *sql.Stmt + purgeRoomStmt *sql.Stmt } func CreateRoomsTable(db *sql.DB) error { @@ -114,6 +118,7 @@ func PrepareRoomsTable(db *sql.DB) (tables.Rooms, error) { {&s.selectRoomIDsStmt, selectRoomIDsSQL}, {&s.bulkSelectRoomIDsStmt, bulkSelectRoomIDsSQL}, {&s.bulkSelectRoomNIDsStmt, bulkSelectRoomNIDsSQL}, + {&s.purgeRoomStmt, purgeRoomSQL}, }.Prepare(db) } @@ -288,6 +293,13 @@ func (s *roomStatements) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, ro return roomNIDs, nil } +func (s *roomStatements) PurgeRoom( + ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, +) error { + _, err := sqlutil.TxStmt(txn, s.purgeRoomStmt).ExecContext(ctx, roomNID) + return err +} + func roomNIDsAsArray(roomNIDs []types.RoomNID) pq.Int64Array { nids := make([]int64, len(roomNIDs)) for i := range roomNIDs { diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index b94d4b80a..d1c585440 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -1390,6 +1390,9 @@ func (d *Database) PurgeRoom(ctx context.Context, roomID string) error { if err := d.EventsTable.PurgeEvents(ctx, txn, roomNID); err != nil { return fmt.Errorf("failed to purge events: %w", err) } + if err := d.RoomsTable.PurgeRoom(ctx, txn, roomNID); err != nil { + return fmt.Errorf("failed to purge room: %w", err) + } return nil }) } diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go index 25b611b3e..9288d65d9 100644 --- a/roomserver/storage/sqlite3/rooms_table.go +++ b/roomserver/storage/sqlite3/rooms_table.go @@ -309,3 +309,9 @@ func (s *roomStatements) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, ro } return roomNIDs, nil } + +func (s *roomStatements) PurgeRoom( + ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, +) error { + return fmt.Errorf("not implemented on SQLite") +} diff --git a/roomserver/storage/tables/interface.go b/roomserver/storage/tables/interface.go index 2712afb3b..051e2e305 100644 --- a/roomserver/storage/tables/interface.go +++ b/roomserver/storage/tables/interface.go @@ -82,6 +82,7 @@ type Rooms interface { SelectRoomIDsWithEvents(ctx context.Context, txn *sql.Tx) ([]string, error) BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID) ([]string, error) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, roomIDs []string) ([]types.RoomNID, error) + PurgeRoom(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID) error } type StateSnapshot interface {