mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 13:22:46 +00:00
Add /_dendrite/admin/purgeRoom/{roomID}
(#2662)
This adds a new admin endpoint `/_dendrite/admin/purgeRoom/{roomID}`. It completely erases all database entries for a given room ID. The roomserver will start by clearing all data for that room and then will generate an output event to notify downstream components (i.e. the sync API and federation API) to do the same. It does not currently clear media and it is currently not implemented for SQLite since it relies on SQL array operations right now. Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> Co-authored-by: Till Faelligen <2353100+S7evinK@users.noreply.github.com>
This commit is contained in:
parent
67f5c5bc1e
commit
738686ae68
48 changed files with 1213 additions and 170 deletions
|
@ -124,6 +124,11 @@ type QueryProvider interface {
|
|||
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
// ExecProvider defines the interface for querys used by RunLimitedVariablesExec.
|
||||
type ExecProvider interface {
|
||||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||
}
|
||||
|
||||
// SQLite3MaxVariables is the default maximum number of host parameters in a single SQL statement
|
||||
// SQLlite can handle. See https://www.sqlite.org/limits.html for more information.
|
||||
const SQLite3MaxVariables = 999
|
||||
|
@ -153,6 +158,22 @@ func RunLimitedVariablesQuery(ctx context.Context, query string, qp QueryProvide
|
|||
return nil
|
||||
}
|
||||
|
||||
// RunLimitedVariablesExec split up a query with more variables than the used database can handle in multiple queries.
|
||||
func RunLimitedVariablesExec(ctx context.Context, query string, qp ExecProvider, variables []interface{}, limit uint) error {
|
||||
var start int
|
||||
for start < len(variables) {
|
||||
n := minOfInts(len(variables)-start, int(limit))
|
||||
nextQuery := strings.Replace(query, "($1)", QueryVariadic(n), 1)
|
||||
_, err := qp.ExecContext(ctx, nextQuery, variables[start:start+n]...)
|
||||
if err != nil {
|
||||
util.GetLogger(ctx).WithError(err).Error("ExecContext returned an error")
|
||||
return err
|
||||
}
|
||||
start = start + n
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StatementList is a list of SQL statements to prepare and a pointer to where to store the resulting prepared statement.
|
||||
type StatementList []struct {
|
||||
Statement **sql.Stmt
|
||||
|
|
|
@ -3,10 +3,11 @@ package sqlutil
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
sqlmock "github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
func TestShouldReturnCorrectAmountOfResulstIfFewerVariablesThanLimit(t *testing.T) {
|
||||
|
@ -164,6 +165,54 @@ func TestShouldReturnErrorIfRowsScanReturnsError(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRunLimitedVariablesExec(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
assertNoError(t, err, "Failed to make DB")
|
||||
|
||||
// Query and expect two queries to be executed
|
||||
mock.ExpectExec(`DELETE FROM WHERE id IN \(\$1\, \$2\)`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec(`DELETE FROM WHERE id IN \(\$1\, \$2\)`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
|
||||
variables := []interface{}{
|
||||
1, 2, 3, 4,
|
||||
}
|
||||
|
||||
query := "DELETE FROM WHERE id IN ($1)"
|
||||
|
||||
if err = RunLimitedVariablesExec(context.Background(), query, db, variables, 2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Query again, but only 3 parameters, still queries two times
|
||||
mock.ExpectExec(`DELETE FROM WHERE id IN \(\$1\, \$2\)`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec(`DELETE FROM WHERE id IN \(\$1\)`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
|
||||
if err = RunLimitedVariablesExec(context.Background(), query, db, variables[:3], 2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Query again, but only 2 parameters, queries only once
|
||||
mock.ExpectExec(`DELETE FROM WHERE id IN \(\$1\, \$2\)`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
|
||||
if err = RunLimitedVariablesExec(context.Background(), query, db, variables[:2], 2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Test with invalid query (typo) should return an error
|
||||
mock.ExpectExec(`DELTE FROM`).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0)).
|
||||
WillReturnError(errors.New("typo in query"))
|
||||
|
||||
if err = RunLimitedVariablesExec(context.Background(), "DELTE FROM", db, variables[:2], 2); err == nil {
|
||||
t.Fatal("expected an error, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func assertNoError(t *testing.T, err error, msg string) {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue