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:
Neil 2023-01-19 20:02:32 +00:00 committed by GitHub
parent 67f5c5bc1e
commit 738686ae68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 1213 additions and 170 deletions

View file

@ -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 {