Drop reference_sha column (#3083)

Companion PR to https://github.com/matrix-org/gomatrixserverlib/pull/383
This commit is contained in:
Till 2023-05-24 12:14:42 +02:00 committed by GitHub
parent 5d6221d191
commit 11b557097c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 299 additions and 227 deletions

View file

@ -20,6 +20,7 @@ import (
"database/sql"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/roomserver/storage/postgres/deltas"
"github.com/matrix-org/dendrite/roomserver/storage/tables"
"github.com/matrix-org/dendrite/roomserver/types"
)
@ -32,11 +33,9 @@ const previousEventSchema = `
CREATE TABLE IF NOT EXISTS roomserver_previous_events (
-- The string event ID taken from the prev_events key of an event.
previous_event_id TEXT NOT NULL,
-- The SHA256 reference hash taken from the prev_events key of an event.
previous_reference_sha256 BYTEA NOT NULL,
-- A list of numeric event IDs of events that reference this prev_event.
event_nids BIGINT[] NOT NULL,
CONSTRAINT roomserver_previous_event_id_unique UNIQUE (previous_event_id, previous_reference_sha256)
CONSTRAINT roomserver_previous_event_id_unique UNIQUE (previous_event_id)
);
`
@ -47,17 +46,17 @@ CREATE TABLE IF NOT EXISTS roomserver_previous_events (
// The lock is necessary to avoid data races when checking whether an event is already referenced by another event.
const insertPreviousEventSQL = "" +
"INSERT INTO roomserver_previous_events" +
" (previous_event_id, previous_reference_sha256, event_nids)" +
" VALUES ($1, $2, array_append('{}'::bigint[], $3))" +
" (previous_event_id, event_nids)" +
" VALUES ($1, array_append('{}'::bigint[], $2))" +
" ON CONFLICT ON CONSTRAINT roomserver_previous_event_id_unique" +
" DO UPDATE SET event_nids = array_append(roomserver_previous_events.event_nids, $3)" +
" WHERE $3 != ALL(roomserver_previous_events.event_nids)"
" DO UPDATE SET event_nids = array_append(roomserver_previous_events.event_nids, $2)" +
" WHERE $2 != ALL(roomserver_previous_events.event_nids)"
// Check if the event is referenced by another event in the table.
// This should only be done while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
const selectPreviousEventExistsSQL = "" +
"SELECT 1 FROM roomserver_previous_events" +
" WHERE previous_event_id = $1 AND previous_reference_sha256 = $2"
" WHERE previous_event_id = $1"
type previousEventStatements struct {
insertPreviousEventStmt *sql.Stmt
@ -66,7 +65,18 @@ type previousEventStatements struct {
func CreatePrevEventsTable(db *sql.DB) error {
_, err := db.Exec(previousEventSchema)
return err
if err != nil {
return err
}
m := sqlutil.NewMigrator(db)
m.AddMigrations([]sqlutil.Migration{
{
Version: "roomserver: drop column reference_sha from roomserver_prev_events",
Up: deltas.UpDropEventReferenceSHAPrevEvents,
},
}...)
return m.Up(context.Background())
}
func PreparePrevEventsTable(db *sql.DB) (tables.PreviousEvents, error) {
@ -82,12 +92,11 @@ func (s *previousEventStatements) InsertPreviousEvent(
ctx context.Context,
txn *sql.Tx,
previousEventID string,
previousEventReferenceSHA256 []byte,
eventNID types.EventNID,
) error {
stmt := sqlutil.TxStmt(txn, s.insertPreviousEventStmt)
_, err := stmt.ExecContext(
ctx, previousEventID, previousEventReferenceSHA256, int64(eventNID),
ctx, previousEventID, int64(eventNID),
)
return err
}
@ -95,9 +104,9 @@ func (s *previousEventStatements) InsertPreviousEvent(
// Check if the event reference exists
// Returns sql.ErrNoRows if the event reference doesn't exist.
func (s *previousEventStatements) SelectPreviousEventExists(
ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte,
ctx context.Context, txn *sql.Tx, eventID string,
) error {
var ok int64
stmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
return stmt.QueryRowContext(ctx, eventID, eventReferenceSHA256).Scan(&ok)
return stmt.QueryRowContext(ctx, eventID).Scan(&ok)
}