2020-02-13 17:27:33 +00:00
// Copyright 2017-2018 New Vector Ltd
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlite3
import (
"context"
"database/sql"
2023-05-24 11:14:42 +01:00
"errors"
2020-09-21 14:55:46 +01:00
"fmt"
"strings"
2020-02-13 17:27:33 +00:00
2020-06-12 14:55:57 +01:00
"github.com/matrix-org/dendrite/internal/sqlutil"
2023-05-24 11:14:42 +01:00
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3/deltas"
2020-05-27 09:36:09 +01:00
"github.com/matrix-org/dendrite/roomserver/storage/tables"
2020-02-13 17:27:33 +00:00
"github.com/matrix-org/dendrite/roomserver/types"
)
2020-09-21 14:55:46 +01:00
// TODO: previous_reference_sha256 was NOT NULL before but it broke sytest because
// sytest sends no SHA256 sums in the prev_events references in the soft-fail tests.
// In Postgres an empty BYTEA field is not NULL so it's fine there. In SQLite it
// seems to care that it's empty and therefore hits a NOT NULL constraint on insert.
// We should really work out what the right thing to do here is.
2020-02-13 17:27:33 +00:00
const previousEventSchema = `
CREATE TABLE IF NOT EXISTS roomserver_previous_events (
previous_event_id TEXT NOT NULL ,
event_nids TEXT NOT NULL ,
2023-05-24 11:14:42 +01:00
UNIQUE ( previous_event_id )
2020-02-13 17:27:33 +00:00
) ;
`
// Insert an entry into the previous_events table.
// If there is already an entry indicating that an event references that previous event then
// add the event NID to the list to indicate that this event references that previous event as well.
// This should only be modified while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
// The lock is necessary to avoid data races when checking whether an event is already referenced by another event.
const insertPreviousEventSQL = `
INSERT OR REPLACE INTO roomserver_previous_events
2023-05-24 11:14:42 +01:00
( previous_event_id , event_nids )
VALUES ( $ 1 , $ 2 )
2020-02-13 17:27:33 +00:00
`
2020-09-21 14:55:46 +01:00
const selectPreviousEventNIDsSQL = `
SELECT event_nids FROM roomserver_previous_events
2023-05-24 11:14:42 +01:00
WHERE previous_event_id = $ 1
2020-09-21 14:55:46 +01:00
`
2020-02-13 17:27:33 +00:00
// 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
2023-05-24 11:14:42 +01:00
WHERE previous_event_id = $ 1
2020-02-13 17:27:33 +00:00
`
type previousEventStatements struct {
2020-07-21 10:48:49 +01:00
db * sql . DB
2020-02-13 17:27:33 +00:00
insertPreviousEventStmt * sql . Stmt
2020-09-21 14:55:46 +01:00
selectPreviousEventNIDsStmt * sql . Stmt
2020-02-13 17:27:33 +00:00
selectPreviousEventExistsStmt * sql . Stmt
}
2022-05-10 13:41:12 +01:00
func CreatePrevEventsTable ( db * sql . DB ) error {
2021-04-26 13:25:57 +01:00
_ , err := db . Exec ( previousEventSchema )
2023-05-24 11:14:42 +01:00
if err != nil {
return err
}
// check if the column exists
var cName string
migrationName := "roomserver: drop column reference_sha from roomserver_prev_events"
err = db . QueryRowContext ( context . Background ( ) , ` SELECT p.name FROM sqlite_master AS m JOIN pragma_table_info(m.name) AS p WHERE m.name = 'roomserver_previous_events' AND p.name = 'previous_reference_sha256' ` ) . Scan ( & cName )
if err != nil {
if errors . Is ( err , sql . ErrNoRows ) { // migration was already executed, as the column was removed
if err = sqlutil . InsertMigration ( context . Background ( ) , db , migrationName ) ; err != nil {
return fmt . Errorf ( "unable to manually insert migration '%s': %w" , migrationName , err )
}
return nil
}
return err
}
m := sqlutil . NewMigrator ( db )
m . AddMigrations ( [ ] sqlutil . Migration {
{
Version : migrationName ,
Up : deltas . UpDropEventReferenceSHAPrevEvents ,
} ,
} ... )
return m . Up ( context . Background ( ) )
2021-04-26 13:25:57 +01:00
}
2022-05-10 13:41:12 +01:00
func PreparePrevEventsTable ( db * sql . DB ) ( tables . PreviousEvents , error ) {
2020-07-21 10:48:49 +01:00
s := & previousEventStatements {
2020-08-19 15:38:27 +01:00
db : db ,
2020-07-21 10:48:49 +01:00
}
2020-02-13 17:27:33 +00:00
2021-07-28 18:30:04 +01:00
return s , sqlutil . StatementList {
2020-02-13 17:27:33 +00:00
{ & s . insertPreviousEventStmt , insertPreviousEventSQL } ,
2020-09-21 14:55:46 +01:00
{ & s . selectPreviousEventNIDsStmt , selectPreviousEventNIDsSQL } ,
2020-02-13 17:27:33 +00:00
{ & s . selectPreviousEventExistsStmt , selectPreviousEventExistsSQL } ,
2020-05-27 11:03:47 +01:00
} . Prepare ( db )
2020-02-13 17:27:33 +00:00
}
2020-05-27 09:36:09 +01:00
func ( s * previousEventStatements ) InsertPreviousEvent (
2020-02-13 17:27:33 +00:00
ctx context . Context ,
txn * sql . Tx ,
previousEventID string ,
eventNID types . EventNID ,
) error {
2020-09-21 14:55:46 +01:00
var eventNIDs string
eventNIDAsString := fmt . Sprintf ( "%d" , eventNID )
selectStmt := sqlutil . TxStmt ( txn , s . selectPreviousEventExistsStmt )
2023-05-24 11:14:42 +01:00
err := selectStmt . QueryRowContext ( ctx , previousEventID ) . Scan ( & eventNIDs )
2020-10-01 10:55:00 +01:00
if err != nil && err != sql . ErrNoRows {
2020-09-21 14:55:46 +01:00
return fmt . Errorf ( "selectStmt.QueryRowContext.Scan: %w" , err )
}
var nids [ ] string
if eventNIDs != "" {
nids = strings . Split ( eventNIDs , "," )
for _ , nid := range nids {
if nid == eventNIDAsString {
return nil
}
}
eventNIDs = strings . Join ( append ( nids , eventNIDAsString ) , "," )
} else {
eventNIDs = eventNIDAsString
}
insertStmt := sqlutil . TxStmt ( txn , s . insertPreviousEventStmt )
_ , err = insertStmt . ExecContext (
2023-05-24 11:14:42 +01:00
ctx , previousEventID , eventNIDs ,
2020-08-19 15:38:27 +01:00
)
return err
2020-02-13 17:27:33 +00:00
}
// Check if the event reference exists
// Returns sql.ErrNoRows if the event reference doesn't exist.
2020-05-27 09:36:09 +01:00
func ( s * previousEventStatements ) SelectPreviousEventExists (
2023-05-24 11:14:42 +01:00
ctx context . Context , txn * sql . Tx , eventID string ,
2020-02-13 17:27:33 +00:00
) error {
var ok int64
2020-06-12 14:55:57 +01:00
stmt := sqlutil . TxStmt ( txn , s . selectPreviousEventExistsStmt )
2023-05-24 11:14:42 +01:00
return stmt . QueryRowContext ( ctx , eventID ) . Scan ( & ok )
2020-02-13 17:27:33 +00:00
}